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

import SwiftUI
import Kingfisher

struct AudioContentCommentListView: View {
    
    @Binding var isPresented: Bool
    let audioContentId: Int
    
    @StateObject var viewModel = AudioContentCommentListViewModel()
    
    var body: some View {
        NavigationView {
            ZStack {
                VStack(spacing: 0) {
                    HStack(spacing: 0) {
                        Text("댓글")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(.white)
                            .padding(.leading, 13.3)
                        
                        Text("\(viewModel.totalCommentCount)")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color(hex: "909090"))
                            .padding(.leading, 6.7)
                        
                        Spacer()
                        
                        Image("ic_close_white")
                            .onTapGesture { isPresented = false}
                    }
                    .padding(.horizontal, 13.3)
                    .padding(.top, 12)
                    
                    Rectangle()
                        .foregroundColor(Color(hex: "595959"))
                        .frame(height: 0.5)
                        .padding(.top, 12)
                        .padding(.bottom, 13.3)
                        .padding(.horizontal, 13.3)
                    
                    HStack(spacing: 8) {
                        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: $viewModel.comment)
                                .autocapitalization(.none)
                                .disableAutocorrection(true)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color(hex: "eeeeee"))
                                .accentColor(Color(hex: "9970ff"))
                                .keyboardType(.default)
                                .padding(.horizontal, 13.3)
                            
                            Spacer()
                            
                            Image("btn_message_send")
                                .resizable()
                                .frame(width: 35, height: 35)
                                .padding(6.7)
                                .onTapGesture {
                                    hideKeyboard()
                                    viewModel.registerComment()
                                }
                        }
                        .background(Color(hex: "232323"))
                        .cornerRadius(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .strokeBorder(lineWidth: 1)
                                .foregroundColor(Color(hex: "9970ff"))
                        )
                        
                        Spacer()
                    }
                    .padding(.horizontal, 13.3)
                    
                    Rectangle()
                        .foregroundColor(Color(hex: "595959"))
                        .frame(height: 0.5)
                        .padding(.top, 12)
                        .padding(.bottom, 13.3)
                        .padding(.horizontal, 13.3)
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        LazyVStack(spacing: 13.3) {
                            ForEach(0..<viewModel.commentList.count, id: \.self) { index in
                                let comment = viewModel.commentList[index]
                                NavigationLink {
                                    AudioContentListReplyView(
                                        audioContentId: audioContentId,
                                        parentComment: comment
                                    )
                                } label: {
                                    AudioContentCommentItemView(comment: comment, isReplyComment: false)
                                    .padding(.horizontal, 26.7)
                                    .onAppear {
                                        if index == viewModel.commentList.count - 1 {
                                            viewModel.getCommentList()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                if viewModel.isLoading {
                    LoadingView()
                }
            }
            .onAppear {
                viewModel.audioContentId = audioContentId
                viewModel.getCommentList()
            }
            .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
                GeometryReader { geo in
                    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(hex: "9970ff"))
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.center)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
        }
    }
}