//
//  UIImage+Ex.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/23.
//


extension UIImage {
    
    var width: CGFloat { size.width }
    var height: CGFloat { size.height }
    
    func normalizedImage() -> UIImage? {
        if imageOrientation == .up {
            return self
        }
        return repaintImage()
    }
    func repaintImage() -> UIImage? {
        let format = UIGraphicsImageRendererFormat()
        format.opaque = false
        format.scale = scale
        let renderer = UIGraphicsImageRenderer(size: size, format: format)
        let image = renderer.image { context in
            draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        }
        return image
    }

    func scaleToFillSize(size: CGSize, mode: UIView.ContentMode = .scaleToFill, scale: CGFloat = 0) -> UIImage? {
        if __CGSizeEqualToSize(self.size, size) {
            return self
        }
        let rect: CGRect
        let rendererSize: CGSize
        if mode == .scaleToFill {
            let isEqualRatio = size.width / size.height == width / height
            if isEqualRatio {
                rendererSize = size
                rect = CGRect(origin: .zero, size: size)
            }else {
                let scale = size.width / width
                var scaleHeight = scale * height
                var scaleWidth = size.width
                if scaleHeight < size.height {
                    scaleWidth = size.height / scaleHeight * size.width
                    scaleHeight = size.height
                }
                rendererSize = .init(width: scaleWidth, height: scaleHeight)
                rect = .init(origin: .zero, size: rendererSize)
            }
        }else {
            rendererSize = size
            if mode == .scaleAspectFit {
                rect = CGRect(origin: .zero, size: size)
            }else {
                var x: CGFloat = 0
                var y: CGFloat = 0
                let scale = size.width / width
                var scaleWidth = size.width
                var scaleHeight = scale * height
                if scaleHeight < size.height {
                    scaleWidth = size.height / scaleHeight * scaleWidth
                    scaleHeight = size.height
                }
                if scaleWidth < size.width {
                    scaleHeight = size.width / scaleWidth * scaleHeight
                    scaleWidth = size.width
                }
                x = -(scaleWidth - size.width) / 2
                y = -(scaleHeight - size.height) / 2
                rect = CGRect(
                    x: x,
                    y: y,
                    width: scaleWidth,
                    height: scaleHeight
                )
            }
        }
        let format = UIGraphicsImageRendererFormat()
        format.opaque = false
        format.scale = scale == 0 ? self.scale : scale
        let renderer = UIGraphicsImageRenderer(size: rendererSize, format: format)
        let image = renderer.image { context in
            draw(in: rect)
        }
        return image
    }
}

extension UIImage {
    /// 将颜色转换为 UIImage
    /// - Parameters:
    ///   - color: 目标颜色
    ///   - size: 图片尺寸（默认 1x1）
    /// - Returns: 转换后的 UIImage
    static func colorFrom(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
        // 创建一个矩形
        let rect = CGRect(origin: .zero, size: size)
        
        // 开始图形上下文
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        
        // 设置颜色填充
        color.setFill()
        UIRectFill(rect)
        
        // 获取生成的图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        // 结束图形上下文
        UIGraphicsEndImageContext()
        
        return image
    }
}
