package com.zswc.creativity.vmodel

import android.app.Activity
import android.app.Application
import androidx.constraintlayout.widget.ConstraintLayout
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.inte.ChoiceData
import com.zswc.creativity.model.LoginBean
import com.zswc.creativity.repository.OneLoginRepository
import com.zswc.creativity.utils.IdentityDialogUtils
import com.zswc.creativity.utils.WeChatUtil
import com.zswc.creativity.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.ysnows.base.base.BViewModel
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.route.IJpush
import com.ysnows.base.utils.UiSwitch


open class OneLoginVModel(application: Application) : BViewModel<OneLoginRepository>(application) {

    var cl: ConstraintLayout? =null
    fun clickLogin() {


        AndPermission.with(repository().context)
                .runtime()
                .permission(Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE,
                        Permission.READ_PHONE_STATE
                )
                .onGranted { data ->
                    //手机号一键登录
                    CCRetrofit.instance()
                            .with(repository().context)
                            .create(IJpush::class.java)
                            .isLogin()
                            .subscribe()

                }
                .start()
    }


    fun login(token: String) {
        repository().login(token)
    }

    fun wxLogin() {

        //微信登录
        WeChatUtil.setWXLoigin(repository().context)

    }


    fun simLogin(mobile: String, type: String) {
        repository().simLogin(mobile, type)
    }

    fun oneLogin(code: String) {

        repository().rxLreq(NetEngine.service.jiguangLogin(code))
                .doOnNext() { res ->
                    if (res.ok(true)) {
                        //保存token

                        var bean: LoginBean = res.data() as LoginBean;

                        MMKV.defaultMMKV().putString("token", bean.TOKEN)
                        UiSwitch.single(repository().context, MainActivity::class.java)
                        (repository().context as Activity).finish()
                    } else {
                        if (res.code() == 0) {

                            var bean: LoginBean = res.data() as LoginBean

                            IdentityDialogUtils.shoChooseIdentity(repository().context!!, cl!!, object : ChoiceData {

                                override fun onRestuse(type: String?) {

                                    simLogin(bean.mobile, type!!);

                                }

                            })


//                            var bean: LoginBean = res.data() as LoginBean;
//                            val items = arrayOf("司机", "货主")
//                            val builder = QMUIDialog.CheckableDialogBuilder(repository().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(bean.mobile, type);
//                                        dialog.dismiss()
//                                    }
//                                    .show()


//

                        }
                    }
                }
                .subscribe()
    }

    override fun createRepository(): OneLoginRepository {
        return OneLoginRepository()
    }

}
