//
//  ContentCurationView.swift
//  SodaLive
//
//  Created by klaus on 2023/09/27.
//

import SwiftUI

struct ContentCurationView: View {
    
    @StateObject var viewModel = ContentCurationViewModel()
    
    let title: String
    let curationId: Int
    
    let columns = [
        GridItem(.flexible(), alignment: .top),
        GridItem(.flexible(), alignment: .top),
        GridItem(.flexible(), alignment: .top)
    ]
    
    var body: some View {
        NavigationView {
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 0) {
                    DetailNavigationBar(title: title)
                    
                    HStack(spacing: 13.3) {
                        Spacer()
                        
                        Text("최신순")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(
                                Color(hex: "e2e2e2")
                                    .opacity(viewModel.sort == .NEWEST ? 1 : 0.5)
                            )
                            .onTapGesture {
                                if viewModel.sort != .NEWEST {
                                    viewModel.sort = .NEWEST
                                }
                            }
                        
                        Text("높은 가격순")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(
                                Color(hex: "e2e2e2")
                                    .opacity(viewModel.sort == .PRICE_HIGH ? 1 : 0.5)
                            )
                            .onTapGesture {
                                if viewModel.sort != .PRICE_HIGH {
                                    viewModel.sort = .PRICE_HIGH
                                }
                            }
                        
                        Text("낮은 가격순")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(
                                Color(hex: "e2e2e2")
                                    .opacity(viewModel.sort == .PRICE_LOW ? 1 : 0.5)
                            )
                            .onTapGesture {
                                if viewModel.sort != .PRICE_LOW {
                                    viewModel.sort = .PRICE_LOW
                                }
                            }
                    }
                    .padding(.vertical, 13.3)
                    .padding(.horizontal, 20)
                    .background(Color(hex: "161616"))
                    .padding(.top, 13.3)
                    
                    HStack(spacing: 0) {
                        Text("전체")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "e2e2e2"))
                        
                        Text("\(viewModel.totalCount)")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "ff5c49"))
                            .padding(.leading, 8)
                        
                        Text("개")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "e2e2e2"))
                            .padding(.leading, 2)
                        
                        Spacer()
                    }
                    .padding(.vertical, 13.3)
                    .padding(.horizontal, 20)
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        LazyVGrid(columns: columns, spacing: 32) {
                            ForEach(0..<viewModel.contentList.count, id: \.self) { index in
                                ContentNewAllItemView(item: viewModel.contentList[index])
                                    .onAppear {
                                        if index == viewModel.contentList.count - 1 {
                                            viewModel.getContentList()
                                        }
                                    }
                            }
                        }
                    }
                }
                .onAppear {
                    viewModel.curationId = curationId
                    viewModel.getContentList()
                }
            }
        }
    }
}