import UIKit
import SwiftUI

enum StatusIconRenderer {

    /// Renders a SwiftUI Shape into a UIImage (black fill, template-ready).
    static func renderShape<S: Shape>(_ shape: S, size: CGSize) -> UIImage {
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { ctx in
            let path = shape.path(in: CGRect(origin: .zero, size: size))
            ctx.cgContext.addPath(path.cgPath)
            ctx.cgContext.setFillColor(UIColor.black.cgColor)
            ctx.cgContext.fillPath()
        }
    }

    static func makeCheckImage(partial: Bool, color: UIColor, width: CGFloat = 11) -> UIImage? {
        let height = floor(width * 9.0 / 11.0)
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height))
        return renderer.image { ctx in
            let gc = ctx.cgContext
            gc.clear(CGRect(x: 0, y: 0, width: width, height: height))
            gc.scaleBy(x: width / 11.0, y: width / 11.0)
            gc.translateBy(x: 1.0, y: 1.0)
            gc.setStrokeColor(color.cgColor)
            gc.setLineWidth(0.99)
            gc.setLineCap(.round)
            gc.setLineJoin(.round)
            if partial {
                gc.move(to: CGPoint(x: 0.5, y: 7))
                gc.addLine(to: CGPoint(x: 7, y: 0))
            } else {
                gc.move(to: CGPoint(x: 0, y: 4))
                gc.addLine(to: CGPoint(x: 2.95157047, y: 6.95157047))
                gc.addCurve(
                    to: CGPoint(x: 3.04490857, y: 6.95157047),
                    control1: CGPoint(x: 2.97734507, y: 6.97734507),
                    control2: CGPoint(x: 3.01913396, y: 6.97734507)
                )
                gc.addCurve(
                    to: CGPoint(x: 3.04660389, y: 6.9498112),
                    control1: CGPoint(x: 3.04548448, y: 6.95099456),
                    control2: CGPoint(x: 3.04604969, y: 6.95040803)
                )
                gc.addLine(to: CGPoint(x: 9.5, y: 0))
            }
            gc.strokePath()
        }
    }

    static func makeClockFrameImage(color: UIColor) -> UIImage? {
        let size = CGSize(width: 11, height: 11)
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { ctx in
            let gc = ctx.cgContext
            gc.translateBy(x: 0, y: size.height)
            gc.scaleBy(x: 1, y: -1)
            gc.clear(CGRect(origin: .zero, size: size))
            gc.setStrokeColor(color.cgColor)
            gc.setFillColor(color.cgColor)
            gc.setLineWidth(1.0)
            gc.strokeEllipse(in: CGRect(x: 0.5, y: 0.5, width: 10, height: 10))
            gc.fill(CGRect(x: 5.0, y: 3.0, width: 1.0, height: 2.5))
        }
    }

    static func makeClockMinImage(color: UIColor) -> UIImage? {
        let size = CGSize(width: 11, height: 11)
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { ctx in
            let gc = ctx.cgContext
            gc.translateBy(x: 0, y: size.height)
            gc.scaleBy(x: 1, y: -1)
            gc.clear(CGRect(origin: .zero, size: size))
            gc.setFillColor(color.cgColor)
            gc.fill(CGRect(x: 5.0, y: 5.0, width: 4.5, height: 1.0))
        }
    }

    static func makeErrorIcon(color: UIColor, width: CGFloat = 20) -> UIImage? {
        let size = CGSize(width: width, height: width)
        let renderer = UIGraphicsImageRenderer(size: size)
        return renderer.image { ctx in
            let gc = ctx.cgContext
            gc.scaleBy(x: width / 11.0, y: width / 11.0)
            gc.setFillColor(color.cgColor)
            gc.fillEllipse(in: CGRect(x: 0.0, y: 0.0, width: 11.0, height: 11.0))
            gc.setFillColor(UIColor.white.cgColor)
            gc.fill(CGRect(x: 5.0, y: 2.5, width: 1.0, height: 4.25))
            gc.fillEllipse(in: CGRect(x: 4.75, y: 7.8, width: 1.5, height: 1.5))
        }
    }
}
