//
//  TSSavePhotoSuccessTool.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/5.
//

let kSavePhotoSuccesswShared = TSSavePhotoSuccessTool.shared
class TSSavePhotoSuccessTool {
    
    static let shared = TSSavePhotoSuccessTool()
    
    private lazy var textLabel:UILabel = {
        let textLabel = UILabel()
        textLabel.textColor = UIColor.white
        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 color = "4FEA9D".uiColor
        let viewButton = UIButton.createButton(title: "View".localized ,backgroundColor: color.withAlphaComponent(0.1),font: UIFont.font(size: 14),titleColor: color,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 = "#333333".uiColor
        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()+2.0) {
            self.saveSuccessBg.removeFromSuperview()
        }
    }
    
    
}
