//
//  UIButton+Ex.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

extension UIButton {
    
    private struct AssociatedKeys {
        static var actionKey = "UIButtonActionKey"
    }

    // 存储回调闭包
    private var actionClosure: (() -> Void)? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? (() -> Void)
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.actionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    
    
    /// 快速创建 UIButton
    /// - Parameters:
    ///   - title: 按钮的标题
    ///   - image: 按钮的图标
    ///   - backgroundImage: 按钮的背景图
    ///   - backgroundColor: 按钮的背景色
    ///   - font: 按钮的字体
    ///   - titleColor: 按钮的字体颜色
    ///   - action: 按钮点击事件的回调
    static func createButton(title: String? = nil,
                             image: UIImage? = nil,
                             backgroundImage: UIImage? = nil,
                             backgroundColor: UIColor? = nil,
                             font: UIFont? = nil,
                             titleColor: UIColor? = nil,
                             corner:CGFloat = 0,
                             action: (() -> Void)? = nil) -> UIButton {
        let button = UIButton(type: .custom)
        button.setUpButton(title: title,
                           image: image,
                           backgroundImage: backgroundImage,
                           backgroundColor: backgroundColor,
                           font: font,
                           titleColor: titleColor,
                           corner:corner,
                           action: action)
        return button
    }

    func setUpButton(title: String? = nil,
                             image: UIImage? = nil,
                             backgroundImage: UIImage? = nil,
                             backgroundColor: UIColor? = nil,
                             font: UIFont? = nil,
                             titleColor: UIColor? = nil,
                             corner:CGFloat = 0,
                             action: (() -> Void)? = nil){
        let button = self
        button.showsTouchWhenHighlighted = false
        button.adjustsImageWhenHighlighted = false
        // 设置标题
        if let title = title {
            button.setTitle(title, for: .normal)
        }
        
        // 设置图片
        if let image = image {
            button.setImage(image, for: .normal)
        }
        
        // 设置背景图片
        if let backgroundImage = backgroundImage {
            button.setBackgroundImage(backgroundImage, for: .normal)
        }
        
        // 设置背景色
        if let backgroundColor = backgroundColor {
            button.backgroundColor = backgroundColor
        }
        
        // 设置字体
        if let font = font {
            button.titleLabel?.font = font
        }
        
        // 设置字体颜色
        if let titleColor = titleColor {
            button.setTitleColor(titleColor, for: .normal)
        }
        
        if corner > 0 {
            button.cornerRadius = corner
        }

        if let action = action {
            button.actionClosure = action
            button.addTarget(button, action: #selector(buttonTapped), for: .touchUpInside)
        }

    }
    
    // 按钮点击事件
    @objc private func buttonTapped() {
        actionClosure?()
    }
}


class UIExpandedTouchButton: UIButton {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let buttonBounds = self.bounds
        let widthDelta = self.width * 0.5 // 增加点击区域的比例，这里增加了50%
        let heightDelta = self.height * 0.5
        let expandedBounds = buttonBounds.insetBy(dx: -widthDelta, dy: -heightDelta)
        return expandedBounds.contains(point)
    }
}

