//
//  LNJoinUsHeaderView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/19.
//

import Foundation
import UIKit
import SnapKit


class LNJoinUsHeaderView: UIView {
    private let container = UIView()
    private let step1View = LNJoinUsStepItemView()
    private let step2View = LNJoinUsStepItemView()
    private let step3View = LNJoinUsStepItemView()
    private let progressLine = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ step: Int) {
        container.isHidden = false
        step1View.isFinished = step >= 1
        step2View.isFinished = step >= 2
        step3View.isFinished = step >= 3
        
        progressLine.snp.remakeConstraints { make in
            make.leading.equalTo(step1View.stepCircle.snp.centerX)
            make.centerY.equalTo(step1View.stepCircle.snp.centerY)
            make.height.equalTo(1)
            if step >= 3 {
                make.trailing.equalTo(step3View.stepCircle.snp.centerX)
            } else if step >= 2 {
                make.trailing.equalTo(step2View.stepCircle.snp.centerX)
            } else {
                make.trailing.equalTo(step1View.stepCircle.snp.centerX)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNJoinUsHeaderView {
    private func setupViews() {
        let image = UIImage.icJoinUsHeaderBg
        let bg = UIImageView()
        bg.backgroundColor = .primary_1
        bg.image = image
        addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(bg.snp.width).multipliedBy(image.size.height / image.size.width)
        }
        
        container.isHidden = true
        container.backgroundColor = .fill.withAlphaComponent(0.5)
        container.layer.cornerRadius = 36.5
        addSubview(container)
        container.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-66)
            make.height.equalTo(73)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .top
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview().inset(9)
        }
        
        let totalLine = UIView()
        totalLine.backgroundColor = .fill_4
        stackView.addSubview(totalLine)
        
        progressLine.backgroundColor = .primary_3
        stackView.addSubview(progressLine)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 0.9
        paragraphStyle.alignment = .center
        
        step1View.stepLabel.text = "1"
        step1View.textLabel.attributedText = .init(string: .init(key: "B00032"), attributes: [
            .paragraphStyle: paragraphStyle,
        ])
        stackView.addArrangedSubview(step1View)
        
        step2View.stepLabel.text = "2"
        step2View.textLabel.attributedText = .init(string: .init(key: "B00033"), attributes: [
            .paragraphStyle: paragraphStyle,
        ])
        stackView.addArrangedSubview(step2View)
        
        step3View.stepLabel.text = "3"
        step3View.textLabel.attributedText = .init(string: .init(key: "B00034"), attributes: [
            .paragraphStyle: paragraphStyle,
        ])
        stackView.addArrangedSubview(step3View)
        
        totalLine.snp.makeConstraints { make in
            make.leading.equalTo(step1View.stepCircle.snp.centerX)
            make.top.equalTo(step1View.stepCircle.snp.centerY)
            make.trailing.equalTo(step3View.stepCircle.snp.centerX)
            make.height.equalTo(1)
        }
        
        progressLine.snp.makeConstraints { make in
            make.leading.equalTo(totalLine)
            make.centerY.equalTo(totalLine)
            make.width.equalTo(0)
        }
    }
}

private class LNJoinUsStepItemView: UIView {
    let stepCircle = UIView()
    let stepLabel = UILabel()
    let textLabel = UILabel()
    
    var isFinished: Bool = false {
        didSet {
            stepCircle.backgroundColor = isFinished ? .primary_3 : .fill_4
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        snp.makeConstraints { make in
            make.width.equalTo(72)
        }
        
        stepCircle.layer.cornerRadius = 10.5
        addSubview(stepCircle)
        stepCircle.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalTo(21)
        }
        
        stepLabel.font = .heading_h3
        stepLabel.textColor = .text_1
        addSubview(stepLabel)
        stepLabel.snp.makeConstraints { make in
            make.centerX.equalTo(stepCircle)
            make.centerY.equalTo(stepCircle).offset(0.5)
        }
        
        textLabel.font = .body_s
        textLabel.textColor = .text_4
        textLabel.textAlignment = .center
        textLabel.numberOfLines = 2
        addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(stepCircle.snp.bottom).offset(6)
            make.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
