//
//  SinglePointWeChatVC+IdmWeChat.swift
//  MobilePortal
//
//  Created by caiyang on 2020/12/9.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension SinglePointWeChatVC {
    /// MARK: IDM 微信 是否绑定 并且获取信息
    func setIDMWeChatBindingHttpPostCmd(){
        showHintView()
        let cmd:HttpCommand = IDMWeChatBindingHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.getIDMWeChatBindingHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpIDMWeChatBindingHttpPostCmd_appId] = AccountInfo.sharedInstance().singlePoint_appId
        dic[kHttpIDMWeChatBindingHttpPostCmd_openId] = judgmentStrNil(str: AccountInfo.sharedInstance().weChat_unionid)
//         dic[kHttpIDMWeChatBindingHttpPostCmd_openId] = "qwertyu"
        dic[kHttpIDMWeChatBindingHttpPostCmd_remoteIp] = AccountInfo.sharedInstance().singlePoint_remoteIp
        dic[kHttpIDMWeChatBindingHttpPostCmd_userName] = ""
        dic[kHttpIDMWeChatBindingHttpPostCmd_password] = ""
        dic[kHttpIDMWeChatBindingHttpPostCmd_equipmentId] = AccountInfo.sharedInstance().appUUID
        dic[kHttpIDMWeChatBindingHttpPostCmd_wechatname] = judgmentStrNil(str: AccountInfo.sharedInstance().weChatInfo_nickname)
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func getIDMWeChatBindingHttpResultResponse(result:RequestResult){
        let r:IDMWeChatBindingHttpResult = result as! IDMWeChatBindingHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getIDMWeChatBindingHttpResult()
            if array.count == 0{
//                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))
                
                if (AccountInfo.sharedInstance().loginType != nil && AccountInfo.sharedInstance().loginType == "1") {
                    self.loginType = .SinglePointLoginType
                    AccountInfo.sharedInstance().singlePoint_startTime = newDateStr()
                } else {
                    self.loginType = .OldLoginType
                }
//                getLoginDeviceInformationHttpCmd()
                judgmentDeactivateEnableS()
                
            } else {
                self.hidenHintView()
                var content = array.firstObject as! String
                if content == "101" && array.count == 2 {
                    let lastObject = array.lastObject as! String
                    content = "(" + content + ")" + lastObject
                    SMToastView.showMessage(content)
                    self.gotoWeChatBinding()
                }
                
            }
        }else{
            hidenHintView()
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    // MARK: 微信登陆之后，判断APP是否被停用
    func judgmentDeactivateEnableS(){
        SinglePointWeChatHttp.sharedInstance().judgmentDeactivateEnable(userid: judgmentStrNil(str: AccountInfo.sharedInstance().UserId), resultBlock: { [weak self] (value:Any) in
            self?.juDeactivateEnableSinglePointDataS(array: value as! NSMutableArray)
        }) {[weak self] (value:Any) in
            self?.hidenHintView()
        }
    }
    func juDeactivateEnableSinglePointDataS(array:NSMutableArray) {
        if (array.count == 0) {
            self.getLoginDeviceInformationHttpCmd()
        } else {
            hidenHintView()
            showOneAlert(title: "提示", message: array.firstObject as! String, titleSecondBtn: "确定", blockOtherBtn: {[weak self] in
                self?.dropOutAPP()
            })
//            deAllSelecCompanyDepartment()
//            AccountInfo.sharedInstance().clearAccount()
        }
    }
}
