//
//  SinglePointViewController+weChat.swift
//  MobilePortal
//
//  Created by caiyang on 2020/7/29.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension SinglePointViewController {
    /// 微信
    @objc func sendAuthRequest(){
        self.loginInitType = .LoginWeiChatType
        WXApiManager.shared()?.delegate = self
        WXApiManager.shared()?.sendAuthRequest(with: self, delegate: self)
        httpViewModel.access_tokenModel = GetAccessTokenModel()
        httpViewModel.userinfoModel = UserinfoModel()
        httpViewModel.access_tokenModel?.successBlock = {[weak self] (value:Any) in
            self?.httpViewModel.userinfoHttpAction()
        }
        httpViewModel.userinfoModel?.successBlock = {[weak self] (value:Any) in
            self?.getWeChatXinfoHttpPostCmd()
        }
 
    }
/// WXAuthDelegate
    func wxAuthSucceed(_ code: String!) {
        AccountInfo.sharedInstance().weChat_code = code
        httpViewModel.getCodeWeChatAccess_token()
    }
    func wxAuthDenied() {
        
    }
    
    /// 通过微信ID获取用户
    func getWeChatXinfoHttpPostCmd(){
        showHintView()
        let cmd:HttpCommand = WeChatXinfoHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpWeChatXinfoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpWeChatXinfoHttpPostCmd_wechatid] = judgmentStrNil(str: AccountInfo.sharedInstance().weChat_unionid)
        dic[kHttpWeChatXinfoHttpPostCmd_ticket] = ""
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpWeChatXinfoHttpResultResponse(result:RequestResult){
        let r:WeChatXinfoHttpResult = result as! WeChatXinfoHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getWeChatXinfoHttpResult()
            if (array.count == 0) {
//                self.getLoginDeviceInformationHttpCmd()
                self.getDeviceDisabledHttpPostCmd()
            } else {
                SMToastView.showMessage((array[0] as! String))
                self.gotoWeChatBinding()
                self.hidenHintView()
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
            self.hidenHintView()
        }
    }
    /// 微信绑定
    func getWeChatBindingHttpPostCmd(){
        showHintView()
        let cmd:HttpCommand = WeChatBindingHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpWeChatBindingHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpWeChatBindingHttpPostCmd_userid] = AccountInfo.sharedInstance().UserId
        dic[kHttpWeChatBindingHttpPostCmd_wechatid] = AccountInfo.sharedInstance().weChat_unionid
        dic[kHttpWeChatBindingHttpPostCmd_state] = "1"
        dic[kHttpWeChatBindingHttpPostCmd_wechatname] = AccountInfo.sharedInstance().weChatInfo_nickname
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpWeChatBindingHttpResultResponse(result:RequestResult){
        let r:WeChatBindingHttpResult = result as! WeChatBindingHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getWeChatBindingHttpResult()
            //            actionHttpGCD()
            if (array.count == 0) {
//                SMToastView.showMessage("绑定成功！")
//                getLoginDeviceInformationHttpCmd()
            } else {
                SMToastView.showMessage((array[0] as! String))
                self.hidenHintView()
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
}
