//
//  SeriesListAllView.swift
//  SodaLive
//
//  Created by klaus on 4/29/24.
//

import SwiftUI

struct SeriesListAllView: View {
    
    @ObservedObject var viewModel = SeriesListAllViewModel()
    @State var columns: [GridItem] = []
    
    let creatorId: Int
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "시리즈 전체보기")
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVGrid(columns: columns, spacing: 33.3) {
                        ForEach(0..<viewModel.seriesList.count, id: \.self) { index in
                            let item = viewModel.seriesList[index]
                            SeriesListItemView(itemWidth: (screenSize().width - 40) / 3, item: item)
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    AppState.shared
                                        .setAppStep(step: .seriesDetail(seriesId: item.seriesId))
                                }
                                .onAppear {
                                    if index == viewModel.seriesList.count - 1 {
                                        viewModel.getSeriesList()
                                    }
                                }
                        }
                    }
                    .padding(13.3)
                }
            }
        }
        .onAppear {
            columns = [
                GridItem(.fixed((screenSize().width - 40) / 3), alignment: .top),
                GridItem(.fixed((screenSize().width - 40) / 3), alignment: .top),
                GridItem(.fixed((screenSize().width - 40) / 3), alignment: .top)
            ]
            viewModel.creatorId = creatorId
            viewModel.getSeriesList()
        }
    }
}

#Preview {
    SeriesListAllView(creatorId: 0)
}
