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

import SwiftUI
import Kingfisher

struct LiveRoomProfileDialog: View {
    
    @Binding var isShowing: Bool
    let profileInfo: LiveRoomMember
    let creatorId: Int
    let isSpeaker: Bool
    
    let memberId = UserDefaults.int(forKey: .userId)
    
    var onClickInviteSpeaker: ((Int) -> Void)? = nil
    var onClickChangeListener: ((Int) -> Void)? = nil
    
    var body: some View {
        ZStack {
            Color.black.opacity(0.7).ignoresSafeArea()
                .onTapGesture {
                    isShowing = false
                }
            
            HStack(spacing: 13.3) {
                KFImage(URL(string: profileInfo.profileImage))
                    .resizable()
                    .frame(width: 80, height: 116.7, alignment: .top)
                    .clipped()
                    .cornerRadius(13.3)
                
                VStack(alignment: .leading, spacing: 0) {
                    Text(profileInfo.nickname)
                        .font(.custom(Font.bold.rawValue, size: 20))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .padding(.top, 6.7)
                    
                    Spacer()
                    
                    if isSpeaker {
                        if profileInfo.role == .LISTENER, let onClickInviteSpeaker = onClickInviteSpeaker {
                            Text("스피커로 초대")
                                .font(.custom(Font.medium.rawValue, size: 10))
                                .foregroundColor(Color(hex: "9970ff"))
                                .padding(.horizontal, 15.4)
                                .padding(.vertical, 8.3)
                                .background(Color.white)
                                .cornerRadius(13.3)
                                .onTapGesture {
                                    onClickInviteSpeaker(profileInfo.id)
                                    isShowing = false
                                }
                        }
                        
                        if (memberId == creatorId || memberId == profileInfo.id) && profileInfo.id != creatorId && profileInfo.role == .SPEAKER,
                            let onClickChangeListener = onClickChangeListener {
                            Text("리스너로 변경")
                                .font(.custom(Font.medium.rawValue, size: 10))
                                .foregroundColor(Color(hex: "9970ff"))
                                .padding(.horizontal, 15.4)
                                .padding(.vertical, 8.3)
                                .background(Color.white)
                                .cornerRadius(13.3)
                                .onTapGesture {
                                    onClickChangeListener(profileInfo.id)
                                    isShowing = false
                                }
                        }
                    }
                }
                .frame(height: 116.7)
                
                Spacer()
            }
            .padding(20)
            .frame(width: screenSize().width - 53.4)
            .background(Color(hex: "9970ff"))
            .cornerRadius(16.7)
        }
    }
}