//
//  TSSaveSuccessTool.swift
//  Pods
//
//  Created by 100Years on 2025/4/24.
//

public let kSaveSuccesswShared = TSSaveSuccessTool.shared
open class TSSaveSuccessTool {
    
    static let shared = TSSaveSuccessTool()
    
    public var clickViewHandle:(()->Void)?
    
    private lazy var textLabel:UILabel = {
        let textLabel = UILabel()
        textLabel.textColor = UIColor.mainText
        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 = "#43DC00".uiColor
        let viewButton = UIButton.createButton(title: "View".localized ,backgroundColor: color.withAlphaComponent(0.1),font: UIFont.font(size: 12),titleColor: color,corner: 14) { [weak self]  in
            guard let self = self else { return }
            if let clickViewHandle = clickViewHandle {
                clickViewHandle()
            }else {
                if let url = URL(string: "photos-redirect://") {
                    if UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    }
                }
            }
            
            DispatchQueue.main.async{
                self.saveSuccessBg.removeFromSuperview()
            }
        }
        viewButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        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(viewButton.intrinsicContentSize.width)
            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(-8)
            make.centerY.equalToSuperview()
        }

        return view
    }

    
    
    public func getBottom(topY:CGFloat)->CGFloat{
        let bottom = -(k_ScreenHeight - 48 - topY)
        debugPrint("bottom=\(bottom)")
        return bottom
    }
    
    public func show(atView:UIView,text:String = "Save Successfully".localized,deadline:Double = 5.0,bottom:CGFloat = -112,showViewBtn:Bool = true,clickViewHandle:(()->Void)? = nil) {
        self.clickViewHandle = clickViewHandle
        kMainAsync {
            self.textLabel.text = text
            self.viewButton.isHidden = !showViewBtn
            atView.addSubview(self.saveSuccessBg)
            self.saveSuccessBg.snp.remakeConstraints { make in
                make.width.greaterThanOrEqualTo(288)
                make.width.lessThanOrEqualTo(k_ScreenWidth-32)
                make.height.equalTo(48)
                make.centerX.equalToSuperview()
                make.top.equalTo(k_Nav_Height + 10)
            }
        }

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