//
//  UIButton+Extension.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/23.
//

import Foundation
import UIKit

// MARK: - UIButton 图片 与 title 位置关系
extension UIButton {
    
    /// 图片 和 title 的布局样式
    enum ImageTitleLayout {
        case imgTop
        case imgBottom
        case imgLeft
        case imgRight
    }
    
    // MARK: 3.1、设置图片和 title 的位置关系(提示：title和image要在设置布局关系之前设置)
    /// 设置图片和 title 的位置关系(提示：title和image要在设置布局关系之前设置)
    /// - Parameters:
    ///   - layout: 布局
    ///   - spacing: 间距
    /// - Returns: 返回自身
    @discardableResult
    func setImageTitleLayout(_ layout: ImageTitleLayout, spacing: CGFloat = 0) -> UIButton {
        switch layout {
        case .imgLeft:
            alignHorizontal(spacing: spacing, imageFirst: true)
        case .imgRight:
            alignHorizontal(spacing: spacing, imageFirst: false)
        case .imgTop:
            alignVertical(spacing: spacing, imageTop: true)
        case .imgBottom:
            alignVertical(spacing: spacing, imageTop: false)
        }
        return self
    }
    
    /// 水平方向
    /// - Parameters:
    ///   - spacing: 间距
    ///   - imageFirst: 图片是否优先
    private func alignHorizontal(spacing: CGFloat, imageFirst: Bool) {
        let edgeOffset = spacing / 2
        imageEdgeInsets = UIEdgeInsets(top: 0, left: -edgeOffset,
                                            bottom: 0,right: edgeOffset)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: edgeOffset,
                                            bottom: 0, right: -edgeOffset)
        if !imageFirst {
            transform = CGAffineTransform(scaleX: -1, y: 1)
            imageView?.transform = CGAffineTransform(scaleX: -1, y: 1)
            titleLabel?.transform = CGAffineTransform(scaleX: -1, y: 1)
        }
        contentEdgeInsets = UIEdgeInsets(top: 0, left: edgeOffset, bottom: 0, right: edgeOffset)
    }
    
    /// 垂直方向
    /// - Parameters:
    ///   - spacing: 间距
    ///   - imageTop: 图片是不是在顶部
    private func alignVertical(spacing: CGFloat, imageTop: Bool) {
        
        guard let imageWidth = self.imageView?.kb_width,
              let imageHeight = self.imageView?.kb_height,
              let text = self.titleLabel?.text,
              let font = self.titleLabel?.font
        else {
            return
        }
        
        let labelString = NSString(string: text)
        let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: font])
        let titleHeight = titleSize.height
        let titleWidth = titleSize.width
        let insetAmount = spacing / 2
        
        if imageTop {
            
            imageEdgeInsets = UIEdgeInsets(top: -titleHeight - insetAmount,
                                           left: (self.kb_width - imageWidth) / 2,
                                           bottom: 0,
                                           right: (self.kb_width - imageWidth) / 2 - titleWidth)
            titleEdgeInsets = UIEdgeInsets(top: 0,
                                           left: -imageWidth,
                                           bottom: -imageHeight - insetAmount,
                                           right: 0)
        } else {
            
            imageEdgeInsets = UIEdgeInsets(top: 0,
                                           left: (self.kb_width - imageWidth) / 2,
                                           bottom: -titleHeight - insetAmount,
                                           right: (self.kb_width - imageWidth) / 2 - titleWidth)
            titleEdgeInsets = UIEdgeInsets(top: -imageHeight - insetAmount,
                                           left: -imageWidth,
                                           bottom: 0,
                                           right: 0)
        }
    }
    
}
