//
//  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) {
        // 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)
    }
}
