package nice3point.by.schedule.ScheduleDialogChanger

import android.view.View
import nice3point.by.schedule.iUpdateViewPager
import org.jetbrains.annotations.Contract

class PScheduleChanger(private val view: iVScheduleChanger) : iPScheduleChanger {

    private val model: iMScheduleChanger = MScheduleChanger(this)
    private lateinit var uiListener: iUpdateViewPager

    override fun createAppendDialog(uiListener: iUpdateViewPager) {
        this.uiListener = uiListener
        view.switchDeleteIconVisibility(View.INVISIBLE)
        view.setConfirmButtonListener(addItemListener)
    }

    override fun createEditDialog(uiListener: iUpdateViewPager, teacher: String, subject: String, cabinet: String, time: String, type: String) {

        this.uiListener = uiListener

        val unzipTime = splitTime(time)
        view.setTextViewData(teacher, subject, cabinet, unzipTime[0], unzipTime[1], type)
        view.setConfirmButtonListener(getEditItemListener(teacher, subject, cabinet, time, type))
    }

    override fun deleteButtonPressed(databaseName: String, teacher: String, subject: String, cabinet: String, time: String, type: String) {
        view.delBtEdit()
        view.setConfirmButtonListener(getDeleteListener(databaseName, teacher, subject, cabinet, time, type))
    }

    private val addItemListener: View.OnClickListener
        @Contract(pure = true)
        get() {
            val changerView = this.view
            return View.OnClickListener {
                val bundle = changerView.getTextViewData()

                if (bundle[0]!!.isEmpty()) {
                    changerView.closeDialog()
                } else {
                    model.addItemToRealm(bundle[0], bundle[1], bundle[2], bundle[3], bundle[4], bundle[5], bundle[6])
                    uiListener.refreshUI()
                    changerView.closeDialog()
                }
            }
        }

    @Contract(pure = true)
    private fun getEditItemListener(teacher: String, subject: String, cabinet: String,
                                    time: String, type: String): View.OnClickListener {
        val changerView = this.view
        return View.OnClickListener {
            val bundle = changerView.getTextViewData()
            model.editRealmItem(bundle, teacher, subject, cabinet, time, type)
            uiListener.refreshUI()
            changerView.closeDialog()
        }
    }

    private fun getDeleteListener(databaseName: String, teacher: String, subject: String, cabinet: String, time: String, type: String): View.OnClickListener {
        val changerView = this.view
        return View.OnClickListener {
            model.deleteRealmItem(databaseName, teacher, subject, cabinet, time, type)
            uiListener.refreshUI()
            changerView.closeDialog()
        }
    }


    private fun splitTime(time: String): Array<String> {

        val massive = time.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        return when {
            massive.size == 1 -> arrayOf(massive[0], "")

            time.isEmpty() -> arrayOf("", "")

            else -> time.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        }
    }

    override fun dialogIsDead() {
        model.closeRealm()
    }

    override fun showSnackBar(text: String) {
        uiListener.showSnackBar(text)
    }
}
