package org.koitharu.kotatsu.reader.ui.colorfilter

import android.content.Context
import android.content.DialogInterface
import androidx.activity.OnBackPressedCallback
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.koitharu.kotatsu.R

class ColorFilterConfigBackPressedDispatcher(
	private val context: Context,
	private val viewModel: ColorFilterConfigViewModel,
) : OnBackPressedCallback(true), DialogInterface.OnClickListener {

	override fun handleOnBackPressed() {
		if (viewModel.isChanged) {
			showConfirmation()
		} else {
			viewModel.onDismiss.call(Unit)
		}
	}

	override fun onClick(dialog: DialogInterface, which: Int) {
		when (which) {
			DialogInterface.BUTTON_NEGATIVE -> viewModel.onDismiss.call(Unit)
			DialogInterface.BUTTON_NEUTRAL -> dialog.dismiss()
			DialogInterface.BUTTON_POSITIVE -> viewModel.save()
		}
	}

	private fun showConfirmation() {
		MaterialAlertDialogBuilder(context)
			.setTitle(R.string.color_correction)
			.setMessage(R.string.text_unsaved_changes_prompt)
			.setNegativeButton(R.string.discard, this)
			.setNeutralButton(android.R.string.cancel, this)
			.setPositiveButton(R.string.save, this)
			.show()
	}
}
