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

import SwiftUI

struct LiveRoomDonationMessageDialog: View {
    
    @StateObject var viewModel: LiveRoomViewModel
    @Binding var isShowing: Bool
    
    @State var creatorId = 0
    
    var body: some View {
        ZStack {
            Color.black
                .opacity(0.7)
                .ignoresSafeArea()
                .onTapGesture {
                    hideKeyboard()
                }
            
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Text("후원 히스토리")
                        .font(.custom(Font.bold.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Text("(\(viewModel.donationMessageCount))")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Spacer()
                    
                    Text("닫기")
                        .font(.custom(Font.light.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .onTapGesture { isShowing = false }
                }
                
                ScrollView {
                    if viewModel.donationMessageList.count > 0 {
                        LazyVStack(spacing: 10.7) {
                            ForEach(0..<viewModel.donationMessageList.count, id: \.self) { index in
                                let donationMessage = viewModel.donationMessageList[index]
                                
                                LiveRoomDonationMessageItemView(message: donationMessage, creatorId: creatorId) {
                                    viewModel.deleteDonationMessage(uuid: $0)
                                }
                                .onTapGesture {
                                    UIPasteboard.general.string = donationMessage.donationMessage
                                    self.viewModel.errorMessage = "후원 히스토리가 복사되었습니다."
                                    self.viewModel.isShowPopup = true
                                }
                            }
                        }
                        .padding(.top, 18.7)
                    } else {
                        Text("후원 히스토리가 없습니다.")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color(hex: "eeeeee"))
                            .padding(.top, 30)
                    }
                }
            }
            .padding(20)
            .background(Color(hex: "222222"))
            .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.getDonationMessageList()
            creatorId = viewModel.liveRoomInfo?.creatorId ?? 0
        }
    }
}
