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

import UIKit

extension SinglePointWeChatVC {
    
    /// 是否开启微信
    @objc func getWeChatHttp(){
//        gotoWeChatBinding()
//        sendAuthRequest()
//        SMToastView.showMessage("当前IDM不支持微信登录！")
        SinglePointWeChatHttp.sharedInstance().getWechatScopeListHttpCmd(resultBlock: { [weak self] (value:Any) in
            self?.weChatIDMType(array: value as! NSMutableArray)
        }) {(value:Any) in

        }
    }
    func weChatIDMType(array:NSMutableArray){
        for index in 0..<array.count {
            let item:ScopeListHttpModel = array[index] as! ScopeListHttpModel
            /// 1.开启；0.禁用；
            if (index == 0) {
                if (item.state == "1") {
                    sendAuthRequest()
                } else {
                    SMToastView.showMessage("当前微信登录未启用！")
                }
            }
        }
    }
    /// 微信
    @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()
            self?.setIDMWeChatBindingHttpPostCmd()
        }
        
    }
    /// 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.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()
            if (array.count == 0) {
            } else {
                SMToastView.showMessage((array[0] as! String))
                self.hidenHintView()
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
}
