//
//  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
    
    var body: some View {
        ZStack {
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    Text("프로필")
                        .font(.custom(Font.bold.rawValue, size: 15))
                        .foregroundColor(Color(hex: "eeeeee"))
                    
                    Spacer()
                    
                    Image("ic_close_white")
                        .onTapGesture {
                            isShowing = false
                        }
                }
                .padding(.top, 13.3)
                
                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(hex: "eeeeee"))
                            
                            Text(userProfile.gender)
                                .font(.custom(Font.medium.rawValue, size: 11.3))
                                .foregroundColor(Color(hex: "ffffff"))
                                .padding(.horizontal, 5.3)
                                .padding(.vertical, 3)
                                .background(Color(hex: "555555"))
                                .cornerRadius(23.3)
                            
                            Spacer()
                            
                            Image("ic_seemore_vertical")
                                .onTapGesture {
                                    onClickMenu(
                                        userProfile.userId,
                                        userProfile.nickname,
                                        userProfile.isBlock
                                    )
                                }
                        }
                        .padding(.top, 21.3)
                        
                        HStack(spacing: 8) {
                            if let isFollwing = userProfile.isFollowing {
                                if isFollwing {
                                    HStack(spacing: 4) {
                                        Image("ic_alarm_selected")
                                            .resizable()
                                            .frame(width: 18.7, height: 18.7)
                                        
                                        Text("팔로잉")
                                            .font(.custom(Font.bold.rawValue, size: 12))
                                            .foregroundColor(Color(hex: "ffffff"))
                                    }
                                    .padding(.vertical, 7.3)
                                    .frame(maxWidth: .infinity)
                                    .background(Color(hex: "3e1b93"))
                                    .cornerRadius(16.7)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 16.7)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color(hex: "9970ff"))
                                    )
                                    .onTapGesture { onClickUnFollow(userProfile.userId) }
                                } else {
                                    HStack(spacing: 4) {
                                        Image("ic_alarm")
                                            .resizable()
                                            .frame(width: 18.7, height: 18.7)
                                        
                                        Text("팔로우")
                                            .font(.custom(Font.bold.rawValue, size: 12))
                                            .foregroundColor(Color(hex: "ffffff"))
                                    }
                                    .padding(.vertical, 7.3)
                                    .frame(maxWidth: .infinity)
                                    .cornerRadius(16.7)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 16.7)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color(hex: "9970ff"))
                                    )
                                    .onTapGesture { onClickFollow(userProfile.userId) }
                                }
                            }
                            
                            HStack(spacing: 4) {
                                Image("ic_message_send")
                                    .resizable()
                                    .frame(width: 18.7, height: 18.7)
                                
                                Text("메시지 보내기")
                                    .font(.custom(Font.bold.rawValue, size: 12))
                                    .foregroundColor(Color(hex: "ffffff"))
                            }
                            .padding(.vertical, 7.3)
                            .frame(maxWidth: .infinity)
                            .background(Color(hex: "9970ff"))
                            .cornerRadius(16.7)
                            .onTapGesture {
                                AppState.shared.setAppStep(step: .writeTextMessage(
                                    userId: userProfile.userId,
                                    nickname: userProfile.nickname))
                            }
                        }
                        .fixedSize(horizontal: false, vertical: true)
                        .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(hex: "9970ff"))
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color(hex: "9970ff"))
                                    )
                                    .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(hex: "9970ff"))
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color(hex: "9970ff"))
                                    )
                                    .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(hex: "9970ff"))
                                    .frame(maxWidth: .infinity)
                                    .padding(.vertical, 13)
                                    .cornerRadius(8)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 8)
                                            .strokeBorder(lineWidth: 1)
                                            .foregroundColor(Color(hex: "9970ff"))
                                    )
                                    .onTapGesture {
                                        viewModel.kickOutId = userProfile.userId
                                        viewModel.isShowKickOutPopup = true
                                    }
                            }
                        }
                        .fixedSize(horizontal: false, vertical: true)
                        .padding(.top, 21.3)
                        
                        Text(userProfile.tags)
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "9970ff"))
                            .lineSpacing(3)
                            .padding(.top, 21.3)
                        
                        Text(userProfile.introduce)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color(hex: "909090"))
                            .lineLimit(introduceLineLimit)
                            .lineSpacing(3)
                            .fixedSize(horizontal: false, vertical: true)
                            .padding(.vertical, 20)
                            .onTapGesture {
                                if let _ = introduceLineLimit {
                                    self.introduceLineLimit = nil
                                } else {
                                    self.introduceLineLimit = 2
                                }
                            }
                    }
                }
            }
            .padding(.horizontal, 13.3)
            .background(Color(hex: "222222").edgesIgnoringSafeArea(.all))
            .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
                    }
                )
            }
        }
    }
}