//
//  MemberProfileDialog.swift
//  SodaLive
//
//  Created by klaus on 9/7/24.
//

import SwiftUI
import Kingfisher

struct MemberProfileDialog: View {
    
    @StateObject var viewModel = UserViewModel()
    
    @Binding var isShowing: Bool
    let memberId: Int
    
    var body: some View {
        ZStack {
            Color.black.opacity(0.7).ignoresSafeArea()
                .onTapGesture {
                    isShowing = false
                }
            
            VStack(alignment: .leading, spacing: 21) {
                HStack(spacing: 0) {
                    Text("프로필")
                        .font(.custom(Font.bold.rawValue, size: 15))
                        .foregroundColor(Color.grayee)
                    
                    Spacer()
                    
                    Image("ic_close_white")
                        .onTapGesture {
                            isShowing = false
                        }
                }
                
                if let profile = viewModel.memberProfile {
                    Text(profile.nickname)
                        .font(.custom(Font.bold.rawValue, size: 18.3))
                        .foregroundColor(Color.grayee)
                    
                    KFImage(URL(string: profile.profileImageUrl))
                        .cancelOnDisappear(true)
                        .downsampling(
                            size: CGSize(
                                width: screenSize().width - 66.7,
                                height: screenSize().width - 66.7
                            )
                        )
                        .resizable()
                        .frame(maxWidth: screenSize().width - 66.7, maxHeight: screenSize().width - 66.7)
                        .aspectRatio(CGSize(width: 1, height: 1), contentMode: .fit)
                        .cornerRadius(8)
                    
                    HStack(spacing: 8) {
                        Text(profile.isBlocked ? "차단 해제" : "차단")
                            .font(.custom(Font.bold.rawValue, size: 15))
                            .foregroundColor(Color.button)
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 13)
                            .cornerRadius(8)
                            .contentShape(Rectangle())
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .strokeBorder(lineWidth: 1)
                                    .foregroundColor(Color.button)
                            )
                            .onTapGesture {
                                if profile.isBlocked {
                                    viewModel.memberUnBlock()
                                } else {
                                    viewModel.isShowUesrBlockConfirm = true
                                }
                            }
                        
                        Text("사용자 신고")
                            .font(.custom(Font.bold.rawValue, size: 15))
                            .foregroundColor(Color.button)
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 13)
                            .cornerRadius(8)
                            .contentShape(Rectangle())
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .strokeBorder(lineWidth: 1)
                                    .foregroundColor(Color.button)
                            )
                            .onTapGesture { viewModel.isShowUesrReportView = true }
                        
                        Text("프로필 신고")
                            .font(.custom(Font.bold.rawValue, size: 15))
                            .foregroundColor(Color.button)
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 13)
                            .cornerRadius(8)
                            .contentShape(Rectangle())
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .strokeBorder(lineWidth: 1)
                                    .foregroundColor(Color.button)
                            )
                            .onTapGesture { viewModel.isShowProfileReportConfirm = true }
                    }
                }
            }
            .padding(.horizontal, 13.3)
            .padding(.top, 13.3)
            .padding(.bottom, 20)
            .background(Color.gray22)
            .cornerRadius(8)
            .padding(.horizontal, 13.3)
            .frame(maxWidth: screenSize().width - 33.3)
            .onAppear {
                if memberId <= 1 {
                    viewModel.errorMessage = "잘못된 요청입니다."
                    viewModel.isShowPopup = true
                } else {
                    viewModel.getMemberProfile(memberId: memberId)
                }
            }
            .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.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .cornerRadius(20)
                        .padding(.bottom, 66.7)
                    Spacer()
                }
                .onDisappear {
                    if viewModel.dismissDialog {
                        isShowing = false
                    }
                }
            }
            
            if viewModel.isShowUesrBlockConfirm {
                UserBlockConfirmDialogView(
                    isShowing: $viewModel.isShowUesrBlockConfirm,
                    nickname: viewModel.nickname,
                    confirmAction: {
                        viewModel.memberBlock()
                    }
                )
            }
            
            if viewModel.isShowUesrReportView {
                UserReportDialogView(
                    isShowing: $viewModel.isShowUesrReportView,
                    confirmAction: { reason in
                        viewModel.report(type: .USER, reason: reason)
                    }
                )
            }
            
            if viewModel.isShowProfileReportConfirm {
                ProfileReportDialogView(
                    isShowing: $viewModel.isShowProfileReportConfirm,
                    confirmAction: {
                        viewModel.report(type: .PROFILE)
                    }
                )
            }
            
            if viewModel.isLoading {
                LoadingView()
            }
        }
    }
}

#Preview {
    MemberProfileDialog(isShowing: .constant(true), memberId: 1)
}
