//
//  LNCommonTextView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/23.
//

import Foundation
import UIKit
import SnapKit


class LNCommonTextView: UIView {
    var maxInput = 0 {
        didSet {
            textViewDidChange(textView)
            textLengthLabel.isHidden = maxInput == 0
        }
    }
    let placeholderLabel = UILabel()
    let textView = UITextView()
    var text: String {
        textView.text
    }
    var attrText: NSTextStorage {
        textView.textStorage
    }
    private let textLengthLabel = UILabel()
    
    weak var delegate: UITextViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func setText(_ text: String) {
        textView.text = text
        textViewDidChange(textView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNCommonTextView: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
        textLengthLabel.text = "\(textView.text.count)/\(maxInput)"
        
        delegate?.textViewDidChange?(textView)
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let currentText = textView.text ?? ""
        guard let swiftRange = Range(range, in: currentText) else {
            return true
        }
        let newText = currentText.replacingCharacters(in: swiftRange, with: text)
        
        if maxInput > 0, newText.count > maxInput {
            return false
        }
        
        return delegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
    }
}

extension LNCommonTextView {
    private func setupViews() {
        backgroundColor = .fill_1
        layer.cornerRadius = 12
        
        textLengthLabel.isHidden = true
        textLengthLabel.text = "0/\(maxInput)"
        textLengthLabel.font = .body_m
        textLengthLabel.textColor = .text_2
        addSubview(textLengthLabel)
        textLengthLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        placeholderLabel.text = .init(key: "A00006")
        placeholderLabel.font = .body_m
        placeholderLabel.textColor = .text_3
        placeholderLabel.numberOfLines = 0
        addSubview(placeholderLabel)
        placeholderLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalToSuperview().offset(12)
        }
        
        textView.font = .body_m
        textView.textColor = .text_5
        textView.backgroundColor = .clear
        textView.delegate = self
        textView.visibleView = self
        addSubview(textView)
        textView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12 - 4)
            make.top.equalToSuperview().offset(12 - 7)
            make.bottom.equalTo(textLengthLabel.snp.top)
        }
    }
}
