//
//  TSGenmojiTextView.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//

class TSCustomTextView: UITextView {
    
    // Placeholder Label
    private lazy var  placeholderLabel: TopLeftLabel = {
        let placeholderLabel = TopLeftLabel()
        placeholderLabel.font = font
        placeholderLabel.textColor = placeholderColor
        placeholderLabel.text = placeholder
        placeholderLabel.numberOfLines = 0
        placeholderLabel.isUserInteractionEnabled = false
        placeholderLabel.textAlignment = .left
        return placeholderLabel
    }()
    
    // Placeholder Text
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
        }
    }
    
    // Placeholder Color
    var placeholderColor: UIColor = .lightGray {
        didSet {
            placeholderLabel.textColor = placeholderColor
        }
    }
    
    // Text Insets
    var textInsets: UIEdgeInsets = .zero {
        didSet {
            updateTextContainerInset()
        }
    }
    
    // Override text property to manage placeholder visibility
    override var text: String! {
        didSet {
            updatePlaceholderVisibility()
        }
    }
    
    // Initializer
    init(placeholder: String? = nil,
         text: String? = nil,
         font: UIFont? = nil,
         textColor: UIColor = .black,
         backgroundColor: UIColor = .white,
         textInsets: UIEdgeInsets = .zero) {
        super.init(frame: .zero, textContainer: nil)
        
        self.placeholder = placeholder
        self.font = font
        self.textColor = textColor
        self.backgroundColor = backgroundColor
        self.textInsets = textInsets
        
        setupPlaceholder()
        self.text = text
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupPlaceholder()
    }
    
    // Setup placeholder label
    private func setupPlaceholder() {
 
        addSubview(placeholderLabel)
        updatePlaceholderVisibility()
        
        // Observe text changes to update placeholder
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(textDidChange),
                                               name: UITextView.textDidChangeNotification,
                                               object: self)
        
        // Update text container insets
        updateTextContainerInset()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // Update placeholder visibility
    private func updatePlaceholderVisibility() {
        placeholderLabel.isHidden = !text.isEmpty
    }
    
    @objc private func textDidChange() {
        updatePlaceholderVisibility()
    }
    
    // Update textContainerInset and placeholder frame
    private func updateTextContainerInset() {
        textContainerInset = textInsets
        setNeedsLayout()
    }
    
    // Layout placeholder
    override func layoutSubviews() {
        super.layoutSubviews()

        
        
        let placeholderX = textInsets.left + 5 // Add padding for placeholder
        let placeholderY = textInsets.top
        let placeholderWidth = bounds.width - textInsets.left - textInsets.right - 10 // Adjust for padding
        let placeholderHeight = bounds.height - textInsets.top - textInsets.bottom
        
        
//        if let font = font {
//            placeholderHeight = placeholder?.height(ofFont: font, maxWidth: k_ScreenWidth-64)
//        }
        
        placeholderLabel.frame = CGRect(x: placeholderX,
                                        y: placeholderY,
                                        width: placeholderWidth,
                                        height: placeholderHeight)
    }
}
