//
//  LiveRoomDonationRankingDialog.swift
//  SodaLive
//
//  Created by klaus on 2023/08/15.
//

import SwiftUI

struct LiveRoomDonationRankingDialog: View {
    
    @Binding var isShowing: Bool
    @StateObject var viewModel = LiveRoomViewModel()
    
    let columns = [GridItem(.flexible())]
    
    var body: some View {
        ZStack {
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Text("현재 라이브 후원랭킹")
                        .font(.custom(Font.bold.rawValue, size: 14.7))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                    
                    Image("ic_close_white")
                        .onTapGesture { isShowing = false }
                }
                
                if let donationStatus = viewModel.donationStatus {
                    LiveRoomDonationRankingTotalCanView(totalCan: donationStatus.totalCan)
                        .padding(.top, 25)
                    
                    HStack(spacing: 0) {
                        Text("전체")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.grayee)
                        
                        Text("\(donationStatus.totalCount)")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color.button)
                            .padding(.leading, 6.7)
                        
                        Text("명")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color.gray77)
                        
                        Spacer()
                    }
                    .padding(.top, 13.3)
                    
                    ScrollView(showsIndicators: false) {
                        LazyVGrid(columns: columns, spacing: 0) {
                            ForEach(0..<donationStatus.donationList.count, id: \.self) { index in
                                let item = donationStatus.donationList[index]
                                LiveRoomDonationRankingItemView(
                                    index: index,
                                    item: item,
                                    itemCount: donationStatus.donationList.count
                                )
                            }
                        }
                    }
                    .padding(.top, 8)
                }
            }
            .padding(20)
            .background(Color.gray22)
            .cornerRadius(8)
            
            if viewModel.isLoading {
                LoadingView()
            }
        }
        .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.getDonationStatus()
        }
    }
}
