//
//  ContentAllByThemeView.swift
//  SodaLive
//
//  Created by klaus on 2/14/24.
//

import SwiftUI

struct ContentAllByThemeView: View {
    @StateObject var viewModel = ContentAllByThemeViewModel()
    
    let themeId: 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(alignment: .leading, spacing: 0) {
                    DetailNavigationBar(title: viewModel.theme)
                    
                    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.themeId = themeId
                    viewModel.getContentList()
                }
            }
        }
    }
}
