package com.quansu.heifengwuliu.repository

import android.annotation.SuppressLint
import com.quansu.heifengwuliu.activity.HomeActivity
import com.quansu.heifengwuliu.activity.RegisterActivity
import com.quansu.heifengwuliu.model.LoginBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BRepository
import com.ysnows.base.utils.B
import com.ysnows.base.utils.UiSwitch

class OneLoginRepository : BRepository() {
    @SuppressLint("CheckResult")
    fun login(token: String) {

        lreq(NetEngine.service.wxLogin(token))
                .doOnError(){
                   it.printStackTrace()
                }
                .doOnNext {
                    if (it.ok(true)) {

                        val bean: LoginBean = it.data() as LoginBean;
                        MMKV.defaultMMKV().putString("token", bean.TOKEN)

                        UiSwitch.single(context, HomeActivity::class.java)

                    } else {
                        if (it.code() == 0) {

                            UiSwitch.bundle(context, RegisterActivity::class.java, B.with().putString("open_id", token).ok())
                        }
                    }
                }
                .subscribe();

    }


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

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

    }


}
