//
//  SessionHeartLevelNoticeView.swift

//
//  Created by Leon on 2025/8/25.
//

class SessionHeartLevelNoticeView: UIView{
    var label : CLLabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
        setupEvent()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        cornerRadius = 16
        snp.makeConstraints { make in
            make.height.equalTo(32)
        }
        label = {
            let v = CLLabel()
            v.font = .t.tls
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.trailing.equalToSuperview().offset(-16)
                make.centerY.equalToSuperview()
            }
            return v
        }()
        
    }
    
    private func setupData(){
        alpha = 0
        transform = .identity
    }
    
    private func setupEvent(){
        
    }
    
    // MARK: - Public
    func showUnlocked(string: String?, completion:(()->Void)? = nil){
        // 从下往上位移 + 淡入
        transform = CGAffineTransform(translationX: 0, y: 30)
        alpha = 0
        UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseOut]) {
            self.alpha = 1
            self.transform = .identity
        }
        backgroundColor = .c.cpn
        label.text = string//"Unlocked \"\(string ?? "-")\" title"
        
        // 隐藏
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {[weak self] in
            UIView.animate(withDuration: 0.5) {
                self?.alpha = 0
                completion?()
            }
        }
    }
    
    func showLose(string: String?, completion:(()->Void)? = nil){
        // 从上往下位移 + 淡入
        transform = CGAffineTransform(translationX: 0, y: -30)
        alpha = 0
        UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseOut]) {
            self.alpha = 1
            self.transform = .identity
        }
        backgroundColor = .c.cwn
        label.text = string//"Losed the title of \"\(string ?? "-")\""
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {[weak self] in
            UIView.animate(withDuration: 0.5) {
                self?.alpha = 0
                completion?()
            }
        }
    }

}
