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

import SwiftUI
import Kingfisher

struct ContentDetailCommentView: View {
    
    let commentCount: Int
    let commentList: [GetAudioContentCommentListItem]
    let isShowSecret: Bool
    
    let registerComment: (String, Bool) -> Void
    
    @State private var comment = ""
    @State private var isSecret = false
    
    var body: some View {
        VStack(alignment: .leading, spacing: 10.3) {
            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.gray90)
                
                Spacer()
                
                if isShowSecret && commentCount <= 0 {
                    HStack(spacing: 8) {
                        Image(isSecret ? "btn_square_select_checked" : "btn_square_select_normal")
                            .resizable()
                            .frame(width: 20, height: 20)
                        
                        Text("비밀댓글")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(isSecret ? Color.button : Color.grayee)
                    }
                    .onTapGesture {
                        isSecret.toggle()
                    }
                }
            }
            
            HStack(spacing: 8) {
                KFImage(
                    URL(
                        string: commentCount > 0 ?
                        commentList[0].profileUrl :
                            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())
                
                if commentCount > 0 {
                    Text(commentList[0].comment)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .foregroundColor(Color.graybb)
                        .lineLimit(1)
                        .lineSpacing(8)
                        .padding(.leading, 3)
                } else {
                    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()
                                registerComment(comment, isSecret)
                            }
                    }
                    .background(Color.gray23)
                    .cornerRadius(10)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .strokeBorder(lineWidth: 1)
                            .foregroundColor(Color.button)
                    )
                }
                
                Spacer()
            }
        }
    }
}
