//
//  TSThemeSetVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/3.
//

class TSThemeSetVC: TSBaseVC {
    
    
    var model:TSThemeModel
    var duration:Float

    lazy var audioPlayer = TSBusinessAudioPlayer{ [weak self] state in
        guard let self = self else { return }
        audioPlayerStateChange(state: state)
    }
    
    lazy var ringTool: TSBandRingTool = {
        let ringTool = TSBandRingTool(targetVC: self)
        return ringTool
    }()
    
    lazy var contactsTool: TSContactsTool = {
        let contactsTool = TSContactsTool(targetVC: self)
        return contactsTool
    }()
    
    
    lazy var ringView: TSThemeSetRingToneView = {
        let ringView = TSThemeSetRingToneView()
        ringView.setBtn.addTarget(self, action: #selector(clickSetRing), for: .touchUpInside)
        ringView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickPlay)))
        return ringView
    }()
    
    lazy var posterView: TSThemeSetPosterView = {
        let posterView = TSThemeSetPosterView()
        posterView.setBtn.addTarget(self, action: #selector(clickSetPoster), for: .touchUpInside)
        return posterView
    }()
    
    lazy var photoView: TSThemeSetPhotorView = {
        let photoView = TSThemeSetPhotorView()
        photoView.setBtn.addTarget(self, action: #selector(clickPhoto), for: .touchUpInside)
        return photoView
    }()
    
    lazy var stackView : TSCustomStackView = {
        let stackView = TSCustomStackView(axis: .vertical,spacing: 16)
        return stackView
    }()
    
    init(model: TSThemeModel, duration: Float) {
        self.model = model
        self.duration = duration
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    var avatarImage:UIImage?{
        return photoView.netWorkImageView.image
    }
    
    override func createView() {
        setViewBgImageNamed(named: kViewBJ)
        addNormalNavBarView()
        setPageTitle("Set Theme".localized)
        
        _ = setNavigationItem("", imageName: "tutorials", direction: .right, action: #selector(clickRight))

        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.bottom.equalTo(0)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
        }
        
        stackView.addSubviewToStack(ringView)
        stackView.addSubviewToStack(posterView)
        stackView.addSubviewToStack(photoView)
    
    }
    
    override func dealThings() {
        ringView.cellView.nameLab.text = model.ringtoneName
        ringView.cellView.setCoverImageView(urlString: model.ringtoneCover)
        ringView.cellView.timeLab.text = duration.floatToMinuteSecond()
        posterView.netWorkImageView.setAsyncImage(urlString: model.poster,contentMode: .scaleAspectFill)
        photoView.netWorkImageView.setAsyncImage(urlString: model.photo,contentMode: .scaleAspectFill)
    }
    
    func audioPlayerStateChange(state:TSBusinessAudioPlayer.PlayerState){
        switch state {
        case .loading(let progress):
            if progress == 0.0 {
                ringView.cellView.isloading = true
            }else if progress == 1.0 {
                ringView.cellView.isloading = false
            }
        case .play:
            ringView.cellView.isPlay = true
        case .stop:
            ringView.cellView.isPlay = false
        default:
            break
        }
    }
    
    @objc func clickRight(){
        let browseVC = TSThemeTutorialsVC()
        kPushVC(target: self, modelVC: browseVC)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        audioPlayer.stop()
    }

}

extension TSThemeSetVC {
    
    @objc func clickPlay(){
        if audioPlayer.isPlaying{
            audioPlayer.stop()
        }else{
            audioPlayer.playRingtone(ringtone: model.ringtone)
        }
    }
    
    @objc func clickSetRing(){
        ringTool.shareBand(with: model.ringtone, fileName: model.name) { success in
            
        }
    }
    
    @objc func clickSetPoster(){
        if let image = posterView.netWorkImageView.image{
            PhotoManagerShared.saveImageToAlbum(image) { success, error in
                if success {
                    kSavePhotoSuccesswShared.show(atView: self.view)
                }else{
                    debugPrint(error)
                }
            }
        }else{
            kShowToastDataMissing()
        }
    }
    
    @objc func clickPhoto(){
        guard let photo = avatarImage else { return }
        contactsTool.setContactsAvatar(avatarImage: photo) { data, error in
            if error == nil {
                kSavePhotoSuccesswShared.show(atView: self.view,showViewBtn: false)
            }
        }
    }

}

