//
//  WebSwiftViewController+Preview.swift
//  MobilePortal
//
//  Created by caiyang on 2021/2/20.
//  Copyright © 2021 yin chen. All rights reserved.
//

import UIKit

extension WebSwiftViewController {
    // 下载zip文件
    func downLoaderZIPFileAction(download:String,cookie:String){
        showHintView()
//        let downloadUrl = ""
        DownLoaderFile.sharedInstance().downloaderFileAction(downLoaderUrl: WebActionOC.share().decodingAction(download), cookie: cookie, gotoblock: { [weak self] (value) in
            print("value==",value)
            self?.hidenHintView()
            self?.gotoDocumentController(path: (value as! String))
        }) { [weak self] in
            self?.hidenHintView()
        }
    }
    func gotoDocumentController(path:String?){
        let url:NSURL = NSURL.init(fileURLWithPath: path!)
        if url == nil {
            print("失败！")
            SMToastView.showMessage("文件链接有误！")
            return
        }
        self.documentController = UIDocumentInteractionController.init(url: url as URL)
        self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
    }
    func downLoaderFileAction(download:String,cookie:String){
        showHintView()
//        let downloadUrl = "https://oa.sphchina.com:8443/download.do?download=1&fileid=5881307&filename=%E5%8A%9E18%20%E5%85%B3%E4%BA%8E%E8%A1%A8%E5%BD%B0%E4%B8%8A%E8%8D%AF%E6%8E%A7%E8%82%A1%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B82020%E5%B9%B4%E5%BA%A6%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%89%E5%B1%8A%E2%80%9C%E7%99%BE%E6%97%A5%E9%AB%98%E6%B8%A9%E5%AE%89%E5%85%A8%E6%97%A0%E4%BA%8B%E6%95%85%E2%80%9D%E7%AB%9E%E8%B5%9B%E6%B4%BB%E5%8A%A8%E4%BC%98%E8%83%9C%E9%9B%86%E4%BD%93%E5%92%8C%E5%85%88%E8%BF%9B%E4%B8%AA%E4%BA%BA%E7%9A%84%E9%80%9A%E7%9F%A5.pdf&f_weaver_belongto_userid=0&f_weaver_belongto_usertype="
        DownLoaderFile.sharedInstance().downloaderFileAction(downLoaderUrl: WebActionOC.share().decodingAction(download), cookie: cookie, gotoblock: { [weak self] (value) in
            self?.gotoPreviewVC(path: (value as! String))
        }) { [weak self] in
            self?.hidenHintView()
        }
    }
    func gotoPreviewVC(path:String?){
        hidenHintView()
        let url:NSURL = NSURL.init(fileURLWithPath: path!)
        if url == nil {
            print("失败！")
            SMToastView.showMessage("文件链接有误！")
            return
        }
        let pre = PreviewController()
        pre.loadUrlPath(fileUrl: url)
//        let pre = PreOCviewController()
//        pre.fileUrlList = NSMutableArray()
        let nav = UINavigationController.init(rootViewController: pre)
        nav.modalPresentationStyle = .fullScreen
//        self.navigationController?.pushViewController(pre, animated: true)
        self.present(nav, animated: true, completion: nil)
    }
    
    func getCookieValueKey() -> String{
        if self.webCookieArray.count == 0 {
            return ""
        }
        let dic:NSDictionary = HTTPCookie.requestHeaderFields(with: self.webCookieArray as! [HTTPCookie]) as NSDictionary
//        print(dic.object(forKey: "Cookie")!)
        guard let cookieStr = dic.object(forKey: "Cookie") else {
            return ""
        }
        return cookieStr as! String
    }
    func downLoaderFileCookieAction(download:String){
        if #available(iOS 11.0, *) {
            let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
            cookieStore.getAllCookies {[weak self](webCookieArray)  in
                self?.webCookieArray = webCookieArray as NSArray
                self?.downLoaderFileAction(download: download, cookie: (self?.getCookieValueKey())!)
            }
        } else {
            webCookieArray = HTTPCookieStorage.shared.cookies! as NSArray
            self.downLoaderFileAction(download: download, cookie: self.getCookieValueKey())
        }
    }
}
