//
//  ContentMainTabSeriesView.swift
//  SodaLive
//
//  Created by klaus on 2/20/25.
//

import SwiftUI

struct ContentMainTabSeriesView: View {
    
    @StateObject var viewModel = ContentMainTabSeriesViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            ScrollView(.vertical, showsIndicators: false) {
                VStack(spacing: 0) {
                    if !viewModel.bannerList.isEmpty {
                        ContentMainBannerViewV2(bannerList: viewModel.bannerList)
                            .padding(.horizontal, 13.3)
                    }
                    
                    if !viewModel.originalAudioDramaList.isEmpty {
                        ContentMainOriginalAudioDramaView(itemList: viewModel.originalAudioDramaList) {
                        }
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.rankSeriesList.isEmpty {
                        ContentMainSeriesRankingView(seriesList: viewModel.rankSeriesList)
                            .padding(.top, 30)
                    }
                    
                    if !viewModel.genreList.isEmpty {
                        ContentMainSeriesByGenreView(
                            genreList: viewModel.genreList,
                            itemList: viewModel.recommendSeriesList
                        ) {
                            viewModel.getRecommendSeriesListByGenre(genreId: $0)
                        }
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.newSeriesList.isEmpty {
                        ContentMainNewOrRecommendSeriesView(
                            title: "새로운 시리즈",
                            recommendSeriesList: viewModel.newSeriesList
                        )
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.rankCompleteSeriesList.isEmpty {
                        ContentMainCompletedSeriesView(
                            itemList: viewModel.rankCompleteSeriesList,
                            onClickMore: {
                                AppState.shared
                                    .setAppStep(step: .completedSeriesAll)
                            }
                        )
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.seriesRankCreatorList.isEmpty {
                        SeriesByChannelView(
                            title: "채널별 추천 시리즈",
                            creatorList: viewModel.seriesRankCreatorList,
                            seriesList: viewModel.recommendSeriesByChannel
                        ) {
                            viewModel.getRecommendSeriesByCreator(creatorId: $0)
                        }
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.eventBannerList.isEmpty {
                        SectionEventBannerView(items: viewModel.eventBannerList)
                            .padding(.top, 30)
                    }
                    
                    if !viewModel.curationList.isEmpty {
                        ContentMainSeriesCurationView(curationList: viewModel.curationList)
                            .padding(.top, 30)
                    }
                }
                .onAppear {
                    viewModel.fetchData()
                }
            }
        }
        .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 {
    ContentMainTabSeriesView()
}
