//
//  PhotoGenerateStateOverlay.swift

//
//  Created by Leon on 2025/9/24.
//

import APNGKit
class PhotoGenerateStateOverlay: UIView {
    var loadingStackV: UIStackView!
    var loadingIndicator: APNGImageView!
    var loadingLabel: UILabel!

    var failedStackV: UIStackView!
    var failedIcon: UIImageView!
    var failedLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        backgroundColor = .c.cbd
        cornerRadius = 16

        setupLoadingViews()
        setupFailedViews()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        addDashedBorder(to: self, cornerRadius: 16)
    }

    private func setupLoadingViews() {
        loadingStackV = {
            let v = UIStackView()
            v.alignment = .center
            v.axis = .vertical
            v.spacing = 8
            addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
            return v
        }()

        loadingIndicator = {
            let v = APNGImageView()

            v.contentMode = .scaleAspectFit
            loadingStackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                // make.center.equalToSuperview()
                make.size.equalTo(CGSize(width: 96, height: 96))
            }

            if let url = Bundle.main.url(forResource: "generating", withExtension: "png") {
                let image = try? APNGImage(fileURL: url)
                v.image = image
            }
            v.stopAnimating()
            return v
        }()

        loadingLabel = {
            let v = UILabel()
            v.textColor = .white
            v.font = .t.tls
            v.textAlignment = .center
            loadingStackV.addArrangedSubview(v)
            v.text = "Generating..."
            return v
        }()

        loadingStackV.isHidden = true
    }

    private func setupFailedViews() {
        failedStackV = {
            let v = UIStackView()
            v.alignment = .center
            v.axis = .vertical
            v.spacing = 16
            addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
            return v
        }()

        failedIcon = {
            let v = UIImageView()
            v.image = UIImage(named: "status-error")
            failedStackV.addArrangedSubview(v)
            return v
        }()

        failedLabel = {
            let v = UILabel()
            v.textColor = .white
            v.font = .t.tls
            v.textAlignment = .center
            failedStackV.addArrangedSubview(v)
            v.text = "Generate Failed"
            return v
        }()

        failedStackV.isHidden = true
    }

    func showLoading() {
        isHidden = false
        loadingStackV.isHidden = false
        failedStackV.isHidden = true

        loadingIndicator.startAnimating()
    }

    func hideLoading() {
        loadingStackV.isHidden = true
        loadingIndicator.stopAnimating()
    }

    func showFailed() {
        isHidden = false
        hideLoading()

        loadingStackV.isHidden = true
        failedStackV.isHidden = false
    }
}
