package mtp.polymer.com.autowidget.utils

import android.app.Dialog

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.StatusLayout
import mtp.polymer.com.autowidget.dialog.showErrorDialog
import mtp.polymer.com.autowidget.dialog.showSuccessDialog


fun Dialog.bindTaskStatus(context: Context, taskUiModelData: LiveData<TaskUiModel>) {
    taskUiModelData.bindOptional(context) { taskUiModel ->
        when (taskUiModel?.taskStatus) {
            TaskStatus.IN_FLIGHT -> show()
            TaskStatus.FAILURE -> dismiss().also {
                (context as? AppCompatActivity)?.showErrorDialog {
                    setTipWord(taskUiModel.msg)
                }
            }
            TaskStatus.SUCCESS -> dismiss().also {
                (context as? AppCompatActivity)?.showSuccessDialog {
                    setTipWord(taskUiModel.msg)
                }
            }
            else -> Unit
        }
    }
}

fun StatusLayout.bindTaskStatus(taskUiModelData: LiveData<TaskUiModel>) {
    taskUiModelData.bindOptional(context) {taskUiModel ->
        when (taskUiModel?.taskStatus) {
            TaskStatus.IN_FLIGHT -> showLoading(hint = taskUiModel.msg)
            TaskStatus.FAILURE -> showError(hint = taskUiModel.msg)
            TaskStatus.SUCCESS -> showSuccess()
            else -> {
            }
        }
    }
}