//
//  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.refresh()
                }
            ) {
                LazyVStack(alignment: .leading, spacing: 0) {
                    Text("콘텐츠 마켓")
                        .font(.custom(Font.bold.rawValue, size: 21.3))
                        .foregroundColor(Color(hex: "9970ff"))
                        .padding(.bottom, 26.7)
                        .padding(.horizontal, 13.3)
                    
                    if !viewModel.isLoading {
                        ContentMainNewContentCreatorView()
                            .padding(.bottom, 26.7)
                            .padding(.horizontal, 13.3)
                        
                        ContentMainBannerView()
                            .padding(.bottom, 40)
                            .padding(.horizontal, 13.3)
                        
                        ContentMainMyStashView()
                            .padding(.bottom, 40)
                            .padding(.horizontal, 13.3)
                        
                        ContentMainNewContentView()
                            .padding(.horizontal, 13.3)
                        
                        ContentMainRankingView()
                            .padding(.top, 40)
                            .padding(.horizontal, 13.3)
                            .animation(nil)
                        
                        ContentMainCurationView()
                            .padding(.top, 40)
                            .padding(.bottom, 20)
                    }
                }
                .padding(.vertical, 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()
            }
        }
    }
}

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