//
//  CompletedSeriesView.swift
//  SodaLive
//
//  Created by klaus on 2/22/25.
//

import SwiftUI

struct CompletedSeriesView: View {
    
    @StateObject var viewModel = CompletedSeriesViewModel()
    
    private let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 13.3) {
                DetailNavigationBar(title: "완결 시리즈")
                
                HStack(alignment: .center, spacing: 0) {
                    Text("전체")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(.graye2)
                    
                    Text("\(viewModel.totalCount)")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(.mainRed)
                        .padding(.leading, 6.7)
                    
                    Text("개")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(.graye2)
                    
                    Spacer()
                }
                .padding(.horizontal, 13.3)
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVGrid(columns: columns, spacing: 13.3) {
                        ForEach(0..<viewModel.rankCompleteSeriesList.count, id: \.self) { index in
                            let item = viewModel.rankCompleteSeriesList[index]
                            
                            SeriesListItemView(
                                itemWidth: (screenSize().width - (13.3 * 4)) / 3,
                                item: item
                            )
                            .onAppear {
                                if index == viewModel.rankCompleteSeriesList.count - 1 {
                                    viewModel.getCompletedSeries()
                                }
                            }
                        }
                    }
                    .padding(.horizontal, 13.3)
                }
            }
        }
        .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()
            }
        }
        .onAppear {
            viewModel.getCompletedSeries()
        }
    }
}

#Preview {
    CompletedSeriesView()
}