package com.miya.fastcashier.ui.dialog

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.Gravity
import android.view.WindowManager
import android.widget.TextView
import com.miya.fastcashier.R
import com.miya.fastcashier.utils.clickWithTrigger

class MessageDialog(context: Context) : Dialog(context){

    private lateinit var tvMessage: TextView
    private lateinit var tvNegative: TextView
    private lateinit var tvPositive: TextView

    private lateinit var mPositiveButtonListener:DialogInterface.OnClickListener
    private lateinit var mNegativeButtonListener:DialogInterface.OnClickListener

    private lateinit var mPositiveButtonText:CharSequence
    private lateinit var mNegativeButtonText:CharSequence

    init {
        initWindow()
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_message)
        tvMessage = findViewById(R.id.tvMessage)
        tvNegative = findViewById(R.id.tvNegative)
        tvPositive = findViewById(R.id.tvPositive)

        tvNegative.clickWithTrigger {
            dismiss()
        }

        tvPositive.clickWithTrigger {
            dismiss()
            mPositiveButtonListener.onClick(null,1)
        }
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(R.layout.dialog_message)
        tvMessage = findViewById(R.id.tvMessage)
        tvNegative = findViewById(R.id.tvNegative)
        tvPositive = findViewById(R.id.tvPositive)
    }

    fun setPositiveButton(text: CharSequence, listener: DialogInterface.OnClickListener): MessageDialog {
        this.mPositiveButtonText = text
        this.mPositiveButtonListener = listener
        return this
    }

    fun setNegativeButton(text: CharSequence, listener: DialogInterface.OnClickListener): MessageDialog {
        this.mNegativeButtonText = text
        this.mNegativeButtonListener = listener
        return this
    }

    fun setCustomMessage(message: CharSequence?) : MessageDialog{
        tvMessage.text = message
        return this
    }

    private fun initWindow() {
        val win = this.window
        win!!.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp.gravity = Gravity.CENTER
        lp.windowAnimations = R.style.AlphaInAndOutStyle
        win.attributes = lp
        win.setBackgroundDrawableResource(R.color.transparent)
    }

}