//
//  CustomStepProgressBar.swift
//  MyTatvaCare
//
//  Created by Nirav Ramani on 15/05/25.
//
import UIKit

@IBDesignable
class CustomStepProgressBar: UIView {
    
    @IBInspectable var totalSteps: Int = 5 {
        didSet { setupSteps() }
    }
    
    @IBInspectable var currentIndex: Int = 0 {
        didSet { updateHighlight() }
    }
    
    @IBInspectable var currentTitle: String = "Provide Permission" {
        didSet { setupSteps() }
    }
    
    @IBInspectable var nextTitle: String = "Connect Transmitter" {
        didSet { setupSteps() }
    }
    
    // Editable property for step colors
    @IBInspectable var stepColor1: UIColor = UIColor.systemBlue {
        didSet { setupSteps() }
    }
    
    @IBInspectable var stepColor2: UIColor = UIColor.systemGreen {
        didSet { setupSteps() }
    }
    
    @IBInspectable var stepColor3: UIColor = UIColor.systemOrange {
        didSet { setupSteps() }
    }
    
    @IBInspectable var stepColor4: UIColor = UIColor.systemPurple {
        didSet { setupSteps() }
    }
    
    @IBInspectable var stepColor5: UIColor = UIColor.systemRed {
        didSet { setupSteps() }
    }
    
    // Array to store custom colors
    private var stepColors: [UIColor] {
        return [stepColor1, stepColor2, stepColor3, stepColor4, stepColor5]
    }
    
    private var stepBars: [UIView] = []
    private var labels: [UILabel] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSteps()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupSteps()
    }
    
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setupSteps()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupSteps()
    }
    
    private func setupSteps() {
        subviews.forEach { $0.removeFromSuperview() }
        stepBars.removeAll()
        labels.removeAll()
        
        guard totalSteps > 0 else { return }
        
        let horizontalStack = UIStackView()
        horizontalStack.axis = .horizontal
        horizontalStack.alignment = .fill
        horizontalStack.spacing = 8
        horizontalStack.distribution = .fill
        horizontalStack.translatesAutoresizingMaskIntoConstraints = false
        
        for i in 0..<totalSteps {
            let isActive = i <= currentIndex
            let isCurrent = i == currentIndex
            let isNext = i == currentIndex + 1
            
            let stepColor = stepColors[i % stepColors.count]
            
            let bar = UIView()
            bar.backgroundColor = stepColor//isActive ? stepColor : UIColor.lightGray
            bar.layer.cornerRadius = 2
            bar.translatesAutoresizingMaskIntoConstraints = false
            
            let barContainer = UIView()
            barContainer.translatesAutoresizingMaskIntoConstraints = false
            barContainer.heightAnchor.constraint(equalToConstant: 4).isActive = true  // ✅ Needed!
            if (isCurrent || isNext) {
                barContainer.widthAnchor.constraint(equalToConstant: 80).isActive = false  // ✅ Needed!
            } else {
                barContainer.widthAnchor.constraint(equalToConstant: 28).isActive = true  // ✅ Needed!
            }
            barContainer.addSubview(bar)
            
            // Full width inside container
            NSLayoutConstraint.activate([
                bar.leadingAnchor.constraint(equalTo: barContainer.leadingAnchor),
                bar.trailingAnchor.constraint(equalTo: barContainer.trailingAnchor),
                bar.centerYAnchor.constraint(equalTo: barContainer.centerYAnchor),
                bar.heightAnchor.constraint(equalToConstant: 4),  // ✅ Proper height
                bar.widthAnchor.constraint(equalToConstant: 120)
            ])
            
            let verticalStack = UIStackView()
            verticalStack.axis = .vertical
            verticalStack.alignment = .fill
            verticalStack.spacing = 4
            verticalStack.translatesAutoresizingMaskIntoConstraints = false
            verticalStack.addArrangedSubview(barContainer)
            verticalStack.heightAnchor.constraint(equalToConstant: 100).isActive = isCurrent
            
            let label = UILabel()
            label.text = isCurrent ? currentTitle : nextTitle
            label.font = isCurrent ? UIFont.boldSystemFont(ofSize: 12) : UIFont.systemFont(ofSize: 12)
            label.textColor = stepColor//isCurrent ? stepColor : UIColor.systemGray3
            label.textAlignment = .center
            label.numberOfLines = 1
            label.adjustsFontSizeToFitWidth = true
            label.minimumScaleFactor = 0.5
            
            if !(isCurrent || isNext) {
                label.textColor = .clear
            }
            labels.append(label)
            verticalStack.addArrangedSubview(label)
            stepBars.append(bar)
            horizontalStack.addArrangedSubview(verticalStack)
        }
        
        addSubview(horizontalStack)
        NSLayoutConstraint.activate([
            horizontalStack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            horizontalStack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
            horizontalStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
            horizontalStack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -15)
        ])
    }
    
    private func updateHighlight() {
        for (i, bar) in stepBars.enumerated() {
            bar.backgroundColor = i <= currentIndex ? UIColor.systemBlue : UIColor.systemGray4
        }
        
        for (i, label) in labels.enumerated() {
            label.font = i == 0 ? UIFont.boldSystemFont(ofSize: 12) : UIFont.systemFont(ofSize: 12)
            label.textColor = i == 0 ? UIColor.systemBlue : UIColor.systemGray3
        }
    }
}
