package com.quansu.heifengwuliu.vmodel

import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.quansu.heifengwuliu.activity.HomeActivity
import com.quansu.heifengwuliu.model.LoginBean
import com.quansu.heifengwuliu.repository.OneLoginRepository
import com.quansu.heifengwuliu.utils.WeChatUtil
import com.quansu.heifengwuliu.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 : BViewModel<OneLoginRepository>() {

    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().lreq(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,HomeActivity::class.java)

                    }else{
                        if(res.code()==0){
                            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()


//                            BottomListSheetBuilder(repository().context)
//                                    .addItem("司机")
//                                    .addItem("货主")
//                                    .setOnSheetItemClickListener { dialog, itemView, position, tag ->
//                                        dialog.dismiss()
//                                        var type= if(builder.checkedIndex==0) "1" else "2"
//                                        Toast.makeText(getActivity(), "Item " + (position + 1), Toast.LENGTH_SHORT).show()
//                                    }
//                                    .build()
//                                    .show()



                        }
                    }
                }
                .subscribe()
    }


}
