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

import SwiftUI

struct AuditionView: View {
    
    @StateObject var viewModel = AuditionViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                HomeNavigationBar(title: "오디션") {}
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVStack(alignment: .leading, spacing: 16.7) {
                        ForEach(0..<viewModel.auditionList.count, id: \.self) {
                            let item = viewModel.auditionList[$0]
                            
                            if $0 == 0 && !item.isOff {
                                VStack(alignment: .leading, spacing: 16.7) {
                                    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 {
                                            AppState.shared
                                                .setAppStep(
                                                    step: .auditionDetail(auditionId: item.id)
                                                )
                                        }
                                }
                            } else if $0 == viewModel.firstIsOffIndex {
                                VStack(alignment: .leading, spacing: 0) {
                                    Color.gray23
                                        .frame(maxWidth: .infinity)
                                        .frame(height: 6.7)
                                        .padding(.top, 13.3)
                                    
                                    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, 16.7)
                                        .onTapGesture {
                                            AppState.shared
                                                .setAppStep(
                                                    step: .auditionDetail(auditionId: item.id)
                                                )
                                        }
                                }
                            } else {
                                AuditionItemView(item: item)
                                    .onTapGesture {
                                        AppState.shared
                                            .setAppStep(
                                                step: .auditionDetail(auditionId: item.id)
                                            )
                                    }
                            }
                            
                            if $0 == viewModel.auditionList.count - 1 {
                                Color.clear
                                    .frame(height: 0)
                                    .onAppear {
                                        viewModel.getAuditionList()
                                    }
                            }
                        }
                    }
                    .padding(.horizontal, 13.3)
                }
            }
            .onAppear {
                viewModel.getAuditionList()
            }
        }
    }
}

#Preview {
    AuditionView()
}
