package com.zswc.creativity.repository

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.activity.RegisterActivity
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 OneLoginRepository : BRepository() {
    @SuppressLint("CheckResult")
    fun login(code: String) {

        rxLreq(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, MainActivity::class.java)

                        (context as Activity).finish()

                    } 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) {

        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)
                        (context as Activity).finish()

                    }
                }
                .subscribe();

    }


}
