package com.zxbw.modulemain.presenter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.zxbw.modulemain.contract.LoginContract
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.uc.UserInfo
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.UserDataUtils

/**
 * Created by gaoleichao on 2018/6/20.
 */
class LoginPresenter:LoginContract.Presenter{

    private val mView: LoginContract.View

    constructor(mView: LoginContract.View) {
        this.mView = mView
    }
    override fun requestCode(phone: String) {
        ApiClient.homeApi.requestCode(phone).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.smsCodeSuccess()
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    var str: String
                    if (TextUtils.equals("105", code)) {
                        str = errorMsg?:""
                    } else {
                        str = "发送失败"
                    }
                    mView.smsCodeFaild(str)
                }
            })
    }

    override fun codeLogin(phone: String, code: String) {
        ApiClient.homeApi.requestCodeLogin(phone, code).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfo>() {
                override fun onSuccess(result: UserInfo) {
                    if(result!=null){
                        val token = result.token
                        if (TextUtils.isEmpty(token)) {
                            EventUtils.onEvent("login_fail", "codeLogin:" + "token为空")
                            mView.loginFaild("登录失败")
                        } else {
                            EventUtils.onEvent("login_suc", "codeLogin")
//                        Constant.newAwardFlag = result.newAwardFlag
                            SettingPreference.saveToken(token)
                            UserDataUtils.updateUserInfo(null)
                            mView.loginSuc()
                            //    getUserInfo()
                        }
                    }else{
                        Log.e("MXL","失败")
                    }

                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String) {
                    EventUtils.onEvent("login_fail", "codeLogin:" + errorMsg)
                    val str: String

                    if (TextUtils.equals("105", code)) {
                        str = errorMsg
                    } else {
                        str = "登录失败"
                    }
                    mView.loginFaild(str)
                }
            })
    }


    override fun getUserInfo() {
        ApiClient.userInfoAPi.getUserInfo().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfo>() {
                override fun onSuccess(result: UserInfo) {
                    val uid = result.uid
                    if (TextUtils.isEmpty(uid)) {
                        EventUtils.onEvent("login_fail", "getUserInfo:" + "uid为空")
                        mView.loginFaild("登录失败")
                    } else {
                        SettingPreference.saveUserInfo(result)

//                        SettingPreference.setTrackId(result.gaodeTrackId?:0L)
//                        SettingPreference.setTerminalId(result.gaodeTerminalId?:0L)
                        mView.loginSuc()
                        EventUtils.onEvent("login_suc", "getUserInfo")
                    }
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String) {
                    EventUtils.onEvent("login_fail", "getUserInfo:" + errorMsg)
                    val str: String
                    if (TextUtils.equals("105", code)) {
                        str = errorMsg
                    } else {
                        str = "登录失败"
                    }
                    mView.loginFaild(str)
                }
            })
    }

    override fun phoneLogin(phone: String) {
//        ApiClient.userApi.requestPhoneLogin(Constant.SHANYAN_KEY,phone).compose(RxSchedulers.observableIO2Main())
//            .subscribe(object : BaseObserver<UserInfoEntity>() {
//                override fun onSuccess(result: UserInfoEntity) {
//                    val token = result.token
//                    Constant.successMsg = result?.successMsg
//                    if (TextUtils.isEmpty(token)) {
//                        EventUtils.onEvent("login_fail", "codeLogin:" + "token为空")
//                        mView.loginFaild("登录失败")
//                    } else {
//                        EventUtils.onEvent("login_suc", "codeLogin")
//                        Constant.newAwardFlag = result.newAwardFlag
//                        SettingPreference.setToken(token)
//                        getUserInfo()
//                    }
//                }
//
//                override fun onFailure(e: Throwable, code: String, errorMsg: String) {
//                    EventUtils.onEvent("login_fail", "codeLogin:" + errorMsg)
//                    val str: String
//                    if (TextUtils.equals("105", code)) {
//                        str = errorMsg
//                    } else {
//                        str = "登录失败"
//                    }
//                    mView.loginFaild(str)
//                }
//            })
    }

}