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

import SwiftUI

struct LiveRoomDonationMessageDialog: View {
    
    @Binding var isShowing: Bool
    @StateObject var viewModel = LiveRoomViewModel()
    
    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]
                                
                                HStack(alignment: .top, spacing: 0) {
                                    VStack(alignment: .leading, spacing: 8) {
                                        Text("\(donationMessage.nickname)님이")
                                            .font(.custom(Font.medium.rawValue, size: 13.3))
                                            .foregroundColor(.white)
                                        
                                        Text("\(donationMessage.canMessage)")
                                            .font(.custom(Font.medium.rawValue, size: 13.3))
                                            .foregroundColor(.white)
                                        
                                        Text("'\(donationMessage.donationMessage)'")
                                            .font(.custom(Font.medium.rawValue, size: 13.3))
                                            .foregroundColor(.white)
                                    }
                                    
                                    Spacer()
                                    
                                    Image("ic_close_white")
                                        .resizable()
                                        .frame(width: 13.3, height: 13.3)
                                        .onTapGesture {
                                            viewModel.deleteDonationMessage(uuid: donationMessage.uuid)
                                        }
                                }
                                .padding(13.3)
                                .background(Color(hex: "333333"))
                                .cornerRadius(5.3)
                                .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(hex: "9970ff"))
                    .foregroundColor(Color.white)
                    .multilineTextAlignment(.leading)
                    .cornerRadius(20)
                    .padding(.bottom, 66.7)
                Spacer()
            }
        }
        .onAppear {
            viewModel.getDonationMessageList()
        }
    }
}