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

class TSThemeSetVC: TSBaseVC {
    
    
    var model:TSThemeModel
    var duration:Float
    
    
    let vm = TSThemeSetVM()
    
    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.cellView.playBtn.addTarget(self, action: #selector(clickPlay), for: .touchUpInside)
        ringView.setBtn.addTarget(self, action: #selector(clickSetRing), for: .touchUpInside)
        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.name
        ringView.cellView.timeLab.text = vm.floatToMinuteSecond(duration)
        posterView.netWorkImageView.setAsyncImage(urlString: model.poster,contentMode: .scaleAspectFill)
        photoView.netWorkImageView.setAsyncImage(urlString: model.photo,contentMode: .scaleAspectFill)
        
        vm.playRingtone(ringtone: model.ringtone)
    }
    
    @objc func clickRight(){
        
    }

}

extension TSThemeSetVC {
    
    @objc func clickPlay(){
        ringView.cellView.isPlay = !ringView.cellView.isPlay
        vm.changeAudioState(isPlay: ringView.cellView.isPlay)
    }
    
    @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)
            }
        }
    }

}

