//
//  AuditionView.swift
//  SodaLive
//
//  Created by klaus on 1/5/25.
//

import SwiftUI

struct AuditionView: View {
    
    @StateObject var viewModel = AuditionViewModel()
    @AppStorage("isAuditionNotification") private var isAuditionNotification: Bool = UserDefaults.bool(forKey: .isAuditionNotification)
    @AppStorage("token") private var token: String = UserDefaults.string(forKey: UserDefaultsKey.token)
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                HomeNavigationBar(title: "오디션") {
                    if !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                        Image(isAuditionNotification ? "btn_audition_notification_selected" : "btn_audition_notification_normal")
                            .onTapGesture {
                                viewModel.updateNotificationSettings()
                            }
                    }
                }
                
                HStack(spacing: 0) {
                    Text("보이스온 오디션 이용방법")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(.white)
                    
                    Spacer()
                    
                    Text("자세히>")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(.white)
                }
                .padding(.horizontal, 13.3)
                .padding(.vertical, 10)
                .background(Color.gray22)
                .cornerRadius(5.3)
                .padding(.horizontal, 13.3)
                .padding(.bottom, 15)
                .onTapGesture {
                    UIApplication.shared.open(
                        URL(string: "https://bit.ly/40EYuJc")!
                    )
                }
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVStack(alignment: .leading, spacing: 25) {
                        ForEach(0..<viewModel.auditionList.count, id: \.self) {
                            let item = viewModel.auditionList[$0]
                            
                            if $0 == 0 && !item.isOff {
                                VStack(alignment: .leading, spacing: 25) {
                                    HStack(spacing: 0) {
                                        Text("오디션")
                                            .font(.custom(Font.bold.rawValue, size: 18.3))
                                            .foregroundColor(Color.grayee)
                                        
                                        Text(" ON")
                                            .font(.custom(Font.bold.rawValue, size: 18.3))
                                            .foregroundColor(Color.mainRed)
                                        
                                        Spacer()
                                        
                                        Text("총 \(viewModel.inProgressCount)개")
                                            .font(.custom(Font.medium.rawValue, size: 11.3))
                                            .foregroundColor(Color.graybb)
                                    }
                                    
                                    AuditionItemView(item: item)
                                        .onTapGesture {
                                            if !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                                                if !item.isOff {
                                                    AppState.shared
                                                        .setAppStep(
                                                            step: .auditionDetail(auditionId: item.id)
                                                        )
                                                }
                                            } else {
                                                AppState.shared
                                                    .setAppStep(step: .login)
                                            }
                                        }
                                }
                            } else if $0 == viewModel.firstIsOffIndex {
                                VStack(alignment: .leading, spacing: 0) {
                                    Color.gray23
                                        .frame(maxWidth: .infinity)
                                        .frame(height: 6.7)
                                        .padding(.top, 5)
                                    
                                    HStack(spacing: 0) {
                                        Text("오디션")
                                            .font(.custom(Font.bold.rawValue, size: 18.3))
                                            .foregroundColor(Color.grayee)
                                        
                                        Text(" OFF")
                                            .font(.custom(Font.bold.rawValue, size: 18.3))
                                            .foregroundColor(Color.graybb)
                                        
                                        Spacer()
                                        
                                        Text("총 \(viewModel.completedCount)개")
                                            .font(.custom(Font.medium.rawValue, size: 11.3))
                                            .foregroundColor(Color.graybb)
                                    }
                                    .padding(.top, 30)
                                    
                                    AuditionItemView(item: item)
                                        .padding(.top, 25)
                                }
                            } else {
                                AuditionItemView(item: item)
                                    .onTapGesture {
                                        if !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                                            if !item.isOff {
                                                AppState.shared
                                                    .setAppStep(
                                                        step: .auditionDetail(auditionId: item.id)
                                                    )
                                            }
                                        } else {
                                            AppState.shared
                                                .setAppStep(step: .login)
                                        }
                                    }
                            }
                            
                            if $0 == viewModel.auditionList.count - 1 {
                                Color.clear
                                    .frame(height: 0)
                                    .onAppear {
                                        viewModel.getAuditionList()
                                    }
                            }
                        }
                    }
                    .padding(.horizontal, 13.3)
                }
            }
            .onAppear {
                viewModel.getAuditionList()
            }
        }
    }
}

#Preview {
    AuditionView()
}
