package com.gh.gamecenter.teenagermode

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import com.gh.gamecenter.common.utils.createRequestBody
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody

class TeenagerModeViewModel(application: Application) : AndroidViewModel(application) {

    @SuppressLint("CheckResult")
    fun postTeenModeLock(pwd: String, successCb: () -> Unit) {
        val body = hashMapOf(
            Pair("android_id", HaloApp.getInstance().gid),
            Pair("password", pwd),
            Pair("password_again", pwd)
        ).createRequestBody()
        RetrofitManager.getInstance().api.postTeenModeLock(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    successCb.invoke()
                }
            })
    }

    @SuppressLint("CheckResult")
    fun postTeenModeUnlock(pwd: String, successCb: () -> Unit, failureCb: () -> Unit) {
        val body = hashMapOf(
            Pair("android_id", HaloApp.getInstance().gid),
            Pair("password", pwd)
        ).createRequestBody()
        RetrofitManager.getInstance().api.postTeenModeUnlock(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    successCb.invoke()
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    failureCb.invoke()
                }
            })
    }

    @SuppressLint("CheckResult")
    fun postTeenModeLogout(pwd: String, successCb: () -> Unit, failureCb: () -> Unit) {
        val body = hashMapOf(
            Pair("android_id", HaloApp.getInstance().gid),
            Pair("password", pwd)
        ).createRequestBody()
        RetrofitManager.getInstance().api.postTeenModeLogout(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    successCb.invoke()
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    failureCb.invoke()
                }
            })
    }

    @SuppressLint("CheckResult")
    fun putTeenModePwd(pwd: String, newPwd: String, successCb: () -> Unit, failureCb: () -> Unit) {
        val body = hashMapOf(
            Pair("last_password", pwd),
            Pair("android_id", HaloApp.getInstance().gid),
            Pair("password", newPwd),
            Pair("password_again", newPwd)
        ).createRequestBody()
        RetrofitManager.getInstance().api.putTeenModePassword(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    successCb.invoke()
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    failureCb.invoke()
                }
            })
    }
}