//
//  LiveRoomHeartRankingDialog.swift
//  SodaLive
//
//  Created by klaus on 11/11/24.
//

import SwiftUI

struct LiveRoomHeartRankingDialog: View {
    
    @Binding var isShowing: Bool
    @Binding var isShowPopup: Bool
    
    let errorMessage: String
    let isLoading: Bool
    let columns = [GridItem(.flexible())]
    let heartStatus: GetLiveRoomHeartListResponse?
    let getHeartStatus: () -> Void
    
    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 heartStatus = heartStatus {
                    HStack(alignment: .center, spacing: 0) {
                        Text("합계")
                            .font(.custom(Font.bold.rawValue, size: 13.3))
                            .foregroundColor(Color.grayd2)
                        
                        Spacer()
                        
                        Text("\(heartStatus.totalHeart)")
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color.button)
                        
                        Text("하트")
                            .font(.custom(Font.medium.rawValue, size: 10.7))
                            .foregroundColor(Color.graybb)
                            .padding(.leading, 4)
                    }
                    .padding(.horizontal, 18.7)
                    .padding(.vertical, 10.7)
                    .background(Color.bg)
                    .cornerRadius(8)
                    .padding(.top, 25)
                    
                    HStack(spacing: 0) {
                        Text("전체")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.grayee)
                        
                        Text("\(heartStatus.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..<heartStatus.heartList.count, id: \.self) { index in
                                let item = heartStatus.heartList[index]
                                LiveRoomHeartRankingItemView(
                                    index: index,
                                    item: item,
                                    itemCount: heartStatus.totalCount
                                )
                            }
                        }
                    }
                    .padding(.top, 8)
                }
            }
            .padding(20)
            .background(Color.gray22)
            .cornerRadius(8)
            
            if isLoading {
                LoadingView()
            }
        }
        .popup(isPresented: $isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
            HStack {
                Spacer()
                Text(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()
            }
            .onDisappear {
                isShowing = false
            }
        }
        .onAppear {
            getHeartStatus()
        }
    }
}

#Preview {
    LiveRoomHeartRankingDialog(
        isShowing: .constant(true),
        isShowPopup: .constant(false),
        errorMessage: "",
        isLoading: false,
        heartStatus: nil,
        getHeartStatus: {}
    )
}
