package com.quansu.heifengwuliu.repository

import android.annotation.SuppressLint
import android.os.Bundle
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.UiSwitch

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

        lreq(NetEngine.service.wxLogin(code))
                .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, Bundle().apply {
                                putString("openid", it.data()?.openid)
                                putString("unionid", it.data()?.unionid)
                            })
                        }
                    }
                }
                .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();

    }


}
