//
//  YHStepView.swift
//  galaxy
//
//  Created by EDY on 2024/1/25.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHStepView: UIScrollView {
    
    static let height = 104.0
    typealias Block = (Int) -> ()
    var block: Block?
    var dataSource: [String]? {
        didSet {
            setUpView(dataSource ?? [])
        }
    }
    var currentIndex: Int = 0 {
        didSet {
            updateView(dataSource ?? [])
        }
    }
    var stepSpace: CGFloat = 6
    var lineWidth: CGFloat {
        guard let dataSource = dataSource else { return 0 }
        if dataSource.count <= 2 {
            return 157
        } else if dataSource.count <= 4 {
            return 75
        } else {
            return 55
        }
    }
    var spaceWidth: CGFloat {
        guard let dataSource = dataSource else { return 0 }
        if dataSource.count <= 2 {
            return 89
        } else if dataSource.count <= 4 {
            return 43
        } else {
            return 43
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView(_ array: [String]) {
        for i in 0 ..< array.count {
            let button = UIButton(type: .custom)
            button.setBackgroundImage(UIImage(named: "service_step_fail"), for: .normal)
            button.tag = 1000 + i
            button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
            addSubview(button)
            button.snp.makeConstraints { make in
                make.top.equalTo(28)
                make.left.equalTo(spaceWidth + i.cgFloat * (lineWidth + stepSpace * 2 + 14))
                make.width.height.equalTo(14)
            }
            let label = UILabel()
            label.text = array[i]
            label.font = kFont(size: 13)
            label.textAlignment = .center
            label.tag = 2000 + i
            label.textColor = UIColor.init(hex: 0x222222)
            addSubview(label)
            label.snp.makeConstraints { make in
                make.centerX.equalTo(button.snp.centerX)
                make.top.equalTo(button.snp.bottom).offset(14)
                make.height.equalTo(20)
            }
            
            if i < array.count - 1 {
                let line = UIImageView()
                line.image = UIImage(named: "service_progress_success")
                line.tag = 3000 + i
                addSubview(line)
                line.snp.makeConstraints { make in
                    make.centerY.equalTo(button.snp.centerY)
                    make.left.equalTo(button.snp.right).offset(stepSpace)
                    make.height.equalTo(3)
                    make.width.equalTo(lineWidth)
                }
                if i < currentIndex {
                    line.image = UIImage(named: "service_progress_success")
                } else {
                    line.image = UIImage(named: "service_progress_fail")
                }
            }
            
            if currentIndex == i {
                button.setBackgroundImage(UIImage(named: "service_step_now"), for: .normal)
                label.textColor = UIColor.init(hex: 0x4388ff)
            } else if i < currentIndex {
                button.setBackgroundImage(UIImage(named: "service_step_success"), for: .normal)
                label.textColor = UIColor.init(hex: 0x222222)
            } else {
                button.setBackgroundImage(UIImage(named: "service_step_fail"), for: .normal)
                label.textColor = UIColor.init(hex: 0xc0c0c0)
            }
        }
    }
    
    func updateView(_ array: [String]) {
        for i in 0 ..< array.count {
            let button = viewWithTag(1000 + i) as? UIButton
            let lable = viewWithTag(2000 + i) as? UILabel
            if i < array.count - 1 {
                let line = viewWithTag(3000 + i) as? UIImageView
                if i < currentIndex {
                    line?.image = UIImage(named: "service_progress_success")
                } else {
                    line?.image = UIImage(named: "service_progress_fail")
                }
            }
            if currentIndex == i {
                button?.setBackgroundImage(UIImage(named: "service_step_now"), for: .normal)
                lable?.textColor = UIColor.init(hex: 0x4388ff)
            } else if i < currentIndex {
                button?.setBackgroundImage(UIImage(named: "service_step_success"), for: .normal)
                lable?.textColor = UIColor.init(hex: 0x222222)
            } else {
                button?.setBackgroundImage(UIImage(named: "service_step_fail"), for: .normal)
                lable?.textColor = UIColor.init(hex: 0xc0c0c0)
            }
        }
    }
    
    @objc func buttonClick(_ button: UIButton) {
        let tag = button.tag
        if tag - 1000 < currentIndex {
            currentIndex = tag - 1000
            if let block = block {
                block(currentIndex)
            }
        }
    }
}
