//
//  PhoneVoiceWavingView.swift

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

import UIKit
import Lottie
class PhontVoiceWavingView: UIView{
    var waveWavingAnimationView: LottieAnimationView!
    var waveSilentView: UIImageView!
    
    var isAnimationPlaying : Bool{
        return waveWavingAnimationView.isAnimationPlaying
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        
        waveWavingAnimationView = {
            let animation = LottieAnimation.named("voice_im_recording")
            let animationView = LottieAnimationView(animation: animation)

            animationView.contentMode = .scaleAspectFit
            animationView.loopMode = .loop
            animationView.backgroundBehavior = .pauseAndRestore
            animationView.backgroundColor = .clear
            animationView.size = CGSize(width: 156, height: 24)
            addSubview(animationView)
            animationView.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 156, height: 24))
                make.center.equalToSuperview()
            }
            animationView.isHidden = true
            return animationView
        }()
        
        waveSilentView = {
            let v = UIImageView()
            v.image = UIImage(named: "ai_phone_call_silent")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 156, height: 8))
                make.center.equalTo(waveWavingAnimationView)
            }
            v.isHidden = true
            return v
        }()
    }
    
    func play(){
        waveWavingAnimationView.isHidden = false
        waveSilentView.isHidden = true
        waveWavingAnimationView.play()
    }
    
    func stop(){
        waveWavingAnimationView.isHidden = true
        waveSilentView.isHidden = false
        waveWavingAnimationView.stop()
    }
}
