//
//  UserProfileFanTalkAllView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/29.
//

import SwiftUI
import Kingfisher

struct UserProfileFanTalkAllView: View {
    
    let userId: Int
    @StateObject var viewModel = UserProfileFanTalkViewModel()
    
    @State private var cheersContent: String = ""
    @State private var cheersId: Int = 0
    
    @State private var memberId: Int = 0
    @State private var isShowMemberProfilePopup: Bool = false
    
    var body: some View {
        GeometryReader { proxy in
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "팬 Talk 전체보기")
                    
                    VStack(alignment: .leading, spacing: 0) {
                        HStack(spacing: 6.7) {
                            Text("응원")
                                .font(.custom(Font.medium.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            Text("\(viewModel.cheersTotalCount)")
                                .font(.custom(Font.medium.rawValue, size: 12))
                                .foregroundColor(Color.gray77)
                        }
                        .padding(.top, 20)
                        
                        Rectangle()
                            .frame(height: 1)
                            .foregroundColor(Color.gray90.opacity(0.5))
                            .padding(.top, 13.3)
                        
                        HStack(spacing: 0) {
                            TextField("응원댓글을 입력하세요", text: $cheersContent)
                                .autocapitalization(.none)
                                .disableAutocorrection(true)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color.grayee)
                                .accentColor(Color.button)
                                .keyboardType(.default)
                                .padding(.horizontal, 13.3)
                            
                            Spacer()
                            
                            Image("btn_message_send")
                                .resizable()
                                .frame(width: 35, height: 35)
                                .padding(6.7)
                                .onTapGesture {
                                    hideKeyboard()
                                    viewModel.writeCheers(creatorId: userId, cheersContent: cheersContent)
                                    cheersContent = ""
                                }
                        }
                        .background(Color.gray23)
                        .cornerRadius(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .strokeBorder(lineWidth: 1)
                                .foregroundColor(Color.button)
                        )
                        .padding(.top, 13.3)
                        
                        Rectangle()
                            .frame(height: 1)
                            .foregroundColor(Color.gray90.opacity(0.5))
                            .padding(.top, 13.3)
                        
                        ScrollView(.vertical, showsIndicators: false) {
                            LazyVStack(spacing: 20) {
                                if viewModel.cheersTotalCount > 0 {
                                    ForEach(0..<viewModel.cheersList.count, id: \.self) { index in
                                        let cheer = viewModel.cheersList[index]
                                        UserProfileFanTalkCheersItemView(
                                            userId: userId,
                                            cheersItem: cheer,
                                            writeCheerReply: { cheersReplyContent in
                                                viewModel.writeCheersReply(parentCheersId: cheer.cheersId, creatorId: userId, cheersReplyContent: cheersReplyContent)
                                            },
                                            modifyCheer: { cheersId, cheersReplyContent in
                                                viewModel.modifyCheers(cheersId: cheersId, creatorId: userId, cheersContent: cheersReplyContent)
                                            },
                                            reportPopup: { cheersId in
                                                viewModel.reportCheersId = cheersId
                                                viewModel.isShowCheersReportView = true
                                            },
                                            onClickDelete: { cheersId in
                                                self.cheersId = cheersId
                                                viewModel.isShowCheersDeleteView = true
                                            },
                                            onClickProfile: {
                                                self.memberId = $0
                                                self.isShowMemberProfilePopup = true
                                            }
                                        )
                                        .onAppear {
                                            if index == viewModel.cheersList.count - 1 {
                                                viewModel.getCheersList(creatorId: userId)
                                            }
                                        }
                                        .onTapGesture {
                                            hideKeyboard()
                                        }
                                    }
                                } else {
                                    Text("응원이 없습니다.\n\n처음으로 응원을 해보세요!")
                                        .font(.custom(Font.light.rawValue, size: 13.3))
                                        .foregroundColor(Color.graybb)
                                        .multilineTextAlignment(.center)
                                        .fixedSize(horizontal: false, vertical: true)
                                        .padding(.vertical, 60)
                                        .frame(width: screenSize().width - 26.7)
                                    
                                    Spacer()
                                }
                            }
                        }
                        .padding(.top, 20)
                    }
                    .frame(width: screenSize().width - 26.7)
                    .onAppear {
                        viewModel.getCheersList(creatorId: userId)
                    }
                    .padding(.horizontal, 13.3)
                }
                .onTapGesture { hideKeyboard() }
                .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()
                    }
                }
                
                ZStack {
                    if viewModel.isShowCheersReportView {
                        CheersReportDialogView(
                            isShowing: $viewModel.isShowCheersReportView,
                            confirmAction: { reason in
                                viewModel.report(type: .CHEERS, reason: reason)
                            }
                        )
                    }
                    
                    if viewModel.isShowCheersDeleteView {
                        if viewModel.isShowCheersDeleteView {
                            SodaDialog(
                                title: "응원글 삭제",
                                desc: "삭제하시겠습니까?",
                                confirmButtonTitle: "삭제",
                                confirmButtonAction: {
                                    viewModel.modifyCheers(cheersId: cheersId, creatorId: userId, isActive: false)
                                    viewModel.isShowCheersDeleteView = false
                                    self.cheersId = 0
                                },
                                cancelButtonTitle: "취소",
                                cancelButtonAction: { viewModel.isShowCheersDeleteView = false }
                            )
                        }
                    }
                    
                    if isShowMemberProfilePopup {
                        MemberProfileDialog(isShowing: $isShowMemberProfilePopup, memberId: memberId)
                    }
                }
            }
        }
    }
}