package com.halo.assistant.fragment.reserve

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.common.utils.toResString
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.entity.ReserveModifyEntity
import com.gh.gamecenter.entity.ReserveReminderEntity
import com.gh.gamecenter.entity.ValidateCodeResponse
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.livedata.Event
import io.reactivex.disposables.CompositeDisposable

class ReserveReminderViewModel(app: Application) : AndroidViewModel(app) {

    private val repository = ReserveReminderRepository.newInstance()

    private val composeDisposable = CompositeDisposable()

    private val _reserveReminder = MutableLiveData<ReserveReminderEntity>()
    val reserveReminder: LiveData<ReserveReminderEntity> = _reserveReminder
    fun updateReserveReminder(reserveReminder: ReserveReminderEntity) {
        _reserveReminder.value = reserveReminder
    }

    private val _sendVerifyCode = MutableLiveData<Event<String>>()
    val sendVerifyCode: LiveData<Event<String>> = _sendVerifyCode

    fun sendVerifyCode(phone: String) {
        repository.sendVerifyCode(phone)
            .compose(singleToMain())
            .subscribe(object : BiResponse<ValidateCodeResponse>() {
                override fun onSuccess(data: ValidateCodeResponse) {
                    _sendVerifyCode.value = Event(data.serviceId)
                }

            }).let(composeDisposable::add)
    }

    private val _unableSmsReminderSuccessfully = MutableLiveData<Event<Unit>>()
    val unableSmsReminderSuccessfully: LiveData<Event<Unit>> = _unableSmsReminderSuccessfully
    fun unableSmsReminder(gameId: String) {
        repository.unableSmsReminder(gameId)
            .compose(singleToMain())
            .subscribe(object : BiResponse<ReserveModifyEntity>() {
                override fun onSuccess(data: ReserveModifyEntity) {
                    _unableSmsReminderSuccessfully.value = Event(Unit)
                }

            }).let(composeDisposable::add)
    }

    /**
     * 由于可能存在用户点击了自动下载立马就退出dialog，这个方法仍然需要执行完毕
     * 所以这里当 预约提醒弹窗 退出时，仍然继续执行
     */
    fun enableAutoDownload(game: GameEntity?, enable: Boolean) {
        repository.enableAutoDownload(game, enable)
    }

    /**
     * 日历添加事件
     * 如果在日历添加期间，用户退出当前页面，操作仍然继续
     * 所以，这里不和ui层生命周期绑定
     */
    @SuppressLint("CheckResult")
    fun addCalendarEvent(gameId: String, calendarConfig: ReserveReminderEntity.CalendarConfig) {
        repository.addCalendarEvent(gameId, calendarConfig)
            .compose(singleToMain())
            .subscribe(object : BiResponse<Long>() {
                override fun onSuccess(data: Long) {
                    ToastUtils.showToast(com.gh.gamecenter.common.R.string.add_calendar_event_successfully.toResString())
                    val reminderEntity = _reserveReminder.value ?: return
                    reminderEntity.calendarConfig = ReserveReminderEntity.CalendarConfig(_notice = true)
                    updateReserveReminder(reminderEntity)
                }
            })

    }



    override fun onCleared() {
        composeDisposable.clear()
    }


}