//
//  SideMenuVC.swift
//  WOKA
//
//  Created by MacBook Pro on 21/05/24.
//

import UIKit

class SideMenuVC: UIViewController {

    @IBOutlet weak var logoutBtn: UIButton!
    @IBOutlet weak var selectionMenuTrailingConstraint: NSLayoutConstraint!
    @IBOutlet weak var languageControl: CustomizableSegmentControl!
    
    @IBOutlet weak var theme1: UIImageView!
    @IBOutlet weak var theme2: UIImageView!
    
    var vm = SideMenuVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        
    }

    @IBAction func closeBtnTapped(_ sender: UIButton) {
        self.sideMenuController?.hideMenu()
    }
}


class CustomizableSegmentControl: UISegmentedControl {
    
    private(set) lazy var radius:CGFloat = bounds.height / 2
    
    private let segmentInset: CGFloat = 5       //your inset amount
    private let segmentImage: UIImage? = UIImage(color: UIColor.white)    //your color
    
    override init(items: [Any]?) {
        super.init(items: items)
        selectedSegmentIndex = 0
    }
    
    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: coder)
    }
    
    
    override func layoutSubviews(){
        super.layoutSubviews()
        
        self.backgroundColor = #colorLiteral(red: 0.01960784314, green: 0, blue: 0.2196078431, alpha: 1)
        
        // selected option color
        self.setTitleTextAttributes([.foregroundColor: UIColor.black, .font : UIFont.systemFont(ofSize: 16, weight: .heavy)], for: .selected)

        // color of other options
        self.setTitleTextAttributes([.foregroundColor: UIColor.white, .font : UIFont.systemFont(ofSize: 16, weight: .heavy)], for: .normal)
        //background
        layer.cornerRadius = radius
        //foreground
        let foregroundIndex = numberOfSegments
        if subviews.indices.contains(foregroundIndex), let foregroundImageView = subviews[foregroundIndex] as? UIImageView{
            foregroundImageView.bounds = foregroundImageView.bounds.insetBy(dx: segmentInset, dy: segmentInset)
            foregroundImageView.image = segmentImage    //substitute with our own colored image
            foregroundImageView.layer.removeAnimation(forKey: "SelectionBounds")    //this removes the weird scaling animation!
            foregroundImageView.layer.masksToBounds = true
            foregroundImageView.layer.cornerRadius = foregroundImageView.bounds.height/2
        }
    }
}

extension UIImage{
    
    //creates a UIImage given a UIColor
    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage)
    }
}

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