//
//  CreatorCommunityCommentView.swift
//  SodaLive
//
//  Created by klaus on 2023/12/15.
//

import SwiftUI
import Kingfisher

struct CreatorCommunityCommentView: View {
    let commentCount: Int
    let commentItem: GetCommunityPostCommentListItem?
    
    let onClickWriteComment: (String) -> Void
    
    @State private var comment = ""
    
    var body: some View {
        VStack(alignment: .leading, spacing: 11) {
            HStack(spacing: 5.3) {
                Text("댓글")
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .foregroundColor(.white)
                
                Text("\(commentCount)")
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .foregroundColor(Color(hex: "909090"))
                
                Spacer()
            }
            
            HStack(spacing: 8) {
                if let comment = commentItem {
                    KFImage(URL(string: comment.profileUrl))
                        .cancelOnDisappear(true)
                        .downsampling(
                            size: CGSize(
                                width: 33.3,
                                height: 33.3
                            )
                        )
                    .resizable()
                    .frame(width: 33.3, height: 33.3)
                    .clipShape(Circle())
                
                    Text(comment.comment)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .foregroundColor(Color(hex: "bbbbbb"))
                        .lineLimit(1)
                        .padding(.leading, 3)
                } else {
                    KFImage(URL(string: UserDefaults.string(forKey: .profileImage)))
                        .cancelOnDisappear(true)
                        .downsampling(
                            size: CGSize(
                                width: 33.3,
                                height: 33.3
                            )
                        )
                    .resizable()
                    .frame(width: 33.3, height: 33.3)
                    .clipShape(Circle())
                    
                    HStack(spacing: 0) {
                        TextField("댓글을 입력해 보세요.", text: $comment)
                            .autocapitalization(.none)
                            .disableAutocorrection(true)
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "eeeeee"))
                            .accentColor(Color(hex: "3bb9f1"))
                            .keyboardType(.default)
                            .padding(.horizontal, 13.3)
                        
                        Spacer()
                        
                        Image("btn_message_send")
                            .resizable()
                            .frame(width: 35, height: 35)
                            .padding(6.7)
                            .onTapGesture {
                                hideKeyboard()
                                onClickWriteComment(comment)
                                comment = ""
                            }
                    }
                    .background(Color(hex: "232323"))
                    .cornerRadius(10)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .strokeBorder(lineWidth: 1)
                            .foregroundColor(Color(hex: "3bb9f1"))
                    )
                }
            }
        }
        .padding(11)
        .background(Color(hex: "ffffff").opacity(0.1))
        .cornerRadius(5.3)
    }
}

struct CreatorCommunityCommentView_Previews: PreviewProvider {
    static var previews: some View {
        CreatorCommunityCommentView(
            commentCount: 0,
            commentItem: GetCommunityPostCommentListItem(
                id: 1,
                writerId: 1,
                nickname: "닉네임",
                profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
                comment: "댓글 테스트",
                date: "1시간전",
                replyCount: 0
            ),
            onClickWriteComment: { _ in }
        )
    }
}
