//
//  CreatorCommunityCommentItemView.swift
//  SodaLive
//
//  Created by klaus on 2023/12/20.
//

import SwiftUI
import Kingfisher

struct CreatorCommunityCommentItemView: View {
    let creatorId: Int
    let postId: Int
    let commentItem: GetCommunityPostCommentListItem
    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) {
                        Text(commentItem.nickname)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color.gray90)
                        
                        Text(commentItem.date)
                            .font(.custom(Font.medium.rawValue, size: 10.3))
                            .foregroundColor(Color.gray52)
                            .padding(.top, 4)
                    }
                    
                    Spacer()
                    
                    if isShowPopupMenuButton && (creatorId == UserDefaults.int(forKey: .userId) || commentItem.writerId == UserDefaults.int(forKey: .userId)) {
                        Image("ic_seemore_vertical")
                            .onTapGesture { isShowPopupMenu.toggle() }
                    }
                }
                
                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(hex: "232323"))
                        .cornerRadius(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .strokeBorder(lineWidth: 1)
                                .foregroundColor(Color(hex: "3bb9f1"))
                        )
                    } 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, 13.3)
                            
                            if !isReplyComment {
                                NavigationLink(
                                    destination: CreatorCommunityCommentReplyView(
                                        creatorId: creatorId,
                                        postId: postId,
                                        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(hex: "595959"))
                    .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(hex: "777777"))
                            .onTapGesture {
                                isModeModify = true
                                isShowPopupMenu = false
                            }
                    }
                    
                    if creatorId == UserDefaults.int(forKey: .userId) ||
                        commentItem.writerId == UserDefaults.int(forKey: .userId)
                    {
                        Text("삭제")
                            .font(.custom(Font.medium.rawValue, size: 14))
                            .foregroundColor(Color(hex: "777777"))
                            .onTapGesture {
                                onClickDelete(commentItem.id)
                                isShowPopupMenu = false
                            }
                    }
                }
                .padding(10)
                .background(Color(hex: "222222"))
            }
        }
        .onAppear { comment = commentItem.comment }
    }
}
