//
//  TSRingLoadingView.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/21.
//

import Kingfisher

class TSRingLoadingView: TSBaseView {
    
    lazy var animatedImageView: AnimatedImageView = {
        let animatedImageView = AnimatedImageView()
        animatedImageView.autoPlayAnimatedImage = false
        if let gifURL = Bundle.main.url(forResource: "ringAnimation", withExtension: "gif") {
            animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]) { result in
                switch result {
                case .success(let value):
                    print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
                case .failure(let error):
                    print("GIF 加载失败: \(error.localizedDescription)")
                }
            }
        }
        
        return animatedImageView
    }()
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(text:"loading...".localized, font: .font(size: 12),textColor: .fromHex("#FFFFFF").withAlphaComponent(0.8) ,textAlignment: .center)
        return textLabel
    }()
    
    
    private var targetView: UIView = UIView()
    
    override func creatUI() {
        contentView.backgroundColor = .black
        contentView.cornerRadius = 16
        contentView.snp.remakeConstraints { make in
            make.width.height.equalTo(100)
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(animatedImageView)
        animatedImageView.snp.makeConstraints { make in
            make.width.height.equalTo(60)
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
        }
  
        contentView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.height.equalTo(18.0)
            make.top.equalTo(animatedImageView.snp.bottom).offset(2)
            make.leading.trailing.equalToSuperview()
        }
        
    }
    
    
    override func dealThings() {
        // 监听应用生命周期事件
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleAppDidEnterBackground),
            name: UIApplication.didEnterBackgroundNotification,
            object: nil
        )
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleAppWillEnterForeground),
            name: UIApplication.willEnterForegroundNotification,
            object: nil
        )
    }
    
    
    lazy var isRotating = false{
        didSet{
            if isRotating == true{
                startRotating(view: animatedImageView)
            }else{
                stopRotating(view: animatedImageView)
            }
        }
    }
    
    
    /// 开始旋转
    func startRotating(view:UIView) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.animatedImageView.startAnimating()
        }
    }

    /// 停止旋转
    func stopRotating(view:UIView) {
        animatedImageView.stopAnimating()
    }
    
    func showLoading(text:String){
        animatedImageView.isHidden = false

        textLabel.text = text
        isRotating = true
    }
    
    func showError(text:String){
        animatedImageView.isHidden = true

        
        textLabel.text = text
        isRotating = false
    }
    
    @objc private func handleAppDidEnterBackground() {
        stopRotating(view: targetView)
    }

    @objc private func handleAppWillEnterForeground() {
        isRotating = isRotating
    }
}
