//
//  LNEditBioPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNEditBioPanel: LNPopupView {
    private let inputTextView = LNCommonTextView()
    
    private let confirmButton = UIButton()
    
    var handler: ((String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ bio: String) {
        inputTextView.setText(bio)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNEditBioPanel: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        if textView.text.isEmpty, confirmButton.isEnabled {
            confirmButton.isEnabled = false
            confirmButton.setBackgroundImage(nil, for: .normal)
        } else if confirmButton.backgroundImage(for: .normal) == nil {
            confirmButton.isEnabled = true
            confirmButton.setBackgroundImage(.primary_8, for: .normal)
        }
    }
}

extension LNEditBioPanel {
    private func setupViews() {
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00184")
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        inputTextView.maxInput = LNProfileManager.bioMaxInput
        container.addSubview(inputTextView)
        inputTextView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.height.equalTo(119)
        }
        
        confirmButton.setTitle(.init(key: "A00185"), for: .normal)
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.setBackgroundImage(.primary_8, for: .normal)
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.backgroundColor = .fill_4
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            endEditing(true)
            dismiss()
            handler?(inputTextView.text)
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(inputTextView.snp.bottom).offset(16)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
}

#if DEBUG

import SwiftUI

struct LNEditBioPanelPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNEditBioPanel()
        view.popup(container)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNEditBioPanelPreview()
})
#endif // DEBUG
