package com.pan_american.android.base

import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.pan_american.android.OASystem
import com.pan_american.android.R

class CustomAlertDialog(context: Context) : AlertDialog(context) {

    fun applySettings(builder: Builder) {

        builder.message?.let { setMessage(it) }
        builder.title?.let { setTitle(it) }

        builder.positiveButtonText?.let { text ->
            setButton(DialogInterface.BUTTON_POSITIVE, text, builder.positiveButtonListener)
        }

        builder.negativeButtonText?.let { text ->
            setButton(DialogInterface.BUTTON_NEGATIVE, text, builder.negativeButtonListener)
        }
    }

    override fun show() {
        super.show()
        getButton(DialogInterface.BUTTON_POSITIVE)?.setTextColor(ContextCompat.getColor(context, R.color.title_background_color))
        getButton(DialogInterface.BUTTON_NEGATIVE)?.setTextColor(ContextCompat.getColor(context, R.color.title_background_color))
        getButton(DialogInterface.BUTTON_NEUTRAL)?.setTextColor(ContextCompat.getColor(context, R.color.title_background_color))
    }

    class Builder(context: Context = OASystem.context) : AlertDialog.Builder(context, R.style.DialogStyle) {

        var message: String? = null
        var title: String? = null
        var positiveButtonText: String? = null
        var negativeButtonText: String? = null
        var positiveButtonListener: DialogInterface.OnClickListener? = null
        var negativeButtonListener: DialogInterface.OnClickListener? = null

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setPositiveButtonAndListener(text: String, listener: DialogInterface.OnClickListener?): Builder {
            this.positiveButtonText = text
            this.positiveButtonListener = listener
            return this
        }

        fun setNegativeButtonAndListener(text: String, listener: DialogInterface.OnClickListener?): Builder {
            this.negativeButtonText = text
            this.negativeButtonListener = listener
            return this
        }

        override fun create(): AlertDialog {
            val dialog = CustomAlertDialog(context)
            dialog.applySettings(this)
            return dialog
        }

        override fun show(): AlertDialog {
            val dialog = create() as CustomAlertDialog
            dialog.show()
            return dialog
        }
    }
}