//
//  UIImage+Theme.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/20.
//

import Foundation

private var customColorImage: [String: UIImage] = [:]

extension UIImage {
    static func generateLinearGradientImage(
        colors: [UIColor],
        size: CGSize,
        location: [NSNumber] = [0, 1],
        startPoint: CGPoint = CGPoint(x: 0, y: 0),
        endPoint: CGPoint = CGPoint(x: 1, y: 0)
    ) -> UIImage? {
        guard colors.count >= 2, size.width > 0, size.height > 0 else {
            return nil
        }
        // 创建渐变图层
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRect(origin: .zero, size: size)
        gradientLayer.colors = colors.map { $0.cgColor } // 转换为 CGColor
        gradientLayer.locations = location
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        
        // 渲染图层为图片
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { _ in
            gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        }
    }
    
    static func image(for color: UIColor, size: CGFloat = 10, cornerRadius: CGFloat? = nil) -> UIImage {
        let key = "fill_\(color.hash)_\(size)_\(cornerRadius ?? 0)"
        if let cache = customColorImage[key] {
            return cache
        }
        let imageSize = CGSize(width: size, height: size)
        let renderer = UIGraphicsImageRenderer(size: imageSize)
        let newImage = renderer.image { context in
            let rect = CGRect(origin: .zero, size: imageSize)
            color.setFill()
            if let cornerRadius {
                let path = UIBezierPath(
                    roundedRect: rect,
                    cornerRadius: min(cornerRadius, size/2)
                )
                path.fill()
            } else {
                context.fill(CGRect(origin: .zero, size: imageSize))
            }
        }
        customColorImage[key] = newImage
        return newImage
    }
    
    static let primary_3: UIImage? = {
        let size = CGSize(width: 10, height: 10)
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { context in
            UIColor.primary_3.setFill()
            context.fill(CGRect(origin: .zero, size: size))
        }
    }()
    
    static let fill: UIImage? = {
        let size = CGSize(width: 10, height: 10)
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { context in
            UIColor.fill.setFill()
            context.fill(CGRect(origin: .zero, size: size))
        }
    }()
    
    static let primary_6: UIImage? = {
        var view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
        let layer0 = CAGradientLayer()
        layer0.colors = [
          UIColor(red: 0.719, green: 0.932, blue: 1, alpha: 1).cgColor,
          UIColor(red: 0.865, green: 1, blue: 0.719, alpha: 1).cgColor,
          UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        ]
        layer0.locations = [0, 0.36, 1]
        layer0.startPoint = CGPoint(x: 0.25, y: 0.5)
        layer0.endPoint = CGPoint(x: 0.75, y: 0.5)
        layer0.transform = CATransform3DMakeAffineTransform(CGAffineTransform(a: 0.75, b: -0.01, c: 0, d: 0.75, tx: 0, ty: 0.12))
        layer0.bounds = view.bounds.insetBy(dx: -0.5*view.bounds.size.width, dy: -0.5*view.bounds.size.height)
        layer0.position = view.center
        view.layer.addSublayer(layer0)
        
        let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
        return renderer.image { _ in
            view.layer.render(in: UIGraphicsGetCurrentContext()!)
        }
    }()
    
    static let primary_7: UIImage? = {
        var view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
        let layer0 = CAGradientLayer()
        layer0.colors = [
          UIColor(hex: "#4ED2FF").cgColor,
          UIColor(hex: "#95E789").cgColor
        ]
        layer0.locations = [0, 1]
        layer0.startPoint = CGPoint(x: 0.25, y: 0.5)
        layer0.endPoint = CGPoint(x: 0.75, y: 0.5)
        layer0.transform = CATransform3DMakeAffineTransform(CGAffineTransform(a: 1.37, b: 0, c: 0, d: 1.37, tx: 0, ty: -0.19))
        layer0.bounds = view.bounds
        layer0.position = view.center
        view.layer.addSublayer(layer0)
        
        let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
        return renderer.image { _ in
            view.layer.render(in: UIGraphicsGetCurrentContext()!)
        }
    }()
    
    static let primary_8: UIImage? = {
        var view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
        let layer0 = CAGradientLayer()
        layer0.colors = [
          UIColor(red: 0.183, green: 0.586, blue: 1, alpha: 1).cgColor,
          UIColor(red: 0.313, green: 1, blue: 0.847, alpha: 1).cgColor
        ]
        layer0.locations = [0.28, 1]
        layer0.startPoint = CGPoint(x: 0.25, y: 0.5)
        layer0.endPoint = CGPoint(x: 0.75, y: 0.5)
        layer0.transform = CATransform3DMakeAffineTransform(CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0.5))
        layer0.bounds = view.bounds.insetBy(dx: -0.5*view.bounds.size.width, dy: -0.5*view.bounds.size.height)
        layer0.position = view.center
        view.layer.addSublayer(layer0)
        
        let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
        return renderer.image { _ in
            view.layer.render(in: UIGraphicsGetCurrentContext()!)
        }
    }()
}
