//
//  TextButton.swift

//
//  Created by Leon on 2025/7/20.
//

import UIKit

/// Default: tll, color: cpvn
class TextButton: CLButton {
    
    var horizontalPadding: CGFloat = 0.0 { // Default text horizontal padding
        didSet {
            updateTitleEdgeInsets()
        }
    }
    var text:String? {
        didSet {
            setTitle(text, for: .normal)
        }
    }
    var titleColor:UIColor? {
        didSet {
            setTitleColor(titleColor, for: .normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        // Assuming .t.tll and .c.tpvn are defined font and color constants
        titleLabel?.font = .t.tll
        setTitleColor(.c.cpvn, for: .normal)
        setTitleColor(.c.ctd, for: .disabled)
        setTitleColor(.c.cpvp, for: .highlighted)
        
        // Ensure titleLabel uses auto layout
        titleLabel?.translatesAutoresizingMaskIntoConstraints = false
        
        // Basic styling
        backgroundColor = .clear // Optional: set as needed
        layer.cornerRadius = 8 // Optional: for rounded corners
        clipsToBounds = true
        
        // Apply initial padding
        updateTitleEdgeInsets()
    }

    private func updateTitleEdgeInsets() {
        // Adjust text padding
        contentEdgeInsets = UIEdgeInsets(top: 0, left: horizontalPadding, bottom: 0, right: horizontalPadding)
    }

//    override var intrinsicContentSize: CGSize {
//        // Calculate intrinsic size with custom height
//        let superSize = super.intrinsicContentSize
//        return CGSize(width: superSize.width + horizontalPadding * 2, height: buttonHeight)
//    }

    // Ensure layout updates when properties change
    override func layoutSubviews() {
        super.layoutSubviews()
        updateTitleEdgeInsets()
    }
}
