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

class TSViewTool: UIView {
    
    static func createNormalSubmitBtn(title:String, imageNamed:String? = nil, action: (() -> Void)? = nil) -> UIButton {
        let image = UIImage(named:imageNamed ?? "")
        let btn = UIButton.createButton(title:title,image: image,font: UIFont.font(size: 18,weight: .medium),titleColor:"#010101".color,action: action)
   
        var buttonBgImage = UIImage(named: "normal_submit_bg")!
//        if UIDevice.isPad {
//            buttonBgImage = buttonBgImage.resizableImage(withCapInsets: UIEdgeInsets(top:24, left: 24, bottom: 24, right: 24), resizingMode: .stretch)
//        }
          btn.setBackgroundImage(buttonBgImage, for: .normal)
        
//        kDelayMainShort {
//            btn.addGradientBg(colors: ["#B3FFAB".color.cgColor,"12FFF7".color.cgColor],startPoint: CGPoint(x: 0, y: 1),endPoint: CGPoint(x: 1, y: 0))
//        }

        
        if image != nil {
            btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
            btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        }
        return btn
    }
    
    
    static func setNormalSubmitBtn(btn:UIButton,showVip:Bool) {
        if showVip{
            btn.setImage(UIImage(named: "vip_icon"), for: .normal)
            btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
            btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        }else{
            btn.setImage(nil, for: .normal)
            btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
    }
    
    static 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
    }
}



let kWapppaperPlaceholderImage = UIImage(named: "wapppaper_placeholder")
let KIconLiveImage = UIImage(named: "icon_live")

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()
    }()
    
    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(textLabel)
        
        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()
                }
            }
        }
        view.addSubview(viewButton)
        
        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) {
        
        kExecuteOnMainThread {
            self.textLabel.text = text
            atView.addSubview(self.saveSuccessBg)
            self.saveSuccessBg.snp.remakeConstraints { make in
                make.width.equalTo(288)
                make.height.equalTo(48)
                make.centerX.equalToSuperview()
                make.top.equalTo(k_Height_statusBar()+56.0)
            }
        }

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