//
//  YHAIEvaluationWebView.swift
//  galaxy
//
//  Created by Dufet on 2024/12/7.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHAIEvaluationWebView: UIView {
    
    var finishBlock: (([String:Any])->())?
    
    lazy var webVC: YHH5WebViewVC = {
        let vc = YHH5WebViewVC()
        vc.isFullScreenFlag = true
        vc.isHideNavigationBar = true
        vc.evaluationResultCallback = {
            [weak self] dict in
            guard let self = self else { return }
            finishBlock?(dict)
            self.dismiss()
        }
        return vc
    }()
    
    static func webView(url: String, finishBlock: (([String:Any])->())?) -> YHAIEvaluationWebView {
        let view = YHAIEvaluationWebView(frame: UIScreen.main.bounds, url: url)
        view.webVC.url = url
        view.finishBlock = finishBlock
        return view
    }
    
    func show() {
        
        self.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight)
        UIApplication.shared.yhKeyWindow()?.addSubview(self)
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut, animations: {
            self.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight)
        }) { finished in
            self.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight)
        }
    }
    
    func dismiss() {
        
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut, animations: {
            self.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight)
        }) { finished in
            self.removeFromSuperview()
        }
    }
    
    init(frame: CGRect, url: String) {
        super.init(frame: frame)
        self.webVC.url = url
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createUI() {
        self.addSubview(webVC.view)
        webVC.view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
