package com.quansu.heifengwuliu.repository

import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.activity.HomeActivity
import com.quansu.heifengwuliu.model.LoginBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.quansu.heifengwuliu.utils.net.Response
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BRepository
import com.ysnows.base.utils.UiSwitch
import io.reactivex.Observable

class RegisterRepository : BRepository() {


    fun register(name: String?, mobile: String?, code: String?, type: String?, open_id: String? = null, unionid: String? = null): Observable<Response<Any>>? {
        if (name.isNullOrEmpty()) {
            toast(R.string.please_input_user_name)
            return null
        }
        if (mobile.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return null
        }
        if (code.isNullOrEmpty()) {
            toast(R.string.please_input_code)
            return null
        }

        return Observable.create { emitter ->
            lreq(NetEngine.service.register(name, mobile, code, type, open_id,unionId = unionid))
                    .doOnNext() {
                        if (it.ok(true)) {
                            var bean: LoginBean = it.data() as LoginBean;

                            MMKV.defaultMMKV().putString("token", bean.TOKEN)
                            UiSwitch.single(context, HomeActivity::class.java)
//                            emitter.onNext(it)
                        }
                    }
                    .subscribe()

        }
    }


}
