package com.zswc.creativity.repository

import android.app.Activity
import com.hwangjr.rxbus.RxBus
import com.zswc.creativity.R
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.model.LoginBean
import com.zswc.creativity.utils.net.NetEngine
import com.ysnows.base.net.Resp
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<Resp<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 ->
            rxLreq(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, MainActivity::class.java)
                            RxBus.get().post(ConfigRx.CLOSE_LOGIN, "")

                            (context as Activity).finish()

//                            emitter.onNext(it)
                        }
                    }
                    .subscribe()

        }
    }


}
