//
//  ContentMainTabContentView.swift
//  SodaLive
//
//  Created by klaus on 2/21/25.
//

import SwiftUI

struct ContentMainTabContentView: View {
    
    @StateObject var viewModel = ContentMainTabContentViewModel()
    
    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.contentThemeList.isEmpty {
                        ContentMainNewContentViewV2(
                            title: "새로운 단편",
                            onClickMore: {
                                AppState.shared
                                    .setAppStep(step: .newContentAll(isFree: false))
                            },
                            themeList: viewModel.contentThemeList,
                            contentList: viewModel.newContentList
                        ) {
                            viewModel.getNewContentOfTheme(theme: $0)
                        }
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.rankSortTypeList.isEmpty {
                        ContentMainTabRankContentView(
                            title: "일간 랭킹",
                            isMore: false,
                            onClickMore: {},
                            sortList: viewModel.rankSortTypeList,
                            onClickSort: { viewModel.getContentRanking(sort: $0) },
                            contentList: viewModel.rankContentList
                        )
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.contentRankCreatorList.isEmpty {
                        ContentByChannelView(
                            title: "채널별 추천 단편",
                            creatorList: viewModel.contentRankCreatorList,
                            contentList: viewModel.likeCountRankContentList,
                            onClickCreator: {
                                viewModel.getPopularContentByCreator(creatorId: $0)
                            }
                        )
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.eventBannerList.isEmpty {
                        SectionEventBannerView(items: viewModel.eventBannerList)
                            .padding(.top, 30)
                    }
                    
                    if !viewModel.tagList.isEmpty {
                        ContentMainTagCurationView(
                            tagList: viewModel.tagList,
                            contentList: viewModel.tagCurationContentList
                        ) {
                            viewModel.getRecommendContentByTag(tag: $0)
                        }
                        .padding(.top, 30)
                    }
                    
                    if !viewModel.curationList.isEmpty {
                        ContentMainCurationViewV2(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 {
    ContentMainTabContentView()
}
