//
//  WebViewController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/7.
//

import Foundation
import WebKit
import SnapKit
import DDLogKit_Private
import DDToastKit_Private
import DDControlsKit_Private

class WebViewController : DDViewController {
    lazy var webView = WKWebView.init()
    var vcTitle : String
    var url : String?

    public init(title:String,url:String) {
        self.vcTitle = title
        self.url = url
        super.init(nibName: nil, bundle: nil)
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        title = vcTitle
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        
        webView.navigationDelegate = self
        webView.uiDelegate = self
        view.addSubview(webView)
        
        let request = URLRequest(url: URL(string: url!)!)
        webView.load(request)
        
        webView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension WebViewController : WKNavigationDelegate,WKUIDelegate {
    public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DDLog(message: "finish--------------------------------")
    }
    public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        DDLog(message: "didFail--------------------------------")
        view.dd_makeToast(webViewLoadFailed) {[weak self] _ in
            self?.navigationController?.popViewController(animated: true)
        }
    }
    public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        DDLog(message: "didFailProvisionalNavigation--------------------------------")
    }
}
