//
//  CustomRateView.swift
//  PhoneManager
//
//  Created by edy on 2025/4/27.
//

import UIKit

class CustomRate{
    
    // 自定义初始化方法
    func show() {
       view.show()
    }
    
    lazy var view: CustomRateView = {
        let view = Bundle.main.loadNibNamed("CustomRateView", owner: nil)?.last as! CustomRateView
        view.frame = CGRect.init(x: 0, y: ScreenH, width:ScreenW, height: ScreenH)
        return view
    }()
}



class CustomRateView: UIView {
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var okBtn: UIButton!
    
    var callblock:(()->Void) = {}
    
    override func awakeFromNib() {
        super.awakeFromNib()
        okBtn.layer.cornerRadius = 15
        bgView.layer.cornerRadius = 12
    }
    
    @IBAction func okClick(_ sender: Any) {
        viewRemoveAction()
        guard let url = URL(string: "https://apps.apple.com/us/app/id\(APPConfig.APPID)?action=write-review") else {
              return
          }
          if UIApplication.shared.canOpenURL(url) {
              UIApplication.shared.open(url, options: [:], completionHandler: nil)
          }
    }
    
    @IBAction func noClick(_ sender: Any) {
        viewRemoveAction()
    }
    
    private func viewRemoveAction(){
        DispatchQueue.main.async {
            self.callblock()
            UIView.animate(withDuration: AniDuration) {
                self.frame = CGRect.init(x: 0, y: ScreenH, width:ScreenW, height: ScreenH)
            }completion: { _ in
                self.removeFromSuperview()
            }
        }
    }
    
    func show(){
        
//        DispatchQueue.main.async {
        KEYWINDOW()?.addSubview(self)
        self.frame = CGRect.init(x: 0, y: ScreenH, width:ScreenW, height: ScreenH)
            UIView.animate(withDuration: AniDuration) {
                self.frame = CGRect.init(x: 0, y: 0, width:ScreenW, height: ScreenH)
            }
//        }
    }
    
}
