//
//  ContentMainView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import SwiftUI
import RefreshableScrollView

struct ContentMainView: View {
    
    @StateObject var viewModel = ContentMainViewModel()
    
    var body: some View {
        ZStack(alignment: .bottomTrailing) {
            Color.black.ignoresSafeArea()
            
            RefreshableScrollView(
                refreshing: $viewModel.isLoading,
                action: {
                    viewModel.getMain()
                }
            ) {
                LazyVStack(alignment: .leading, spacing: 0) {
                    Text("콘텐츠 마켓")
                        .font(.custom(Font.bold.rawValue, size: 21.3))
                        .foregroundColor(Color(hex: "9970ff"))
                        .padding(.bottom, 26.7)
                    
                    if viewModel.newContentUploadCreatorList.count > 0 {
                        ContentMainNewContentCreatorView(items: viewModel.newContentUploadCreatorList)
                            .padding(.bottom, 26.7)
                    }
                    
                    if viewModel.bannerList.count > 0 {
                        ContentMainBannerView(items: viewModel.bannerList)
                            .padding(.bottom, 40)
                    }
                    
                    if viewModel.orderList.count > 0 {
                        ContentMainMyStashView(items: viewModel.orderList)
                            .padding(.bottom, 40)
                    }
                    
                    ContentMainNewContentView(
                        themes: viewModel.themeList,
                        items: viewModel.newContentList,
                        selectTheme: { viewModel.selectedTheme = $0 },
                        selectedTheme: $viewModel.selectedTheme
                    )
                    
                    if viewModel.curationList.count > 0 {
                        ContentMainCurationView(items: viewModel.curationList)
                            .padding(.top, 40)
                            .padding(.bottom, 20)
                    }
                }
                .padding(13.3)
            }
            
            if UserDefaults.string(forKey: .role) == MemberRole.CREATOR.rawValue {
                HStack(spacing: 5) {
                    Image("ic_thumb_play")
                        .resizable()
                        .frame(width: 20, height: 20)
                    
                    Text("콘텐츠 업로드")
                        .font(.custom(Font.bold.rawValue, size: 13.3))
                        .foregroundColor(.white)
                }
                .padding(13.3)
                .background(Color(hex: "9970ff"))
                .cornerRadius(44)
                .padding(.trailing, 16.7)
                .padding(.bottom, 16.7)
                .onTapGesture {
                    AppState.shared.setAppStep(step: .createContent)
                }
            }
            
            if viewModel.isLoading {
                LoadingView()
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color(hex: "9970ff"))
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
        .onAppear {
            viewModel.getMain()
        }
    }
}

struct ContentMainView_Previews: PreviewProvider {
    static var previews: some View {
        ContentMainView()
    }
}