//
//  ConfettiView.swift
//  lichunWebsocket
//
//  Confetti animation overlay for celebrations
//

import SwiftUI
import UIKit

// MARK: - Helper Function
func createConfettiImage(color: UIColor, size: CGSize = CGSize(width: 10, height: 10)) -> UIImage? {
    let renderer = UIGraphicsImageRenderer(size: size)

    return renderer.image { ctx in
        color.setFill()
        ctx.cgContext.addEllipse(in: CGRect(origin: .zero, size: size))
        ctx.cgContext.drawPath(using: .fill)
    }
}

// MARK: - Confetti View
struct ConfettiView: UIViewRepresentable {
    func makeUIView(context: UIViewRepresentableContext<ConfettiView>) -> UIView {
        let view = UIView(frame: .zero)

        let emitter = CAEmitterLayer()
        emitter.emitterShape = .line
        emitter.emitterPosition = CGPoint(x: view.bounds.midX, y: 0)
        emitter.emitterSize = CGSize(width: view.bounds.width, height: 1)

        // Cozy warm colors for confetti
        let cozyColors: [UIColor] = [
            UIColor(red: 1.0, green: 0.71, blue: 0.82, alpha: 1.0),  // Soft pink #FFB4D2
            UIColor(red: 1.0, green: 0.83, blue: 0.67, alpha: 1.0),  // Soft orange #FFD4AA
            UIColor(red: 1.0, green: 0.94, blue: 0.67, alpha: 1.0),  // Soft yellow #FFF0AA
            UIColor(red: 0.77, green: 0.71, blue: 0.99, alpha: 1.0), // Soft purple #C4B5FD
            UIColor(red: 1.0, green: 0.61, blue: 0.74, alpha: 1.0),  // Pink #FF9BBD
            UIColor(red: 1.0, green: 0.63, blue: 0.77, alpha: 1.0),  // Rose #FFA0C4
        ]

        var cells: [CAEmitterCell] = []

        // Create multiple confetti cells with different colors and shapes
        for color in cozyColors {
            // Circle confetti
            let circleCell = CAEmitterCell()
            circleCell.birthRate = 8
            circleCell.lifetime = 12.0
            circleCell.lifetimeRange = 3.0
            circleCell.velocity = 120
            circleCell.velocityRange = 60
            circleCell.emissionRange = CGFloat(Double.pi)
            circleCell.spin = 3.0
            circleCell.spinRange = 4.0
            circleCell.scale = 0.15
            circleCell.scaleRange = 0.1
            circleCell.alphaSpeed = -0.15
            circleCell.contents = createConfettiImage(color: color, size: CGSize(width: 10, height: 10))?.cgImage
            cells.append(circleCell)

            // Rectangle confetti
            let rectCell = CAEmitterCell()
            rectCell.birthRate = 6
            rectCell.lifetime = 14.0
            rectCell.lifetimeRange = 4.0
            rectCell.velocity = 100
            rectCell.velocityRange = 50
            rectCell.emissionRange = CGFloat(Double.pi)
            rectCell.spin = 2.5
            rectCell.spinRange = 3.5
            rectCell.scale = 0.12
            rectCell.scaleRange = 0.08
            rectCell.alphaSpeed = -0.12
            rectCell.contents = createRectConfetti(color: color, size: CGSize(width: 8, height: 12))?.cgImage
            cells.append(rectCell)
        }

        // Add special heart confetti
        let heartCell = CAEmitterCell()
        heartCell.birthRate = 4
        heartCell.lifetime = 15.0
        heartCell.lifetimeRange = 3.0
        heartCell.velocity = 90
        heartCell.velocityRange = 40
        heartCell.emissionRange = CGFloat(Double.pi)
        heartCell.spin = 2.0
        heartCell.spinRange = 3.0
        heartCell.scale = 0.2
        heartCell.scaleRange = 0.1
        heartCell.alphaSpeed = -0.1
        heartCell.contents = createHeartConfetti(color: cozyColors[0], size: CGSize(width: 12, height: 12))?.cgImage
        cells.append(heartCell)

        emitter.emitterCells = cells
        view.layer.addSublayer(emitter)

        return view
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<ConfettiView>) {
    }
}

// MARK: - Additional Confetti Shapes
func createRectConfetti(color: UIColor, size: CGSize = CGSize(width: 8, height: 12)) -> UIImage? {
    let renderer = UIGraphicsImageRenderer(size: size)
    return renderer.image { ctx in
        color.setFill()
        let rect = CGRect(origin: .zero, size: size)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: 2)
        path.fill()
    }
}

func createHeartConfetti(color: UIColor, size: CGSize = CGSize(width: 12, height: 12)) -> UIImage? {
    let renderer = UIGraphicsImageRenderer(size: size)
    return renderer.image { ctx in
        color.setFill()

        // Simple heart shape using bezier path
        let path = UIBezierPath()
        let width = size.width
        let height = size.height

        // Start at bottom point
        path.move(to: CGPoint(x: width / 2, y: height))

        // Left side of heart
        path.addCurve(
            to: CGPoint(x: 0, y: height / 3),
            controlPoint1: CGPoint(x: width / 2, y: height * 0.75),
            controlPoint2: CGPoint(x: 0, y: height * 0.55)
        )

        // Left top arc
        path.addArc(
            withCenter: CGPoint(x: width / 4, y: height / 4),
            radius: width / 4,
            startAngle: .pi,
            endAngle: 0,
            clockwise: true
        )

        // Right top arc
        path.addArc(
            withCenter: CGPoint(x: width * 3 / 4, y: height / 4),
            radius: width / 4,
            startAngle: .pi,
            endAngle: 0,
            clockwise: true
        )

        // Right side of heart
        path.addCurve(
            to: CGPoint(x: width / 2, y: height),
            controlPoint1: CGPoint(x: width, y: height * 0.55),
            controlPoint2: CGPoint(x: width / 2, y: height * 0.75)
        )

        path.close()
        path.fill()
    }
}
