//
//  BaseViewController+BaseHttp.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/9/21.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension BaseViewController {
    // MARK: 判断APP是否被停用
    func judgmentDeactivateEnableHome(){
        SinglePointWeChatHttp.sharedInstance().judgmentDeactivateEnable(userid: judgmentStrNil(str: AccountInfo.sharedInstance().UserId), resultBlock: { [weak self] (value:Any) in
            self?.juDeactivateEnableHomeData(array: value as! NSMutableArray)
        }) { (value:Any) in
        }
    }
    func juDeactivateEnableHomeData(array:NSMutableArray) {
        if (array.count == 0) {
        } else {
            showOneAlert(title: "提示", message: array.firstObject as! String, titleSecondBtn: "确定", blockOtherBtn: {[weak self] in
                self?.dropOutAPP()
            })
//            deAllSelecCompanyDepartment()
//            AccountInfo.sharedInstance().clearAccount()
        }
    }
    // MARK:获取智能客服 未读消息
    func getUnReadNumberHttpPostCmd(){
        let cmd:HttpCommand = UnReadNumberHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpUnReadNumberHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpUnReadNumberHttpPostCmd_userId] = AccountInfo.sharedInstance().UserId
        attrDic[kHttpUnReadNumberHttpPostCmd_IDMToken] = AccountInfo.sharedInstance().singlePoint_access_token
        
        dic[kHttpUnReadNumberHttpPostCmd_attr] = attrDic
        dic[kHttpUnReadNumberHttpPostCmd_orderby] = "orderby"
        dic[kHttpUnReadNumberHttpPostCmd_pageNum] = 1
        dic[kHttpUnReadNumberHttpPostCmd_pageSize] = 10
        dic[kHttpUnReadNumberHttpPostCmd_sort] = "sort"
        dic[kHttpUnReadNumberHttpPostCmd_logic] = "logic"
        dic[kHttpUnReadNumberHttpPostCmd_filters] = []
        
        cmd.requestInfo = (dic as! [AnyHashable : Any])
//        print(dic)
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    @objc func httpUnReadNumberHttpResultResponse(result:RequestResult){
        let r:UnReadNumberHttpResult = result as! UnReadNumberHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getUnReadNumberHttpResult()
            if array.count != 0{
            }else{
                NotificationCenter.default.post(name: Notification.Name(rawValue: "publishBtnEetNum"), object: "0")
            }
        }else{
//            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    //    // MARK:上传手机信息
    func getClickRateHttpCmd(infoModel: ClickRateModel){
        let cmd:HttpCommand = ClickRateHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpClickRateHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpClickRateHttpCmd_devicenumber] = AccountInfo.sharedInstance().appUUID
        dic[kHttpClickRateHttpCmd_companyname] = judgmentStrNil(str: AccountInfo.sharedInstance().infoCompanyName)
        dic[kHttpClickRateHttpCmd_userid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpClickRateHttpCmd_name] = infoModel.name
        dic[kHttpClickRateHttpCmd_belong] = infoModel.belong
        dic[kHttpClickRateHttpCmd_scope] = infoModel.scope
        dic[kHttpClickRateHttpCmd_type] = infoModel.type
        cmd.requestInfo = (dic as! [AnyHashable : Any])
//        print(dic)
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    @objc func httpClickRateHttpResultResponse(result:RequestResult){
        let r:ClickRateHttpResult = result as! ClickRateHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getClickRateHttpResult()
            if array.count != 0{
            }else{
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    
    
//    // MARK:上传手机信息
    func getDeviceInformationHttpCmd(){
        let cmd:HttpCommand = DeviceInformationHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpDeviceInformationHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        cmd.requestInfo = DeviceInformation.share().getDeviceInformation(dic) as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()

    }
    @objc func httpDeviceInformationHttpResultResponse(result:RequestResult){
        let r:DeviceInformationHttpResult = result as! DeviceInformationHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getDeviceInformationHttpResult()
            if array.count != 0{
//                SMToastView.showMessage(array[0] as? String )
            }else{

            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    //版本更新
//    func getNewVersionUpdateHttpCmd(){
//        let cmd:HttpCommand = NewVersionUpdateHttpCmd.init(version: PHttpVersion_v1)
//        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
//            if result != nil {
//                self?.httpNewVersionUpdateHttpResultResponse(result: result)
//            }else{
//                print("空的")
//            }
//        }
//        let dic:NSMutableDictionary = NSMutableDictionary()
//        let attrDic:NSMutableDictionary = NSMutableDictionary()
//        attrDic[kHttpAdvisoryHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
//        dic[kHttpAdvisoryHttpCmd_attr] = attrDic
//        dic[kHttpAdvisoryHttpCmd_orderby] = "createon"
//        dic[kHttpAdvisoryHttpCmd_pageNum] = "0"
//        dic[kHttpAdvisoryHttpCmd_pageSize] = "20"
//        dic[kHttpAdvisoryHttpCmd_sort] = "desc"
//        let filtersDic:NSMutableDictionary = NSMutableDictionary()
//        filtersDic[kHttpAdvisoryHttpCmd_value] = "41"
//        let filtersArray = NSMutableArray()
//        filtersArray.add(filtersDic)
//        dic[kHttpAdvisoryHttpCmd_filters] = filtersArray
//
//        cmd.requestInfo = dic as [NSObject:AnyObject]
//        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
//        cmd.requestInfo = dic as? [AnyHashable : Any]
//        cmd.completeDelegate=completeDelegate
//        print("url==%@",cmd.getUrl())
//        cmd.execute()
//    }
//    func httpNewVersionUpdateHttpResultResponse(result:RequestResult){
//        let r:NewVersionUpdateHttpResult = result as! NewVersionUpdateHttpResult
//        if r.isOk() {
//            let array:NSMutableArray = r.getNewVersionUpdateHttpResult()
//            if array.count != 0{
//                let model:NewVersionUpdateModel = array[0] as! NewVersionUpdateModel
//                showOneAlert(title: "版本更新", message: ("请更新到"+model.versionNumber!), titleSecondBtn: "确定", blockOtherBtn: {[weak self] in
//                    self?.deAllSelecCompanyDepartment()
//                    self?.downloadUrl(urlString: model.downloadLink!)
//                })
//            }
//        }else{
//            SMToastView.showMessage("请查看当前网络状态！")
//        }
//    }
    // MARK: IDM验证Token
    func isAccess_tokenTrue(){
        if AccountInfo.sharedInstance().singlePoint_access_token == nil || AccountInfo.sharedInstance().singlePoint_access_token == "" || AccountInfo.sharedInstance().loginType != "1" {
            return
        }
        if isSingOutAction() {return}
        SinglePointWeChatHttp.sharedInstance().judgmentTokenExpired(resultBlock: {(value) in
//            self?.classAction(selecHttpModel: selecHttpModel)
        }) {(value) in
//            self?.classAction(selecHttpModel: selecHttpModel)
        }
        return
        let cmd:HttpCommand = VerificationTokenHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpVerificationTokenHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpVerificationTokenHttpPostCmd_tokenId] = AccountInfo.sharedInstance().singlePoint_access_token!
        dic[kHttpVerificationTokenHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpVerificationTokenHttpPostCmd_appId] = AccountInfo.sharedInstance().singlePoint_appId
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpVerificationTokenHttpResultResponse(result:RequestResult){
        let r:VerificationTokenHttpResult = result as! VerificationTokenHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getVerificationTokenHttpResult()
            if array.count == 0{
                print("验证成功！")
            } else {
                print("验证失败！")
//                AccountInfo.sharedInstance().clearAccount()
//                self.deAllSelecCompanyDepartment()
//                showOneAlert(title: "温馨提示", message: "您当前登录已失效，需要重新登录！", titleSecondBtn: "确定", blockOtherBtn: {
//                    NotificationCenter.default.post(name: Notification.Name(rawValue: "setLoginWindow"), object: "0")
//                })
            }
            self.hidenHintView()
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    // MARK:根据公司判断登录方式是否发生改变
    func judgmentLoginTypeHttpAction(){
        SinglePointWeChatHttp.sharedInstance().getLoginTypeHttpGetCmd(userid: judgmentStrNil(str: AccountInfo.sharedInstance().UserId), resultBlock: {[weak self] (value:Any) in
            self?.judgmentloginTypeAction(array: value as! NSMutableArray)
        }) { (value:Any) in
            
        }
    }
    func judgmentloginTypeAction(array:NSMutableArray) {
        if array.count != 0 {
            let item:String = array.firstObject as! String
            var loginType = ""
            if (item == "3") {
                ///集团单点
                loginType = LoginType.SinglePointLoginType.rawValue
            } else {
                /// 自带登录
                loginType = LoginType.OldLoginType.rawValue
            }
            if (AccountInfo.sharedInstance().loginType != loginType) {
                AccountInfo.sharedInstance().clearAccount()
                self.deAllSelecCompanyDepartment()
                showOneAlert(title: "温馨提示", message: "登录已失效，请重新登录！", titleSecondBtn: "确定", blockOtherBtn: {
                    NotificationCenter.default.post(name: Notification.Name(rawValue: "setLoginWindow"), object: "0")
                })
            }
        }
    }
}
