//
//  LLSpinner.swift
//  WOKA
//
//  Created by MacBook Pro on 06/05/24.
//

import UIKit

open class LLSpinner {
    
    // Spinner view
    internal static var spinnerView: UIActivityIndicatorView?
    // Background view behind spinner
    internal static var backgroundView: UIView?
    // Background view covering entire frame
    internal static var frameTintView: UIView?
    // Label to display text
    internal static var label: UILabel?
    
    // Default spinner style and background color
    public static var style: UIActivityIndicatorView.Style = .large
    public static var backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.3)
    
    // Touch handler closure
    internal static var touchHandler: (() -> Void)?
    
    // Function to show spinner
    public static func spin(style: UIActivityIndicatorView.Style = style,
                            backgroundColor: UIColor = backgroundColor,
                            text: String? = nil,
                            touchHandler: (() -> Void)? = nil) {
        if spinnerView == nil,
            let window = UIApplication.shared.mainKeyWindow {
            let frame = UIScreen.main.bounds
            
            // Add background view covering entire frame
            frameTintView = UIView(frame: frame)
            frameTintView?.center = CGPoint(x: window.frame.size.width  / 2,
                                            y: window.frame.size.height / 2)
            frameTintView?.backgroundColor = backgroundColor
            frameTintView?.layer.cornerRadius = 10
            window.addSubview(frameTintView!)
            
            // Add background view behind spinner
            backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 120))
            backgroundView?.center = CGPoint(x: window.frame.size.width  / 2,
                                              y: window.frame.size.height / 2 - 20) // Adjust vertical position
            backgroundView?.backgroundColor = .white
            backgroundView?.layer.cornerRadius = 10
            window.addSubview(backgroundView!)
            
            // Create and add spinner view
            spinnerView = UIActivityIndicatorView(style: style)
            spinnerView!.center = CGPoint(x: backgroundView!.center.x,
                                          y: backgroundView!.center.y - 10) // Move spinner up
            spinnerView!.color = UIColor.appColor(.TextDarkBlue)!
            window.addSubview(spinnerView!)
            spinnerView!.startAnimating()
            
            // Add label if text is provided
            if let text = text {
                label = UILabel(frame: CGRect(x: 0, y: spinnerView!.frame.maxY + 10, width: window.frame.size.width, height: 20))
                label?.font = FontCustom().customFont(fontName: .Exo2_Bold, size: 16)
                label?.textColor = UIColor.appColor(.TextDarkBlue)
                label?.text = text
                label?.textAlignment = .center
                window.addSubview(label!)
            }
        }
        
        // Attach touch handler if provided
        if touchHandler != nil {
            self.touchHandler = touchHandler
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(runTouchHandler))
            spinnerView!.addGestureRecognizer(tapGestureRecognizer)
        }
    }
    
    // Run touch handler
    @objc internal static func runTouchHandler() {
        if touchHandler != nil {
            touchHandler!()
        }
    }
    
    // Function to stop spinner
    public static func stop() {
        // Remove spinner view
        if let _ = spinnerView {
            spinnerView!.stopAnimating()
            spinnerView!.removeFromSuperview()
            spinnerView = nil
        }
        
        // Remove background view behind spinner
        if let _ = backgroundView {
            backgroundView!.removeFromSuperview()
            backgroundView = nil
        }
        
        // Remove label
        if let _ = label {
            label!.removeFromSuperview()
            label = nil
        }
        
        // Remove background view covering entire frame
        if let _ = frameTintView {
            frameTintView!.removeFromSuperview()
            frameTintView = nil
        }
    }
}
