//
//  CCTransformProgressPopView.swift
//  SwiftProject
//
//  Created by Zhang, Joyce on 2024/2/23.
//

import UIKit

class CCTransformProgressPopView: UIView {
    
    typealias TransformActionBlock = ()->Void
    var transformActionBlock: TransformActionBlock?
    
    typealias CloseActionBlock = ()->Void
    var closeActionBlock: CloseActionBlock?
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "asset_exporting")
//        imageView.layer.cornerRadius = KHZSize(40)
//        imageView.clipsToBounds = true
//        imageView.layer.borderWidth = KHZSize(2)
//        imageView.layer.borderColor = UIColor.white.cgColor
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.font = KFont_Regular(18)
        label.textColor = KTextColor_White
        label.text = NSLocalizedString("导出中...", comment: "")
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.font = KFont_Regular(14)
        label.textColor = UIColor(hexString: "#9B9B9C")
        label.text = NSLocalizedString("请不要锁屏或者退出APP", comment: "")
        return label
    }()
    
    lazy var progressLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.font = KFont_Medium(18)
        label.textColor = KTextColor_White
        label.text = "0%"
        return label
    }()
    
    lazy var progressView: UIProgressView = {
        let progress = UIProgressView(progressViewStyle: .default)
        progress.backgroundColor = UIColor.clear
        progress.progressTintColor = KMain_Color //已有进度颜色
        progress.trackTintColor = UIColor(hexString: "#6A696A")  //剩余进度颜色（即进度槽颜色）
        return progress
    }()
    
    lazy var transformButton: UIButton = {
        
        let button = UIButton(type: .custom)
        //        button.setImage(UIImage.init(named: "login_button_normal"), for: .normal)
        //        button.setImage(UIImage.init(named: "login_ckeck_button_selected"), for: .highlighted)
        button.backgroundColor = KMain_Color
        button.layer.cornerRadius = KHZSize(20)
        button.clipsToBounds = true
        button.titleLabel?.font = KFont_Medium(14)
        button.titleLabel?.textColor = KTextColor_White
        button.setTitle(NSLocalizedString("转码导出", comment: ""), for: .normal)
        button.setTitleColor(KTextColor_White, for: .normal)
        button.addTarget(self, action: #selector(transformButtonAction(_:)), for: .touchUpInside)
        
        return button
        
    }();
    
    lazy var closeButton: UIButton = {
        
        let button = UIButton(type: .custom)
        //        button.setImage(UIImage.init(named: "login_button_normal"), for: .normal)
        //        button.setImage(UIImage.init(named: "login_ckeck_button_selected"), for: .highlighted)
        button.backgroundColor = .clear
        button.layer.cornerRadius = KHZSize(20)
        button.clipsToBounds = true
        button.titleLabel?.font = KFont_Medium(14)
        button.titleLabel?.textColor = KTextColor_White
        button.setTitle(NSLocalizedString("回到首页", comment: ""), for: .normal)
        button.setTitleColor(KTextColor_White, for: .normal)
        button.addTarget(self, action: #selector(closeButtonAction), for: .touchUpInside)
        
        return button
        
    }();
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        progressView.progress = 0.0 //默认进度50%
        
        self.backgroundColor = KColorFromRGB(rgbValue: 0x000000, alpha: 0.9)
        
        //加载
        self.addSubview(iconImageView)
        self.addSubview(titleLabel)
        self.addSubview(detailLabel)
        self.addSubview(progressView)
        self.addSubview(progressLabel)
        self.addSubview(closeButton)
        self.addSubview(transformButton)


        //布局
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(KVZSize(-60))
            make.width.equalTo(KHZSize(232))
            make.height.equalTo(KHZSize(176))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp.bottom).offset(KHZSize(16))
            make.centerX.equalTo(self.snp.centerX)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(KHZSize(16))
            make.centerX.equalTo(self.snp.centerX)
        }
        
        progressView.snp.makeConstraints { (make) in
            make.top.equalTo(detailLabel.snp.bottom).offset(KHZSize(24))
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(KHZSize(232))
            make.height.equalTo(5)
        }
        
        progressLabel.snp.makeConstraints { (make) in
            make.top.equalTo(progressView.snp.bottom).offset(KHZSize(8))
            make.centerX.equalTo(self.snp.centerX)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.snp.bottom).offset(KHZSize(-52))
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(KHZSize(345))
            make.height.equalTo(KHZSize(54))
        }
        
        transformButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(closeButton.snp.top).offset(KHZSize(-8))
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(KHZSize(345))
            make.height.equalTo(KHZSize(54))
        }
    }
    
    func resetUI() {
        titleLabel.text = NSLocalizedString("导出中...", comment: "")
        detailLabel.text = NSLocalizedString("请不要锁屏或者退出APP", comment: "")
        progressLabel.text = "0%"
        transformButton.isHidden = true
        closeButton.isHidden = true
        progressView.progress = Float(0)
        progressView.isHidden = false
        progressLabel.isHidden = false
    }
    func updateProgress(value:Float) {
        
        if value > 0.98 {
            titleLabel.text = NSLocalizedString("转码完成", comment: "")
            detailLabel.text = NSLocalizedString("正在导出内容至相册中", comment: "")//"导出内容已存储到相册中"
            progressLabel.isHidden = true
            transformButton.setTitle(NSLocalizedString("继续转码", comment: ""), for: .normal)
            transformButton.isHidden = false
            closeButton.isHidden = false
            progressView.isHidden = true
            iconImageView.image = UIImage.init(named: "asset_exported")
            
        }else{
            titleLabel.text = NSLocalizedString("处理中...", comment: "")
            detailLabel.text = NSLocalizedString("请不要锁屏或者退出APP", comment: "")
            progressLabel.text = String.init(format: "%.f%%", value*100)
            transformButton.isHidden = true
            closeButton.isHidden = true
            progressView.progress = value
        }
    }
    
    func updateInfo(title:String,desc:String) {
        titleLabel.text = title
        detailLabel.text = desc
    }
    
    @objc func transformButtonAction(_ btn:UIButton) {
        
        print("点击了确认")
        self.transformActionBlock?()
    }
    
    @objc func closeButtonAction(_ btn:UIButton) {
        
        print("点击了取消")
        self.closeActionBlock?()
    }
    
    

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
