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

import SwiftUI
import Kingfisher

struct LiveRoomUserProfileDialogView: View {
    
    @Binding var isShowing: Bool
    
    @State private var introduceLineLimit: Int? = 2
    
    let viewModel: LiveRoomViewModel
    let userProfile: GetLiveRoomUserProfileResponse
    let onClickSetManager: (Int) -> Void
    let onClickReleaseManager: (Int) -> Void
    let onClickFollow: (Int) -> Void
    let onClickUnFollow: (Int) -> Void
    let onClickInviteSpeaker: (Int) -> Void
    let onClickChangeListener: (Int) -> Void
    let onClickMenu: (Int, String, Bool) -> Void
    let onClickNoChatting: (Int, String, String) -> Void
    
    var body: some View {
        ZStack {
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Text("프로필")
                        .font(.custom(Font.bold.rawValue, size: 15))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                    
                    Image("ic_close_white")
                        .onTapGesture {
                            isShowing = false
                        }
                }
                
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(alignment: .leading, spacing: 0) {
                        HStack(spacing: 8) {
                            Text(userProfile.nickname)
                                .font(.custom(Font.bold.rawValue, size: 18.3))
                                .foregroundColor(Color.grayee)
                            
                            Text(userProfile.gender)
                                .font(.custom(Font.medium.rawValue, size: 11.3))
                                .foregroundColor(.white)
                                .padding(.horizontal, 5.3)
                                .padding(.vertical, 3)
                                .background(Color.gray55)
                                .cornerRadius(23.3)
                            
                            if let isFollowing = userProfile.isFollowing {
                                Image(isFollowing ? "btn_following" : "btn_follow")
                                    .contentShape(Rectangle())
                                    .onTapGesture {
                                        if isFollowing {
                                            onClickUnFollow(userProfile.userId)
                                        } else {
                                            onClickFollow(userProfile.userId)
                                        }
                                    }
                                    .padding(.leading, 13.3)
                            }
                            
                            Spacer()
                            
                            Image("ic_seemore_vertical")
                                .onTapGesture {
                                    onClickMenu(
                                        userProfile.userId,
                                        userProfile.nickname,
                                        userProfile.isBlock
                                    )
                                }
                        }
                        .padding(.top, 21.3)
                        
                        KFImage(URL(string: userProfile.profileUrl))
                            .resizable()
                            .aspectRatio(CGSize(width: 1, height: 1), contentMode: .fill)
                            .cornerRadius(8)
                            .padding(.top, 21.3)
                        
                        HStack(spacing: 8) {
                            if let isSpeaker = userProfile.isSpeaker {
                                Text(isSpeaker ? "리스너 변경" : "스피커 초대")
                                    .font(.custom(Font.bold.rawValue, size: 15))
                                    .foregroundColor(Color.button)
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color.button)
                                    )
                                    .onTapGesture {
                                        if isSpeaker {
                                            onClickChangeListener(userProfile.userId)
                                        } else {
                                            onClickInviteSpeaker(userProfile.userId)
                                        }
                                        
                                        isShowing = false
                                    }
                            }
                            
                            if let isManager = userProfile.isManager {
                                Text(isManager ? "스탭 해제" : "스탭 지정")
                                    .font(.custom(Font.bold.rawValue, size: 15))
                                    .foregroundColor(Color.button)
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color.button)
                                    )
                                    .onTapGesture {
                                        if isManager {
                                            onClickReleaseManager(userProfile.userId)
                                        } else {
                                            onClickSetManager(userProfile.userId)
                                        }
                                        
                                        isShowing = false
                                    }
                            }
                            
                            if (userProfile.isSpeaker != nil && !viewModel.isEqualToStaffId(creatorId: userProfile.userId)) ||
                                (userProfile.isSpeaker != nil && userProfile.isManager != nil) {
                                Text("내보내기")
                                    .font(.custom(Font.bold.rawValue, size: 15))
                                    .foregroundColor(Color.button)
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color.button)
                                    )
                                    .onTapGesture {
                                        viewModel.kickOutId = userProfile.userId
                                        viewModel.isShowKickOutPopup = true
                                    }
                            }
                        }
                        .fixedSize(horizontal: false, vertical: true)
                        .padding(.top, 21.3)
                        
                        if let _ = userProfile.isManager {
                            Text("3분간 채팅금지")
                                .font(.custom(Font.bold.rawValue, size: 15))
                                .foregroundColor(Color.button)
                                .frame(maxWidth: .infinity)
                                .padding(.vertical, 13)
                                .cornerRadius(8)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 8)
                                        .strokeBorder(lineWidth: 1)
                                        .foregroundColor(Color.button)
                                )
                                .onTapGesture { onClickNoChatting(userProfile.userId, userProfile.nickname, userProfile.profileUrl) }
                                .padding(.top, 21.3)
                        }
                        
                        if let _ = userProfile.isFollowing, !userProfile.tags.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                            Text(userProfile.tags)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color.button)
                                .lineSpacing(3)
                                .padding(.top, 21.3)
                        }
                        
                        if let _ = userProfile.isFollowing, !userProfile.introduce.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
                            Text(userProfile.introduce)
                                .font(.custom(Font.medium.rawValue, size: 12))
                                .foregroundColor(Color.button)
                                .lineLimit(introduceLineLimit)
                                .lineSpacing(3)
                                .fixedSize(horizontal: false, vertical: true)
                                .padding(.top, 20)
                                .onTapGesture {
                                    if let _ = introduceLineLimit {
                                        self.introduceLineLimit = nil
                                    } else {
                                        self.introduceLineLimit = 2
                                    }
                                }
                        }
                    }
                }
            }
            .padding(.horizontal, 13.3)
            .padding(.top, 13.3)
            .padding(.bottom, 20)
            .background(Color.gray22)
            .cornerRadius(8)
            
            if viewModel.isShowKickOutPopup {
                SodaDialog(
                    title: "내보내기",
                    desc: viewModel.kickOutDesc,
                    confirmButtonTitle: "내보내기",
                    confirmButtonAction: {
                        viewModel.kickOut()
                        isShowing = false
                    },
                    cancelButtonTitle: "취소",
                    cancelButtonAction: {
                        viewModel.isShowKickOutPopup = false
                        viewModel.kickOutDesc = ""
                        viewModel.kickOutId = 0
                    }
                )
            }
        }
    }
}
