//
//  GlobalLoadingOverlayView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 全局 Loading 遮罩，对齐 Android `LoadingScrim` 样式。
final class GlobalLoadingOverlayView: UIView {

    private let cardView = UIView()
    private let loadingView = LottieLoadingView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black.withAlphaComponent(0.2)
        isUserInteractionEnabled = true

        cardView.backgroundColor = .white
        cardView.layer.cornerRadius = 15
        cardView.clipsToBounds = true

        addSubview(cardView)
        cardView.addSubview(loadingView)

        cardView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(100)
        }
        loadingView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(80)
        }
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 显示并开始动画。
    func present() {
        isHidden = false
        loadingView.startAnimating()
    }

    /// 隐藏并停止动画。
    func dismiss() {
        loadingView.stopAnimating()
        isHidden = true
    }
}
