
//
//  WeChatViewModel.swift
//  MobilePortal
//
//  Created by caiyang on 2020/5/15.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

class WeChatViewModel: NSObject {
    var dataSource = NSMutableArray()
    var endBlock:selectBlock!
    var access_tokenModel:GetAccessTokenModel?
    var refresh_tokenModel:GetRefresh_tokenModel?
    var userinfoModel:UserinfoModel?
    
}
extension WeChatViewModel {
    /// 
    func getCodeWeChatAccess_token() {
        WeChatProvider.request(WeChatAPI.weChatAccessToken(value: "")) {[weak self] (result) in
            
            if case let .success(response) = result {
                // 解析数据
                let data = try? response.mapJSON()
                let json = JSON(data!)
                print(json)
                if let access_tokenHttpModel = JSONDeserializer<GetAccessTokenModel>.deserializeFrom(json: json.description) {
//                    self?.access_tokenModel = access_tokenHttpModel
                    if ((access_tokenHttpModel.errcode) != nil) {
                        let str = "(" + access_tokenHttpModel.errcode! + ")" + access_tokenHttpModel.errmsg!
                        SMToastView.showMessage(str)
                        return
                    }
                    /// 接口调用凭证
                    AccountInfo.sharedInstance().weChat_access_token = judgmentStrNil(str: access_tokenHttpModel.access_token)
                    /// 用户刷新 access_token
                    AccountInfo.sharedInstance().weChat_refresh_token = judgmentStrNil(str: access_tokenHttpModel.refresh_token)
                    /// 授权用户唯一标识
                    AccountInfo.sharedInstance().weChat_openid = judgmentStrNil(str: access_tokenHttpModel.openid)
                    /// 当且仅当该移动应用已获得该用户的 userinfo 授权时，才会出现该字段
                    AccountInfo.sharedInstance().weChat_unionid = judgmentStrNil(str: access_tokenHttpModel.unionid)
                    ///
                    AccountInfo.sharedInstance().weChat_expires_in = judgmentStrNil(str: access_tokenHttpModel.expires_in)
                    if ((self?.access_tokenModel?.successBlock) != nil) {
                        self?.access_tokenModel?.successBlock!("")
                    }
                }
            }
        }
    }
    /// 刷新access_token 的接口
    func refresh_tokenHttpAction() {
        WeChatProvider.request(WeChatAPI.weChatRefreshToken(value: "")) {[weak self] (result) in
            
            if case let .success(response) = result {
                // 解析数据
                let data = try? response.mapJSON()
                let json = JSON(data!)
                print(json)
                if let refresh_tokenHttpModel = JSONDeserializer<GetRefresh_tokenModel>.deserializeFrom(json: json.description) {
//                    self?.refresh_tokenModel = refresh_tokenHttpModel
                    if ((refresh_tokenHttpModel.errcode) != nil) {
                        let str = "(" + refresh_tokenHttpModel.errcode! + ")" + refresh_tokenHttpModel.errmsg!
                        SMToastView.showMessage(str)
                        return
                    }
                    /// 接口调用凭证
                    AccountInfo.sharedInstance().weChat_access_token = judgmentStrNil(str: refresh_tokenHttpModel.access_token)
                    /// 用户刷新 access_token
                    AccountInfo.sharedInstance().weChat_refresh_token = judgmentStrNil(str: refresh_tokenHttpModel.refresh_token)
                    /// 授权用户唯一标识
                    AccountInfo.sharedInstance().weChat_openid = judgmentStrNil(str: refresh_tokenHttpModel.openid)
                    /// 当且仅当该移动应用已获得该用户的 userinfo 授权时，才会出现该字段
//                    AccountInfo.sharedInstance().weChat_unionid = judgmentStrNil(str: access_tokenHttpModel.unionid)
                    /// access_token 接口调用凭证超时时间，单位（秒）
                    AccountInfo.sharedInstance().weChat_expires_in = judgmentStrNil(str: refresh_tokenHttpModel.expires_in)
                    print(self?.access_tokenModel?.access_token!)
                }
            }
        }
    }
    /// access_token是否合格 的接口
    func isAccessTokenHttpAction() {
        WeChatProvider.request(WeChatAPI.weChatIsAccessToken(value: "")) {[weak self] (result) in
            
            if case let .success(response) = result {
                // 解析数据
                let data = try? response.mapJSON()
                let json = JSON(data!)
                print(json)
                if let access_tokenHttpModel = JSONDeserializer<GetAccessTokenModel>.deserializeFrom(json: json.description) {
                    self?.access_tokenModel = access_tokenHttpModel
                    if ((access_tokenHttpModel.errcode) != nil && access_tokenHttpModel.errcode == "0") {
                        
                    } else {
                        let str = "(" + access_tokenHttpModel.errcode! + ")" + access_tokenHttpModel.errmsg!
                        SMToastView.showMessage(str)
                    }
                }
            }
        }
    }
    /// userinfo 的接口
    func userinfoHttpAction() {
        WeChatProvider.request(WeChatAPI.weChatUserInfo(value: "")) {[weak self] (result) in
            
            if case let .success(response) = result {
                // 解析数据
                let data = try? response.mapJSON()
                let json = JSON(data!)
                print(json)
                if let userinfoHttpModel = JSONDeserializer<UserinfoModel>.deserializeFrom(json: json.description) {
//                    self?.userinfoModel = userinfoHttpModel
                    if ((userinfoHttpModel.errcode) != nil) {
                        let str = "(" + userinfoHttpModel.errcode! + ")" + userinfoHttpModel.errmsg!
                        SMToastView.showMessage(str)
                        return
                    }
                    ///普通用户昵称
                    AccountInfo.sharedInstance().weChatInfo_nickname = judgmentStrNil(str: userinfoHttpModel.nickname)
                    /// 普通用户性别，1 为男性，2 为女性
                    AccountInfo.sharedInstance().weChatInfo_sex = judgmentStrNil(str: userinfoHttpModel.nickname)
                    AccountInfo.sharedInstance().weChatInfo_province = judgmentStrNil(str: userinfoHttpModel.province)
                    AccountInfo.sharedInstance().weChatInfo_city = judgmentStrNil(str: userinfoHttpModel.city)
                    AccountInfo.sharedInstance().weChatInfo_country = judgmentStrNil(str: userinfoHttpModel.country)
                    AccountInfo.sharedInstance().weChatInfo_headimgurl = judgmentStrNil(str: userinfoHttpModel.headimgurl)
                    if ((self?.userinfoModel?.successBlock) != nil) {
                        self?.userinfoModel?.successBlock!("")
                    }
                    
                }
            }
        }
    }
}
