//
//  ViewController.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/9/3.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

import SwiftyJSON
import HandyJSON
import SnapKit

class ViewController: UIViewController {

    lazy var viewModel: SmallRobotViewModel = {
        return SmallRobotViewModel()
    }()
    var jiamiStr = ""
    var jiemiStr = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = statusBarColor
//        BDVoiceWakeUp.share().initwakeupEventManager()
//        IFlySpeech.share().initIFlyAIUIAgent()
//        BDSpeechRecognition.share().initwakeupEventManager()
        for index in 0..<5 {
            let btn:UIButton = UIButton.init(type: .custom)
            btn.frame = CGRect(x:50, y: 100+index*50, width:100 , height:30 )
            btn.setTitle(String(index), for: .normal)
            btn.backgroundColor = UIColor.white
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.tag = index+100
            btn.addTarget(self, action: #selector(operatingAction(_:)), for: .touchUpInside)
            self.view.addSubview(btn)
        }
//        SunAcBuy.com
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    @objc func operatingAction(_ send:UIButton){
        if send.tag == 100 {
//            IFlySpeech.share().startParsing()
//            BDSpeechRecognition.share().start()
//            getAdvisoryHttp()
//            IFLYSpeechTwo.share().initIfly()
//            addd()
//            self.jiami()
            if self.isInstallation(urlString: "wxwork://") == false {
//                self.openSafari(urlString: "wxwork://")
                self.openSafari(urlString: "https://apps.apple.com/cn/app/id1087897068")
                
            } else {
                print("未安装！")
            }
        }else if send.tag == 101 {
            self.jiemi()
//            BDVoiceWakeUp.share().startWakeup()
//            IFLYSpeechTwo.share().startParsing()
        }else if send.tag == 102 {
//            sendAuthRequest()
        }else if send.tag == 103 {
            
        }
        
    }
    // 用手机浏览器
    public func openSafari(urlString:String?){
        if urlString == nil {
            SMToastView.showMessage("链接地址有误，请联系管理员！")
            return
        }
        let url = URL(string: (WebActionOC.share()?.getUrl(urlString)!)!)
        //根据iOS系统版本，分别处理
        if #available(iOS 10, *) {
            UIApplication.shared.open(url!, options: [:],
                                      completionHandler: {
                                        (success) in
            })
        } else {
            UIApplication.shared.openURL(url!)
        }
    }
    //是否安装APP
    public func isInstallation(urlString:String?)->Bool{
        if urlString?.count == 0 || urlString == nil {
            return true
        }
        let url = URL(string: urlString!)
        if url == nil {
            return true
        }
        if UIApplication.shared.canOpenURL(url!) {
            return false
        }
        return true
    }
    func sendAuthRequest(){
        let req = SendAuthReq.init()
        req.scope = "snsapi_userinfo"
        req.state = "wxd477edab60670232"
        WXApi.send(req)
    }
    
    func getAdvisoryHttp() {
        viewModel.refreshDataSource()
        viewModel.endBlock = {
            //            self.group.leave()
        }
    }
    func addd(){
         MBProgressHUDFunc.sharedInstance().showHintView()
        let docDic : String? = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first;
        guard let path = docDic?.count == 0 ? "": docDic else {
            return
        }
        print(path)
        
    }
    func jiami() {
        let count = "method=m3&enc=Tj42MjY2NTM1NjUxNDM2NjM5Mzg2J00%2BbmZ0dGJoZi9tam9sL2RwbS9xZm9lam9oJ1U%2BMjcyMjoxNzc3NzExMQ%3D%3D&P=-3127525867802769921&userId=199476&IDMToken=MTAuMTEuMTgwLjgw|NWM2MTc1MmM4Nzc4NDVkZTY4NDIwZDlhNWU3ZTBkNGVmZGYwMjAyNmIwZTI2NjczNGU3OGU1ODA0MzE2ZmEyYg==|A2nBqk2Ku3JSx8XHdA75fbDBzBg="
        AccountInfo.sharedInstance().passWord = (WebActionOC.share()?.encryptAES(count, key: "Ag30FVKDPwlEAQcP"))!
        print(AccountInfo.sharedInstance().passWord!)
    }
    func jiemi(){
        //uh5uTvFBTAbKXDsq
        self.jiemiStr = (WebActionOC.share()?.decryptAES("lfmii+rmeJgtNZj26jGSxhQGkHfKc5t3+4v4DoV8Y3PtaXr6wpDNz8Jhh2iyXkR93JRgXjGaJDwx6okprcVmHu7iogXb7TnzjID/5yCKfpmZjywgRPBcxk3g6gX1PRxWu8RnSVqZCk9vKTYd9H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Btxj9jVSRgaxKxuiRF0WcrMfrKwE2yJzkmMh4drFS3lHsywNDztdBI5DOWoygqVMGXowf13H3rYiJpImQGqZFOjyGh9XtYFNrJQUaVzdqvscIx19yLO7k9Ctimy3Bx9J1GrYDIfWrvpRkuM3SAFS4wkcoh5X+IGdMVbf1PgVyLqN13mmzKnL55OhtvBPy7zNqHeTbvHCC/yFj0QnKIiun9I+Ych/yrWS7CVDV1c+j8kYDDMk2MclTAl3M+9CeI+nYf9h23g+KQU5Jaz/pscx9A/hPebItlhVoLqDWbd2UYkSqhyFDOxUJUhesc4NlWG9huJbxLLdlcVaLJhrQq1IR4ZSobvF5tFQKziauNE6T/YObqCxwYA=;
//        self.jiemiStr = (WebActionOC.share()?.decryptAES("2Xf9U4hAMm9C5qmCNscSuLRqKW9wTkt9dvbXXGaejTo4KMvRFIPRaH2oNe+CnBbjPF2r5xrC0rF/eo7+KSoxCL58cefHbG8IKDpKIfCU04l6YW0hrvd+Tk3aryZtXTwXAjTTPyC2MKqHEJqIHu1qie7WQuI3SNGsbkyDkqM35JhoJk3xrQa8ZELL8lfUp4LF6hgKzriIlZwtopz6Eves3W8D3eYQ5/pOQmVkt0WqKlyzQ4p+QTcUGdsh7MzhhkfQ7Gh5lfszrA/o07Xc7wgOeRFwgr/CyrE+WdAVJYLkYs4amYo/ofA6ezhEuKsF64Znbml9dS96PMW+NdaCYkhTRhslfkjdOEiars5i63PsVyl4X9ndu9p4DBLs+BEVwyyknLw6f1RMyeHdrqDthh0pR5Xzcc6L1HlZJZ+SwZ9iDntjDd+5ZRnBTxyKuPtYwddanpDtA3xDn5+oiSkylZtypsfX7bo9S467fRg41HLfTwhvwKKvG/OkkT50NcyiC/0XUR68rkxFLAuzSEkgx4jDS3XO0ea1NDoh9r3CY7o6++paIUA10psVIGk/av6cG0DOC121KjOR6pY8VU1IKecPBQ==", key: "uh5uTvFBTAbKXDsq"))!
        print(self.jiemiStr)
//        showLXFPopUpToolView()
//        let str = "O4AiYdkx8UIJun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"))!)
//        print(WebActionOC.share()?.decryptAES(str, key: "uh5uTvFBTAbKXDsq")!)
        
        
//        var url = "SHYTB.com://ni+oR1nmE301aJZNMhehoefxjdqbk4uj6yHnu/KkUwensP4If46nozqoSAIyuKJn"
//        url = self.get(str1: url, str2: "//")
//        url = (WebActionOC.share()?.decryptAES(url, key: "6joMrSFQhTO4hRCT"))!
//        if self.judgmentUrl(str1: url, str2: "userId") && self.judgmentUrl(str1: url, str2: "&") {
//            let result = url.components(separatedBy:"&")
//            for index in 0..<result.count {
//                let str1 = result[index]
//                if self.judgmentUrl(str1: str1, str2: "userId") {
//                   print(self.get(str1: str1, str2: "="))
//                }
//            }
//        } else if self.judgmentUrl(str1: url, str2: "userId") && !self.judgmentUrl(str1: url, str2: "&") {
//            print("-------")
//
//        }
        
    }
    /// 应用公告通知
    func showLXFPopUpToolView() {
        LXFPopUpToolView.sharedInstance().show(title: "版本升级公告", content: "尊敬的用户您好，上药移动将于今晚18:00整版本升级，预计升级时间20分钟，届时将暂停访问，请知悉。")
    }
    func get(str1:String,str2:String)->String{
        let string = str1 as NSString
        let startRange = string.range(of: str2)
        let range = NSMakeRange(startRange.location+startRange.length, string.length-startRange.location-startRange.length)
        return string.substring(with: range)
    }
    //MARK:地址是否合法
    func judgmentUrl(str1:String,str2:String) -> (Bool) {
        let string = str1 as NSString
        if string.contains(str2) {
            return true
        }
        return false
    }
    
}

