//
//  PQMineViewModel.swift
//  PQSpeed
//
//  Created by SanW on 2020/5/27.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFFramework

// MARK: 我的相关ViewModel



/// 我的相关ViewModel
class PQMineViewModel: NSObject {
    typealias completeHander = (_ userInfo: [String: Any]?, _ msg: String?) -> Void

    /// 微信登录
    /// - Parameters:
    ///   - unionid: <#unionid description#>
    ///   - openId: <#openId description#>
    ///   - nickName: <#nickName description#>
    ///   - avatarUrl: <#avatarUrl description#>
    ///   - completeHander: <#completeHander description#>
    /// - Returns: <#description#>
    class func wechatLogin(unionid: String, openId: String, nickName: String, avatarUrl: String, completeHander: @escaping completeHander) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + login, parames: ["unionid": unionid, "openId": openId, "nickName": nickName, "avatarUrl": avatarUrl]) { response, _, error, _ in
            if error == nil {
                let userInfo: [String: Any] = response as! [String: Any]
                // 处理用户登录信息
                dealWithLoginInfo(userInfo: userInfo)
                PQMineViewModel.userStatus { _, _ in
                    completeHander(response! as? [String: Any], nil)
                }
            } else {
                completeHander(nil, error?.msg)
                
            }
        }
    }

    /// 手机号登录
    /// - Parameters:
    ///   - isLogin: <#isLogin description#>
    ///   - phone: <#phone description#>
    ///   - password: <#password description#>
    /// - Returns: <#description#>
    class func phoneLoginOrRegister(isLogin: Bool, phone: String, password: String, completeHander: @escaping completeHander) {
        var url: String!
        if isLogin {
            url = PQENVUtil.shared.longvideoapi + phoneLoginUrl
        } else {
            url = PQENVUtil.shared.longvideoapi + phoneRegisterUrl
        }
        SWNetRequest.postRequestData(url: url, parames: ["account": phone, "passwd": password]) { response, _, error, _ in
            if error != nil {
                completeHander(nil, error?.msg)
          
                return
            }
            if response is NSNull || response == nil {
                completeHander(nil, "请稍后重试")
                return
            }
            let userInfo: [String: Any] = response as! [String: Any]
            // 处理用户登录信息
            dealWithLoginInfo(userInfo: userInfo)
            PQMineViewModel.userStatus { _, _ in
                completeHander(response! as? [String: Any], nil)
            }
        }
    }

    /// 退出登录
    /// - Returns: description
   public class func quitLogin(completeHander: @escaping completeHander) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + logoutUrl, parames: nil) { _, _, _, _ in
            // 取消别名绑定

            BFLoginUserInfo.shared.resetData(isClear: true)
            completeHander(nil, nil)
       
        }
    }

    /// Apple账号登录
    /// - Parameters:
    ///   - token: token
    /// - Returns: <#description#>b
    class func appleLogin(token: String, completeHander: @escaping completeHander) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + appleLoginUrl, parames: ["token": token]) { response, _, error, _ in
            if error != nil {
                completeHander(nil, error?.msg)
                return
            }
            if response is NSNull || response == nil {
                completeHander(nil, "请稍后重试")
                return
            }
            let userInfo: [String: Any] = response as! [String: Any]
            // 处理用户登录信息
            dealWithLoginInfo(userInfo: userInfo)
            PQMineViewModel.userStatus { _, _ in
                completeHander(response! as? [String: Any], nil)
            }
        }
    }
    
    
    /// 处理用户登录信息
    /// - Parameter userInfo: <#userInfo description#>
    /// - Returns: <#description#>
    class func dealWithLoginInfo(userInfo: [String: Any]) {
        BFLoginUserInfo.shared.updateData(userInfo: userInfo)

        saveUserDefaults(key: cUserInfoStorageKey, value: BFLoginUserInfo.shared.toString())
    
      
    }

    /// 获取用户状态 1有效，2 已删除，3 已屏蔽，4 敏感
    /// - Parameter completeHander: <#completeHander description#>
    /// - Returns: <#description#>
    class func userStatus(completeHander: @escaping (_ status: Int?, _ msg: String?) -> Void) {
        if !BFLoginUserInfo.shared.isLogin() {
            completeHander(nil, "未登录")
            return
        }
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + userStatusUrl, parames: nil) { response, _, error, _ in
            if error != nil {
                completeHander(nil, error?.msg)
                return
            }
            if response is NSNull || response == nil {
                completeHander(nil, "请稍后重试")
                return
            }
            BFLoginUserInfo.shared.userStatus = "\(response ?? "1")"
            completeHander(Int("\(response ?? "1")"), nil)
        }
    }
    
    /// 发送验证码
    /// - Parameters:
    ///   - phoneNumber: <#phoneNumber description#>
    ///   - completeHander: <#completeHander description#>
    /// - Returns: <#description#>
    class func sendIndetifyCode(phoneNumber: String, completeHander: @escaping (_ isSuccess: Bool, _ msg: String?, _ codeId: String?) -> Void) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + sendIndetifyCodeUrl, parames: ["uid": BFLoginUserInfo.shared.uid, "phoneNumber": phoneNumber]) { response, _, error, _ in
            if error != nil {
                completeHander(false, error?.msg, nil)
                return
            }
            if response is NSNull || response == nil {
                completeHander(false, "请稍后重试", nil)
                return
            }
            let codeId: String? = "\(response ?? "")"
            completeHander(true, "短信验证码已发送", codeId)
        }
    }
    
    /// 检查手机号
    /// - Parameters:
    ///   - phoneNumber: <#phoneNumber description#>
    ///   - codeId: <#codeId description#>
    ///   - indetifyCode: <#indetifyCode description#>
    ///   - completeHander: <#completeHander description#>
    /// - Returns: <#description#>
    class func checkIndetifyCode(phoneNumber _: String, codeId: String, indetifyCode: String, completeHander: @escaping (_ isSuccess: Bool, _ msg: String?) -> Void) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + checkIndetifyCodeUrl, parames: ["uid": BFLoginUserInfo.shared.uid, "indetifyCode": indetifyCode, "codeId": codeId]) { response, _, error, _ in
            if error != nil {
                completeHander(false, error?.msg)
                return
            }
            if response is NSNull || response == nil {
                completeHander(false, "请稍后重试")
                return
            }
            completeHander(true, "验证码验证成功")
        }
    }
    
    /// 修改手机号
    /// - Parameters:
    ///   - phoneNumber: <#phoneNumber description#>
    ///   - completeHander: <#completeHander description#>
    /// - Returns: <#description#>
    class func updatePhone(phoneNumber: String, completeHander: @escaping (_ isSuccess: Bool, _ msg: String?) -> Void) {
        SWNetRequest.postRequestData(url: PQENVUtil.shared.longvideoapi + updatePhoneUrl, parames: ["phoneNum": phoneNumber]) { _, _, error, _ in
            if error != nil {
                completeHander(false, error?.msg)
                return
            }
            completeHander(true, "修改手机号成功")
        }
    }
}
