//
//  LNProfileVoiceBarView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNProfileVoiceBarView: UIButton {
    private let statusIc = UIImageView()
    private let durationLabel = UILabel()
    
    private var curUrl: String?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ url: String) {
        curUrl = url
        LNVoiceResourceManager.shared.getRemoteAudioDuration(urlStr: url)
        { [weak self] duration, err in
            guard let self else { return }
            guard let duration, err == nil else { return }
            guard curUrl == url else { return }
            
            isHidden = duration <= 0
            durationLabel.text = "\(Int(duration.rounded()))\""
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileVoiceBarView: LNVoicePlayerNotify {
    func onAudioStartPlay(path: String) {
        guard path == curUrl else { return }
        statusIc.image = .icVoicePause
    }
    
    func onAudioStopPlay(path: String) {
        guard path == curUrl else { return }
        statusIc.image = .icVoicePlay
        LNVoiceResourceManager.shared.getRemoteAudioDuration(urlStr: path)
        { [weak self] duration, err in
            guard let self else { return }
            guard let duration, err == nil else { return }
            guard curUrl == path else { return }
            
            isHidden = duration <= 0
            durationLabel.text = "\(Int(duration.rounded()))\""
        }
    }
    
    func onAudioUpdateDuration(path: String, cur: TimeInterval, total: TimeInterval) {
        guard path == curUrl else { return }
        durationLabel.text = "\(Int((total - cur).rounded()))\""
    }
}

extension LNProfileVoiceBarView {
    private func setupViews() {
        isHidden = true
        
        layer.cornerRadius = 13
        setBackgroundImage(.primary_7, for: .normal)
        clipsToBounds = true
        addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curUrl else { return }
            if LNVoicePlayer.shared.playingUrl == curUrl {
                LNVoicePlayer.shared.stop()
            } else {
                LNVoicePlayer.shared.play(curUrl)
            }
        }), for: .touchUpInside)
        snp.makeConstraints { make in
            make.height.equalTo(26)
            make.width.equalTo(62)
        }
        
        statusIc.isUserInteractionEnabled = false
        statusIc.image = .icVoicePlay
        statusIc.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        statusIc.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        addSubview(statusIc)
        statusIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(3)
        }
        durationLabel.isUserInteractionEnabled = false
        durationLabel.text = "15“"
        durationLabel.font = .heading_h5
        durationLabel.textColor = .text_1
        durationLabel.textAlignment = .center
        addSubview(durationLabel)
        durationLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(statusIc.snp.trailing)
            make.trailing.equalToSuperview()
        }
    }
}
