package com.miya.fastcashier.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.widget.TextView
import android.text.SpannableString
import android.view.Gravity
import com.miya.fastcashier.R
import android.os.Bundle
import android.text.TextUtils
import android.view.ViewGroup.MarginLayoutParams
import android.text.method.LinkMovementMethod
import android.view.View
import com.miya.fastcashier.utils.clickWithTrigger

class CommonDialog : Dialog {
    protected var tvMessage: TextView? = null
    protected var tvNegative: TextView? = null
    protected var tvPositive: TextView? = null
    protected var onClickListener: OnDialogClickListener? = null
    protected var mContext: Context
    private var mCustomMessage = ""
    private var mContentSpanStr: SpannableString? = null
    private var mPositiveStr = ""
    private var mNegativeStr = ""
    private var mMessageGravity = Gravity.LEFT
    private var contentLeftMargin = 0
    private var contentRightMargin = 0
    private var contentTopMargin = 0
    private var contentBottomMargin = 0

    interface OnDialogClickListener {
        fun onNegativeClick()
        fun onPositiveClick()
    }

    fun setOnClickListener(onClickListener: OnDialogClickListener?): CommonDialog {
        this.onClickListener = onClickListener
        return this
    }

    constructor(ctx: Context, theme: Int) : super(ctx, theme) {
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        mContext = ctx
    }

    constructor(ctx: Context) : super(ctx, R.style.CommonDialog) {
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        mContext = ctx
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.dialog_common)
        tvMessage = findViewById(R.id.tvMessage)
        tvNegative = findViewById(R.id.tvNegative)
        tvPositive = findViewById(R.id.tvPositive)
        var vNegative: View = findViewById(R.id.vNegative)
        var vPositive: View = findViewById(R.id.vPositive)
        vNegative?.clickWithTrigger {
            if (onClickListener != null) {
                onClickListener!!.onNegativeClick()
            }
        }
        vPositive?.clickWithTrigger {
            if (onClickListener != null) {
                onClickListener!!.onPositiveClick()
            }
        }

        if (!TextUtils.isEmpty(mCustomMessage)) {
            tvMessage?.setText(mCustomMessage)
            val marginLayoutParams = tvMessage?.getLayoutParams() as MarginLayoutParams
            if (contentTopMargin != 0) {
                marginLayoutParams.topMargin = contentTopMargin
            }
            if (contentBottomMargin != 0) {
                marginLayoutParams.bottomMargin = contentBottomMargin
            }
            if (contentLeftMargin != 0) {
                marginLayoutParams.leftMargin = contentLeftMargin
            }
            if (contentRightMargin != 0) {
                marginLayoutParams.rightMargin = contentRightMargin
            }
            tvMessage?.getViewTreeObserver()?.addOnGlobalLayoutListener {
                val textPaint = tvMessage?.getPaint()
                textPaint?.textSize = tvMessage!!.getTextSize()
                val textViewWidth = textPaint!!.measureText(mCustomMessage).toInt()
                tvMessage?.setText(mCustomMessage)
                if (textViewWidth > tvMessage!!.getWidth()) {
                    tvMessage?.setGravity(Gravity.LEFT)
                } else {
                    tvMessage?.setGravity(Gravity.CENTER)
                }
            }
        } else {
            if (mContentSpanStr != null) {
                tvMessage?.setText(mContentSpanStr)
                tvMessage?.setMovementMethod(LinkMovementMethod.getInstance())
                tvMessage?.setHighlightColor(Color.TRANSPARENT)
                val marginLayoutParams = tvMessage?.getLayoutParams() as MarginLayoutParams
                if (contentTopMargin != 0) {
                    marginLayoutParams.topMargin = contentTopMargin
                }
                if (contentBottomMargin != 0) {
                    marginLayoutParams.bottomMargin = contentBottomMargin
                }
                if (contentLeftMargin != 0) {
                    marginLayoutParams.leftMargin = contentLeftMargin
                }
                if (contentRightMargin != 0) {
                    marginLayoutParams.rightMargin = contentRightMargin
                }
            }
            tvMessage?.setGravity(mMessageGravity)
        }
        if (!TextUtils.isEmpty(mPositiveStr)) {
            tvPositive?.setText(mPositiveStr)
        }
        if (!TextUtils.isEmpty(mNegativeStr)) {
            tvNegative?.setText(mNegativeStr)
        }

        resize()
    }

    fun setCustomMessage(message: String): CommonDialog {
        mCustomMessage = message
        return this
    }

    fun setCustomMessage(contentStr: SpannableString): CommonDialog {
        mContentSpanStr = contentStr
        return this
    }

    fun setMessageGravity(gravity: Int): CommonDialog {
        mMessageGravity = gravity
        return this
    }

    fun setPositiveStr(positiveStr: String): CommonDialog {
        mPositiveStr = positiveStr
        return this
    }

    fun setNegativeStr(negativeStr: String): CommonDialog {
        mNegativeStr = negativeStr
        return this
    }

    fun setTvMessageMargin(
        leftMargin: Int,
        rightMargin: Int,
        topMargin: Int,
        bottomMargin: Int
    ): CommonDialog {
        contentLeftMargin = leftMargin
        contentRightMargin = rightMargin
        contentTopMargin = topMargin
        contentBottomMargin = bottomMargin
        return this
    }

    fun show(gravity: Int) {
        this.window!!.setGravity(gravity)
        this.show()
    }

    fun resize() {
        this.window!!.setLayout(-1, -2)
    }
}