//
//  ContentMainIntroduceCreatorAllView.swift
//  SodaLive
//
//  Created by klaus on 2/22/25.
//

import SwiftUI

struct ContentMainIntroduceCreatorAllView: View {
    
    @StateObject var viewModel = ContentMainIntroduceCreatorAllViewModel()
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    var body: some View {
        NavigationView {
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 13.3) {
                    DetailNavigationBar(title: "크리에이터 소개")
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        LazyVGrid(columns: columns, spacing: 13.3) {
                            ForEach(0..<viewModel.introduceCreatorList.count, id: \.self) { index in
                                let item = viewModel.introduceCreatorList[index]
                                ContentNewAllItemView(item: item)
                                    .onAppear {
                                        if index == viewModel.introduceCreatorList.count - 1 {
                                            viewModel.getIntroduceCreatorList()
                                        }
                                    }
                            }
                        }
                        .padding(.horizontal, 13.3)
                    }
                }
                .onAppear {
                    viewModel.getIntroduceCreatorList()
                }
            }
            .navigationBarHidden(true)
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .frame(width: screenSize().width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .cornerRadius(20)
                        .padding(.bottom, 66.7)
                    Spacer()
                }
            }
        }
    }
}

#Preview {
    ContentMainIntroduceCreatorAllView()
}