//
//  ApplyGradrient.swift
//  WOKA
//
//  Created by MacBook Pro on 25/04/24.
//

import UIKit

extension UIView {
    
    /// Apply gradient colors to the view.
    ///
    /// - Parameters:
    ///   - colors: An array of UIColor objects defining the color of each gradient stop.
    ///   - startPoint: The start point of the gradient, defined in the unit coordinate space. The start point corresponds to the top-left corner of the layer's bounds rectangle.
    ///   - endPoint: The end point of the gradient, defined in the unit coordinate space. The end point corresponds to the bottom-right corner of the layer's bounds rectangle.
    ///
    func applyGradient(colors: [UIColor], startPoint: CGPoint, endPoint: CGPoint) {
        
        // Check if the view already has a gradient layer
        if let sublayers = layer.sublayers {
            for sublayer in sublayers {
                if let gradientLayer = sublayer as? CAGradientLayer {
                    // Update the existing gradient layer
                    gradientLayer.colors = colors.map { $0.cgColor }
                    gradientLayer.startPoint = startPoint
                    gradientLayer.endPoint = endPoint
                    gradientLayer.frame = bounds
                    return
                }
            }
        }
        
        // Create a new CAGradientLayer instance
        let gradientLayer = CAGradientLayer()
        
        // Set the frame of the gradient layer to match the bounds of the view
        gradientLayer.frame = bounds
        
        // Convert the array of UIColor objects to an array of CGColor objects
        gradientLayer.colors = colors.map { $0.cgColor }
        
        // Set the start and end points of the gradient
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        
        // Insert the gradient layer as the bottom layer of the view's layer hierarchy
        layer.insertSublayer(gradientLayer, at: 0)
    }
    
    func applyMultiGradient(colors: [UIColor], startPoint: CGPoint, endPoint: CGPoint) {
        // Check if the view already has a gradient layer
        if let sublayers = layer.sublayers {
            for sublayer in sublayers {
                if let gradientLayer = sublayer as? CAGradientLayer {
                    // Update the existing gradient layer
                    gradientLayer.colors = colors.map { $0.cgColor }
                    gradientLayer.startPoint = startPoint
                    gradientLayer.endPoint = endPoint
                    gradientLayer.frame = bounds
                    return
                }
            }
        }
        
        // Create a new CAGradientLayer instance
        let gradientLayer = CAGradientLayer()
        
        // Set the frame of the gradient layer to match the bounds of the view
        gradientLayer.frame = bounds
        
        // Ensure that there are at least two colors
        guard colors.count >= 2 else {
            fatalError("At least two colors are required for the gradient.")
        }
        
        // Calculate the color stops
        let colorStops = (0..<colors.count).map { CGFloat($0) / CGFloat(colors.count - 1) }
        
        // Convert UIColors to CGColors
        let cgColors = colors.map { $0.cgColor }
        
        // Set the colors and color stops for the gradient layer
        gradientLayer.colors = cgColors
        gradientLayer.locations = colorStops as [NSNumber]?
        
        // Set the start and end points of the gradient
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        
        // Insert the gradient layer as the bottom layer of the view's layer hierarchy
        layer.insertSublayer(gradientLayer, at: 0)
    }


}
