//
//  LNIMChatInputMenuView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/4.
//

import Foundation
import UIKit
import SnapKit
import AVFAudio


class LNIMChatInputMenuView: UIView {
    private let container = UIView()
    private let textInput = LNIMChatTextInputView()
    private let voiceInput = LNIMChatVoiceInputView()
    
    weak var viewModel: LNIMChatViewModel? {
        didSet {
            textInput.viewModel = viewModel
            voiceInput.viewModel = viewModel
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func hideInput() {
        textInput.hideInput()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNIMChatInputMenuView: LNIMChatTextInputViewDelegate {
    func onVoiceInputClick() {
        endEditing(true)
        
        LNPermissionHelper.requestMicrophoneAccess { [weak self] granted in
            guard let self else { return }
            guard granted else {
                showToast(.init(key: "B00022"))
                return
            }
            
            textInput.snp.remakeConstraints { make in
                make.horizontalEdges.equalToSuperview()
                make.top.equalTo(self.container.snp.bottom)
            }
            voiceInput.snp.remakeConstraints { make in
                make.edges.equalToSuperview()
            }
            
            voiceInput.startRecord()
            UIView.animate(withDuration: 0.1) { [weak self] in
                guard let self else { return }
                layoutIfNeeded()
            }
        }
    }
}

extension LNIMChatInputMenuView: LNIMChatVoiceInputViewDelegate {
    func onVoiceFinishInput() {
        voiceInput.snp.remakeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(container.snp.bottom)
        }
        textInput.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
        }
        UIView.animate(withDuration: 0.1) { [weak self] in
            guard let self else { return }
            layoutIfNeeded()
        }
    }
}

extension LNIMChatInputMenuView {
    private func setupViews() {
        backgroundColor = .fill
        
        container.clipsToBounds = true
        addSubview(container)
        container.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        textInput.delegate = self
        container.addSubview(textInput)
        textInput.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        voiceInput.delegate = self
        container.addSubview(voiceInput)
        voiceInput.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(container.snp.bottom)
        }
    }
}

#if DEBUG

import SwiftUI

struct LNIMChatInputMenuViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNIMChatInputMenuView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNIMChatInputMenuViewPreview()
})
#endif
