package com.zswc.creativity.repository

import android.annotation.SuppressLint
import android.app.Activity
import com.hwangjr.rxbus.RxBus
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.zswc.creativity.R
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.model.LoginBean
import com.zswc.creativity.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BRepository
import com.ysnows.base.utils.UiSwitch


class LoginRepository : BRepository() {
    @SuppressLint("CheckResult")
    fun login(mobile: String?, passWord: String?) {

        if (mobile.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return
        }
        if (passWord.isNullOrEmpty()) {
            toast(R.string.please_input_pwd)
            return
        }

        rxLreq(NetEngine.service.login(mobile, passWord))
                .doOnNext() {
                    if (it.ok(true)) {
                        var bean: LoginBean =it.data() as LoginBean
                        MMKV.defaultMMKV().putString("token",bean.TOKEN)
                        UiSwitch.single(context, MainActivity::class.java)

                        RxBus.get().post(ConfigRx.CLOSE_LOGIN, "")
                        (context as Activity).finish()

                    }
                }
                .subscribe()

    }

    @SuppressLint("CheckResult")
    fun loginCode(mobile: String?, code: String?) {

        if (mobile.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return
        }
        if (code.isNullOrEmpty()) {
            toast(R.string.please_input_code)
            return
        }
        rxLreq(NetEngine.service.smsLogin(mobile, code))
                .doOnNext() {
                    if (it.ok(true)) {
                        var bean: LoginBean =it.data() as LoginBean
                        MMKV.defaultMMKV().putString("token",bean.TOKEN)
                        UiSwitch.single(context, MainActivity::class.java)
                        RxBus.get().post(ConfigRx.CLOSE_LOGIN, "")

                        (context as Activity).finish()

                    }
//                    else{
//                        if(it.code()==0){
//                            smsDialogShow(mobile)
//                        }
//                    }
                }
                .subscribe()

    }

    fun smsDialogShow(mobile: String){
        val items = arrayOf("司机", "货主")
        val builder = QMUIDialog.CheckableDialogBuilder(context)
        builder.setTitle("选择类型去注册")
                .setCheckedIndex(0)
                .addItems(items) { dialog, which -> builder.checkedIndex = which }
                .addAction("取消") { dialog, index ->
                    dialog.dismiss()
                }
                .addAction("确定") { dialog, index ->
                    var type= if(builder.checkedIndex==0) "1" else "2"
                    simLogin(mobile,type);
                    dialog.dismiss()
                }
                .show()


    }

    @SuppressLint("CheckResult")
    fun simLogin(mobile:String,type: String) {

        rxLreq(NetEngine.service.simlRegister(mobile,type))
                .doOnNext{
                    if (it.ok(true)) {
                        var bean: LoginBean =it.data() as LoginBean;
                        MMKV.defaultMMKV().putString("token",bean.TOKEN)
                        UiSwitch.single(context, MainActivity::class.java)
                        RxBus.get().post(ConfigRx.CLOSE_LOGIN, "")

                        (context as Activity).finish()

                    }
                }
                .subscribe();

    }


}
