//
//  AudioContentCommentItemView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/13.
//

import SwiftUI
import Kingfisher

struct AudioContentCommentItemView: View {
    let contentCreatorId: Int
    let audioContentId: Int
    let commentItem: GetAudioContentCommentListItem
    let isReplyComment: Bool
    let isShowPopupMenuButton: Bool
    
    let modifyComment: (Int, String) -> Void
    let onClickDelete: (Int) -> Void
    let onClickProfile: (Int) -> Void
    
    @State var isShowPopupMenu: Bool = false
    @State var isModeModify: Bool = false
    @State var comment: String = ""
    
    var body: some View {
        ZStack(alignment: .topTrailing) {
            VStack(alignment: .leading, spacing: 0) {
                HStack(spacing: 6.7) {
                    KFImage(URL(string: commentItem.profileUrl))
                        .cancelOnDisappear(true)
                        .downsampling(
                            size: CGSize(
                                width: 40,
                                height: 40
                            )
                        )
                        .resizable()
                        .frame(width: 40, height: 40)
                        .clipShape(Circle())
                        .onTapGesture {
                            if UserDefaults.int(forKey: .userId) != commentItem.writerId {
                                onClickProfile(commentItem.writerId)
                            }
                        }
                    
                    VStack(alignment: .leading, spacing: 0) {
                        HStack(spacing: 6.7) {
                            Text(commentItem.nickname)
                                .font(.custom(Font.medium.rawValue, size: 12))
                                .foregroundColor(Color.gray90)
                            
                            if commentItem.isSecret {
                                Text("비밀댓글")
                                    .font(.custom(Font.medium.rawValue, size: 11))
                                    .foregroundColor(Color.grayee)
                                    .padding(.horizontal, 4)
                                    .padding(.vertical, 2)
                                    .background(Color.button.opacity(0.2))
                                    .cornerRadius(3.3)
                            }
                        }
                        
                        Text(commentItem.date)
                            .font(.custom(Font.medium.rawValue, size: 10.3))
                            .foregroundColor(Color.gray52)
                            .padding(.top, 4)
                    }
                    
                    Spacer()
                    
                    if isShowPopupMenuButton && (contentCreatorId == UserDefaults.int(forKey: .userId) || commentItem.writerId == UserDefaults.int(forKey: .userId)) {
                        Image("ic_seemore_vertical")
                            .onTapGesture { isShowPopupMenu.toggle() }
                    }
                }
                
                if commentItem.donationCan > 0 {
                    HStack(spacing: 3) {
                        Image("ic_can")
                            .resizable()
                            .frame(width: 13.3, height: 13.3)
                        
                        Text("\(commentItem.donationCan)")
                            .font(.custom(Font.bold.rawValue, size: 12))
                            .foregroundColor(.white)
                    }
                    .padding(.horizontal, 6.7)
                    .padding(.vertical, 2.7)
                    .background(
                        commentItem.donationCan >= 10000 ? Color(hex: "973a3a") :
                            commentItem.donationCan >= 5000 ? Color(hex: "d85e37") :
                            commentItem.donationCan >= 1000 ? Color(hex: "d38c38") :
                            commentItem.donationCan >= 500 ? Color(hex: "59548f") :
                            commentItem.donationCan >= 100 ? Color(hex: "4d6aa4") :
                            commentItem.donationCan >= 50 ? Color(hex: "2d7390") :
                            Color(hex: "548f7d")
                    )
                    .cornerRadius(10.7)
                    .padding(.leading, 46.7)
                    .padding(.bottom, 5)
                }
                
                HStack(spacing: 0) {
                    if isModeModify {
                        HStack(spacing: 0) {
                            TextField("댓글을 입력해 보세요.", text: $comment)
                                .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()
                                    if commentItem.comment != comment {
                                        modifyComment(commentItem.id, comment)
                                    }
                                    isModeModify = false
                                }
                        }
                        .background(Color.gray23)
                        .cornerRadius(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .strokeBorder(lineWidth: 1)
                                .foregroundColor(Color.button)
                        )
                    } else {
                        VStack(alignment: .leading, spacing: 13.3) {
                            Text(commentItem.comment)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color.grayee)
                                .fixedSize(horizontal: false, vertical: true)
                                .lineSpacing(8)
                                .padding(.top, commentItem.donationCan > 0 ? 0 : 13.3)
                            
                            if !isReplyComment {
                                NavigationLink(
                                    destination: AudioContentListReplyView(
                                        creatorId: contentCreatorId,
                                        audioContentId: audioContentId,
                                        parentComment: commentItem
                                    )
                                ) {
                                    Text(commentItem.replyCount > 0 ? "답글 \(commentItem.replyCount)개" : "답글 쓰기")
                                        .font(.custom(Font.medium.rawValue, size: 13.3))
                                        .foregroundColor(Color.button)
                                }
                            }
                        }
                    }
                    
                    Spacer()
                }
                .padding(.leading, 46.7)
                
                Rectangle()
                    .foregroundColor(Color.gray59)
                    .frame(height: 0.5)
                    .padding(.top, 16.7)
            }
            
            if isShowPopupMenu {
                VStack(spacing: 10) {                    
                    if commentItem.writerId == UserDefaults.int(forKey: .userId) {
                        Text("수정")
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color.gray77)
                            .onTapGesture {
                                isModeModify = true
                                isShowPopupMenu = false
                            }
                    }
                    
                    if contentCreatorId == UserDefaults.int(forKey: .userId) ||
                        commentItem.writerId == UserDefaults.int(forKey: .userId)
                    {
                        Text("삭제")
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color.gray77)
                            .onTapGesture {
                                onClickDelete(commentItem.id)
                                isShowPopupMenu = false
                            }
                    }
                }
                .padding(10)
                .background(Color.gray22)
            }
        }
        .onAppear { comment = commentItem.comment }
    }
}
