package com.qimai.android.widgetlib.popup

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import com.qimai.android.widgetlib.R

/**
 *   created by wangwei   ON 2019-08-28  email:wangwei_5521@163.com
 *   @version 1.1.1
 *   @Description
 *
 *   kotlin builder <a href ="https://dev.to/rahulpr27/teach-me-builder-pattern-in-kotlin-3pl8">builder</a>
 *  为什么没有这种方式 为了兼容java 调用方式
 **/
class QmAlertDialog private constructor(
    private val title: CharSequence,
    private val subTitle: CharSequence,
    private val leftButtonText: String,
    private val rightButtonText: String,
    private val leftButtonClickListener: (CommonPopWindow: CommonPopWindow) -> Unit,
    private val rightButtonClickListener: (CommonPopWindow: CommonPopWindow) -> Unit,
    private val rootView: ViewGroup,
    private val dismissListener: PopupWindow.OnDismissListener?
    , private val width: Int
) {

    private lateinit var commonPopWindow: CommonPopWindow

    constructor(builder: Builder) : this(
        title = builder.title,
        subTitle = builder.subTitle,
        leftButtonText = builder.leftButtonText,
        rightButtonText = builder.rightButtonText,
        leftButtonClickListener = builder.leftButtonClick,
        rightButtonClickListener = builder.rightButtonClick,
        rootView = builder.rootView,
        dismissListener = builder.dismissListener,
        width = builder.width
    )

    fun show() {
        if (!::commonPopWindow.isInitialized) {
            val contentView =
                LayoutInflater.from(rootView.context)
                    .inflate(R.layout.qm_alert_content, rootView, false)

            val titleView = contentView.findViewById<TextView>(R.id.alert_title)
            val subTitleView = contentView.findViewById<TextView>(R.id.alert_subtitle)
            val cancel = contentView.findViewById<TextView>(R.id.alert_cancel)
            val confirm = contentView.findViewById<TextView>(R.id.alert_confirm)
            if (!TextUtils.isEmpty(title)) {
                titleView.text = title
            } else {
                titleView.visibility = View.GONE
            }
            if (!TextUtils.isEmpty(subTitle)) {
                subTitleView.text = subTitle
            } else {
                subTitleView.visibility = View.GONE
            }

            cancel.text = leftButtonText
            confirm.text = rightButtonText

            cancel.setOnClickListener {
                this.leftButtonClickListener.invoke(commonPopWindow)
            }

            confirm.setOnClickListener {
                this.rightButtonClickListener.invoke(commonPopWindow)
            }

            val builder = CommonPopWindow.Builder()
                .configGravity(Gravity.CENTER)
                .configContentView(contentView)
                .dismissListener(PopupWindow.OnDismissListener {
                    dismissListener?.onDismiss()
                })
            if (width >0){
                builder.configWidowWith(width)
            }
            commonPopWindow = builder.build()

        }
        commonPopWindow.show(rootView)
        commonPopWindow.dimBehind(0.5f)

    }

    fun dismiss() {
        if (::commonPopWindow.isInitialized) {
            commonPopWindow.dismiss()
        }
    }


    class Builder {

        internal var title: CharSequence = ""
        internal var subTitle: CharSequence = ""

        internal var leftButtonText = "取消"
        internal var rightButtonText = "确定"

        internal lateinit var leftButtonClick: (CommonPopWindow: CommonPopWindow) -> Unit
        internal lateinit var rightButtonClick: (CommonPopWindow: CommonPopWindow) -> Unit
        private lateinit var context: Context
        internal lateinit var rootView: ViewGroup
        internal var dismissListener: PopupWindow.OnDismissListener? = null
        internal var width: Int = 0


        fun withTitle(title: CharSequence) = apply {
            this.title = title
        }

        fun withDismissListener(dimissListener: PopupWindow.OnDismissListener) = apply {
            this.dismissListener = dimissListener
        }

        fun withContext(context: Context) = apply {
            this.context = context
        }


        fun withSubTitle(subTitle: CharSequence) = apply {
            this.subTitle = subTitle
        }

        fun withRootView(rootView: ViewGroup) = apply {
            this.rootView = rootView
        }

        fun withWidth(windowWidth: Int) = apply {
            this.width = windowWidth

        }


        fun build(): QmAlertDialog {
            return QmAlertDialog(this)
        }


        fun withLeftButton(
            leftButtonText: String,
            clickListener: (CommonPopWindow: CommonPopWindow) -> Unit
        ) =
            apply {

                this.leftButtonText = leftButtonText
                leftButtonClick = clickListener
            }


        fun withRightButton(
            rightButtonText: String,
            clickListener: (CommonPopWindow: CommonPopWindow) -> Unit
        ) = apply {

            this.rightButtonText = rightButtonText
            this.rightButtonClick = clickListener
        }

    }


}