//
//  SpacedButton.swift
//  PhysicalWallPaper
//
//  Created by nkl on 2024/11/13.
//

import Foundation
import UIKit

class SpacedButton: UIButton {

    var spacing: CGFloat = 8.0 {
        didSet {
            updateInsets()
        }
    }

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

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        // 初始化配置
        updateInsets()
    }

    private func updateInsets() {
        guard let imageView = imageView, let titleLabel = titleLabel else {
            return
        }

        let imageSize = imageView.frame.size
        let titleSize = titleLabel.intrinsicContentSize

        imageEdgeInsets = UIEdgeInsets(
            top: 0,
            left: -spacing/2,
            bottom: 0,
            right: spacing/2
        )

        titleEdgeInsets = UIEdgeInsets(
            top: 0,
            left: spacing/2,
            bottom: 0,
            right: -spacing/2
        )

        contentEdgeInsets = UIEdgeInsets(
            top: 0,
            left: spacing/2,
            bottom: 0,
            right: spacing/2
        )
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateInsets()
    }
}
