//
//  SinglePointWeChatVC+SinglePoint.swift
//  MobilePortal
//
//  Created by caiyang on 2020/8/24.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit
import HandyJSON
import SwiftyJSON
extension SinglePointWeChatVC {
    /// NEW 获取 Access_token
    func getVerifyTokenHttpPostCmdAction(){
        showHintView()
        let cmd:HttpCommand = VerifyTokenHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpVerifyTokenHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpVerifyTokenHttpPostCmd_appId] = AccountInfo.sharedInstance().singlePoint_appId
        dic[kHttpVerifyTokenHttpPostCmd_userName] = loginView.userTextView.text
        dic[kHttpVerifyTokenHttpPostCmd_passWord] = loginView.passTextView.text
        dic[kHttpVerifyTokenHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpVerifyTokenHttpPostCmd_attributeNames] = ""
        dic[kHttpVerifyTokenHttpPostCmd_authnMethod] = "UsernamePassword"
        dic[kHttpVerifyTokenHttpPostCmd_equipmentId] = AccountInfo.sharedInstance().appUUID
        
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpVerifyTokenHttpResultResponse(result:RequestResult){
        let r:VerifyTokenHttpResult = result as! VerifyTokenHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getVerifyTokenHttpResult()
            if array.count == 0{
//                SMToastView.showMessage("登录成功！")
                let strMessage = "设备：" + judgmentStrNil(str: AccountInfo.sharedInstance().appUUID!) + ";IDM单点登录;" + newDateStr()
                SinglePointWeChatHttp.sharedInstance().setIdmLogSaveHttpPostCmd(inputmessage: judgmentStrNil(str: self.loginView.userTextView.text), logmessage: strMessage, idmmessage: judgmentStrNil(str: AccountInfo.sharedInstance().UserId))
                AccountInfo.sharedInstance().singlePoint_startTime = newDateStr()
                getLoginDeviceInformationHttpCmd()
            } else {
                self.hidenHintView()
                var content = array.firstObject as! String
                if content == "002" && array.count == 2 {
                    let lastObject = array.lastObject as! String
                    content = "(" + content + ")" + lastObject
                    self.tankuangTwo(content: content)
                } else if array.count == 1 {
//                    SMToastView.showMessage(content)
                    return
                }
            }
        }else{
            AccountInfo.sharedInstance().clearAccount()
            SMToastView.showMessage("请查看当前网络状态！")
            self.hidenHintView()
        }
    }
    /// 002
    func tankuangTwo(content:String) {
        showAlert(title: "IDM登陆失败", message: content, titleCancelBtn: "取消", titleSecondBtn: "去修改") {[weak self] in
            AccountInfo.sharedInstance().ChangPassWordUserId = self?.loginView.userTextView.text; self?.gotoChangePassWordViewController(changePassWordType: .LoginChangePassWordType)
        }
    }
    /// 登录 获取 Access_token
    func loginGetAccess_token(){
        showHintView()
        let cmd:HttpCommand = SinglePointLoginHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpSinglePointLoginHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpSinglePointLoginHttpPostCmd_userName] = loginView.userTextView.text
        dic[kHttpSinglePointLoginHttpPostCmd_passWord] = loginView.passTextView.text
        dic[kHttpSinglePointLoginHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpSinglePointLoginHttpPostCmd_authnMethod] = "UsernamePassword"
        dic[kHttpSinglePointLoginHttpPostCmd_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 httpSinglePointLoginHttpResultResponse(result:RequestResult){
        let r:SinglePointLoginHttpResult = result as! SinglePointLoginHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getSinglePointLoginHttpResult()
            if array.count == 0{
//                SMToastView.showMessage("登录成功！")
                loginGetUserInfo()
            } else {
                self.hidenHintView()
            }
        }else{
            AccountInfo.sharedInstance().clearAccount()
            SMToastView.showMessage("请查看当前网络状态！")
            self.hidenHintView()
        }
    }
    /// 登录 获取IDM 个人信息
    func loginGetUserInfo(){
        let cmd:HttpCommand = SinglePointInfoHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpSinglePointinfoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpSinglePointInfoHttpPostCmd_appId] = AccountInfo.sharedInstance().singlePoint_appId
        dic[kHttpSinglePointInfoHttpPostCmd_tokenId] = AccountInfo.sharedInstance().singlePoint_access_token
        dic[kHttpSinglePointInfoHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpSinglePointInfoHttpPostCmd_attributeNames] = ""
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpSinglePointinfoHttpResultResponse(result:RequestResult){
        let r:SinglePointinfoHttpResult = result as! SinglePointinfoHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getSinglePointinfoHttpResult()
            if array.count == 0{
                self.getDeviceDisabledHttpPostCmd()
            } else {
                SMToastView.showMessage((array.firstObject as! String))
            }
            self.hidenHintView()
        }else{
            AccountInfo.sharedInstance().clearAccount()
            SMToastView.showMessage("请查看当前网络状态！")
            self.hidenHintView()
        }
    }
    /// 判断 Access_token 是否合格
//    func isAccess_tokenTrue(){
//        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:SinglePointinfoHttpResult = result as! SinglePointinfoHttpResult
//        if r.isOk() {
//            let array:NSMutableArray = r.getSinglePointinfoHttpResult()
//            if array.count == 0{
//            } else {
//            }
//            self.hidenHintView()
//        }else{
//            AccountInfo.sharedInstance().clearAccount()
//            SMToastView.showMessage("请查看当前网络状态！")
//            self.hidenHintView()
//        }
//    }
}
