//
//  HeartBeatAnimationView.swift

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

import UIKit
import Lottie

class HeartBeatAnimationView: UIView{
    var bottomBg : AutoRatioImageView!
    var topOverlay: UIImageView!
    var bottomOverlay: UIImageView!
    
    var lottieView: LottieAnimationView!
    
    var percent: CGFloat = 0{
        didSet{
            dlog("Heart animation percent:\(percent)")
            changeWavePositionBy(percent: percent)
        }
    }
    
    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() {
        bottomBg = {
            let v = AutoRatioImageView()
//            v.image = UIImage(named: "heart_wave_bg")
            v.setImage( UIImage(named: "heart_wave_bg"))
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
            }
            return v
        }()
        
        topOverlay = {
            let v = UIImageView()
            v.image = UIImage(named: "heart_wave_overlay_top")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(234/393.0)
            }
            return v
        }()
        
        bottomOverlay = {
            let v = UIImageView()
            v.image = UIImage(named: "heart_wave_overlay_bottom")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalTo(topOverlay.snp.bottom)
                make.leading.bottom.trailing.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(166/393.0)
            }
            return v
        }()
        
        lottieView = {
            let animation = LottieAnimation.named("heart_beat_wave_lite")
            let animationView = LottieAnimationView(animation: animation)
            
            
            animationView.contentMode = .scaleAspectFit
            animationView.loopMode = .loop
            animationView.backgroundBehavior = .pauseAndRestore
            animationView.backgroundColor = .clear
            animationView.size = CGSize(width: 120, height: 120)
            //addSubview(animationView)
            //insertSubview(animationView, at: 0)
            insertSubview(animationView, aboveSubview: bottomBg)
            animationView.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 120, height: 120))
                make.centerX.equalToSuperview()
                make.bottom.equalTo(topOverlay).offset(60)
            }
            return animationView
        }()
    }
    
    private func setupData(){
        lottieView.play()
        
        changeWavePositionBy(percent: 0)
    }
    
    private func setupEvent(){
        
    }
    
    private func changeWavePositionBy(percent:CGFloat){
        lottieView.snp.updateConstraints { make in
            // ⚠️配置100
            make.bottom.equalTo(topOverlay).offset(100 * (1-percent))
        }
    }
}
