//
//  TSViewTool.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

class TSViewTool: UIView {
    

}


func createBlurEffectView(style:UIBlurEffect.Style,backgroundColor:UIColor? = nil) -> UIVisualEffectView {
    let blurEffect = UIBlurEffect(style: style)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
    if backgroundColor != nil {
        blurEffectView.backgroundColor = backgroundColor
    }
    
    return blurEffectView
}

//常用提交按钮
func kCreateNormalSubmitBtn(title:String, action: (() -> Void)? = nil) -> UIButton {
    let btn = UIButton.createButton(title:title,backgroundImage:UIImage(named: "submit_btn_bg"),font: UIFont.font(size: 16,weight: .medium),titleColor:.black,corner: 30,action: action)
    return btn
}
//常用取消按钮
func kCreateNormalCancelBtn(title:String, action: (() -> Void)? = nil) -> UIButton {
    let btn = UIButton.createButton(title:title,backgroundColor: .fromHex("#FFFFFF", alpha: 0.4),font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 30,action: action)
    return btn
}
//常用确定按钮
func kCreateNormalConfirmBtn(title:String, action: (() -> Void)? = nil) -> UIButton {
    let btn = UIButton.createButton(title:title,backgroundImage:UIImage(named: "submit_btn_small_bg"),font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 30,action: action)
    return btn
}



let kSavePhotoSuccesswShared = TSSavePhotoSuccessTool.shared
class TSSavePhotoSuccessTool {
    
    static let shared = TSSavePhotoSuccessTool()
    
    private lazy var textLabel:UILabel = {
        let textLabel = UILabel()
        textLabel.textColor = "#4A5178".color
        textLabel.text = "Save Successfully".localized
        textLabel.font = UIFont.font(size: 14)
        return textLabel
    }()
    
    private lazy var saveSuccessBg: UIView = {
        return creatSaveSuccessBg()
    }()
    
    
    private lazy var viewButton:UIView = {
        let viewButton = UIButton.createButton(title: "View".localized ,backgroundColor: "4FEA9D".toColor()?.withAlphaComponent(0.2),font: UIFont.font(size: 14),titleColor: "4FEA9D".toColor(),corner: 14) {
            if let url = URL(string: "photos-redirect://") {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    playVibration()
                }
            }
        }
        return viewButton
    }()
    
    func creatSaveSuccessBg() -> UIView {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 288, height: 48)
        // 阴影
        view.backgroundColor = .clear
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOffset = CGSize(width: 0, height: 2)
        view.layer.shadowOpacity = 0.1
        
        // 圆角
        let colorBg = UIView()
        colorBg.backgroundColor = .white
        colorBg.layer.cornerRadius = 8
        colorBg.layer.masksToBounds = true
        colorBg.clipsToBounds = true
        
        view.addSubview(colorBg)
        colorBg.snp.makeConstraints { make in
            make.leading.trailing.top.bottom.equalTo(0)
        }
        
        let image = UIImage(named: "success_icon")
        let iconView = UIImageView(image: image)
        view.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalTo(12)
        }
    
        view.addSubview(viewButton)
        view.addSubview(textLabel)
        viewButton.snp.makeConstraints { make in
            make.width.equalTo(65)
            make.height.equalTo(28)
            make.trailing.equalTo(-8)
            make.centerY.equalToSuperview()
        }
    
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconView.snp.trailing).offset(8)
            make.trailing.equalTo(viewButton.snp.leading).offset(-4)
            make.centerY.equalToSuperview()
        }

        return view
    }

    
    
    func show(atView:UIView,text:String = "Save Successfully".localized,showViewBtn:Bool = true) {
        
        kExecuteOnMainThread {
            self.textLabel.text = text
            self.viewButton.isHidden = !showViewBtn
            atView.addSubview(self.saveSuccessBg)
            self.saveSuccessBg.snp.remakeConstraints { make in
                make.width.equalTo(288)
                make.height.equalTo(48)
                make.centerX.equalToSuperview()
                make.bottom.equalTo(-112)
            }
        }

        DispatchQueue.main.asyncAfter(deadline: .now()+5.0) {
            self.saveSuccessBg.removeFromSuperview()
        }
    }
    
    
}
