//
//  LiveRoomProfileItemTitleView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/14.
//

import SwiftUI
import Kingfisher

struct LiveRoomProfileItemTitleView: View {
    
    let title: String
    let count: Int?
    let totalCount: Int?
    
    var body: some View {
        HStack(spacing: 0) {
            Text(title)
                .font(.custom(Font.bold.rawValue, size: 13))
                .foregroundColor(Color.grayee)
            
            if let count = count {
                Text("\(count)")
                    .font(.custom(Font.medium.rawValue, size: 13))
                    .foregroundColor(Color.button)
                    .padding(.leading, 6.7)
            }
            
            if let totalCount = totalCount {
                Text("/\(totalCount > 5 ? 5 : totalCount - 1)")
                    .font(.custom(Font.medium.rawValue, size: 13))
                    .foregroundColor(Color.graybb)
            }
            
            Spacer()
        }
    }
}

struct LiveRoomProfileItemMasterView: View {
    
    let id: Int
    let nickname: String
    let profileUrl: String
    let onClickProfile: (Int) -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            HStack(spacing: 0) {
                KFImage(URL(string: profileUrl))
                    .cancelOnDisappear(true)
                    .downsampling(
                        size: CGSize(
                            width: 60,
                            height: 60
                        )
                    )
                    .resizable()
                    .frame(width: 60, height: 60)
                    .clipShape(Circle())
                    .onTapGesture { onClickProfile(id) }
                
                Image("ic_crown")
                    .padding(.leading, 16.7)
                
                Text(nickname)
                    .font(.custom(Font.medium.rawValue, size: 14))
                    .foregroundColor(Color.grayee)
                    .padding(.leading, 4)
            }
            .padding(.horizontal, 16.7)
            
            Rectangle()
                .frame(height: 1)
                .foregroundColor(Color.gray90.opacity(0.3))
        }
    }
}

struct LiveRoomProfileItemUserView: View {
    let isStaff: Bool
    let userId: Int
    let creatorId: Int
    let nickname: String
    let profileUrl: String
    let role: LiveRoomMemberRole
    
    let onClickChangeListener: (Int) -> Void
    let onClickInviteSpeaker: (Int) -> Void
    let onClickKickOut: (Int) -> Void
    let onClickProfile: (Int) -> Void
    let onClickNoChatting: (Int, String, String) -> Void
    
    var body: some View {
        ZStack {
            VStack(spacing: 10) {
                HStack(spacing: 0) {
                    KFImage(URL(string: profileUrl))
                        .cancelOnDisappear(true)
                        .downsampling(
                            size: CGSize(
                                width: 46.7,
                                height: 46.7
                            )
                        )
                        .resizable()
                        .frame(width: 46.7, height: 46.7)
                        .clipShape(Circle())
                        .onTapGesture { onClickProfile(userId) }
                    
                    if role == .MANAGER {
                        Image("ic_badge_manager")
                            .padding(.leading, 16.7)
                        
                        Text(nickname)
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color.grayee)
                            .lineLimit(2)
                            .multilineTextAlignment(.leading)
                            .padding(.leading, 4)
                            .padding(.trailing, 10)
                    } else {
                        Text(nickname)
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color.grayee)
                            .lineLimit(2)
                            .multilineTextAlignment(.leading)
                            .padding(.horizontal, 10)
                    }
                    
                    Spacer()
                    
                    if role == .LISTENER && isStaff {
                        Text("스피커로 초대")
                            .font(.custom(Font.medium.rawValue, size: 10))
                            .foregroundColor(.white)
                            .padding(.horizontal, 5.5)
                            .padding(.vertical, 12)
                            .background(Color.button.opacity(0.3))
                            .cornerRadius(6.7)
                            .overlay(
                                RoundedRectangle(cornerRadius: 6.7)
                                    .stroke(Color.button, lineWidth: 1)
                            )
                            .onTapGesture {
                                onClickInviteSpeaker(userId)
                            }
                    }
                    
                    if role == .SPEAKER && (userId == UserDefaults.int(forKey: .userId) || isStaff) {
                        Text("리스너로 변경")
                            .font(.custom(Font.medium.rawValue, size: 10))
                            .foregroundColor(.white)
                            .padding(.horizontal, 5.5)
                            .padding(.vertical, 12)
                            .background(Color.button)
                            .cornerRadius(6.7)
                            .onTapGesture {
                                onClickChangeListener(userId)
                            }
                    }
                    
                    if role != .MANAGER && creatorId == UserDefaults.int(forKey: .userId) {
                        Text("채금")
                            .font(.custom(Font.medium.rawValue, size: 10))
                            .foregroundColor(.white)
                            .padding(.horizontal, 5.5)
                            .padding(.vertical, 12)
                            .background(Color.button.opacity(0.3))
                            .cornerRadius(6.7)
                            .overlay(
                                RoundedRectangle(cornerRadius: 6.7)
                                    .stroke(Color.button, lineWidth: 1)
                            )
                            .cornerRadius(6.7)
                            .padding(.leading, 10)
                            .onTapGesture {
                                onClickNoChatting(userId, nickname, profileUrl)
                            }
                    }
                    
                    if role != .MANAGER && isStaff {
                        Image("ic_kick_out")
                            .padding(.leading, 10)
                            .onTapGesture {
                                onClickKickOut(userId)
                            }
                    }
                }
                
                Rectangle()
                    .frame(height: 1)
                    .foregroundColor(Color.gray90.opacity(0.3))
            }
            .padding(.horizontal, 16.7)
        }
    }
}

struct LiveRoomProfileRequestSpeakerView: View {
    
    let onClickRequestSpeaker: () -> Void
    
    var body: some View {
        HStack(spacing: 6.7) {
            Spacer()
            Image("ic_request_speak")
            Text("스피커 요청하기")
                .font(.custom(Font.bold.rawValue, size: 13.3))
                .foregroundColor(.white)
            Spacer()
        }
        .padding(.vertical, 8)
        .overlay(
            RoundedRectangle(cornerRadius: 5.3)
                .stroke(Color.gray90, lineWidth: 1)
        )
        .onTapGesture {
            onClickRequestSpeaker()
        }
        .padding(.horizontal, 16.7)
    }
}
