package com.qimai.android.widgetlib.popup

import android.content.Context
import android.graphics.Point
import android.view.Gravity
import android.view.View
import android.widget.PopupWindow
import com.qimai.android.tools.dpTopx
import com.qimai.android.tools.getScreenWidth

/**
 *   created by wangwei   ON 2019-08-28  email:wangwei_5521@163.com
 *   @version 1.1.1
 *   @Description
 **/
class CommonPopWindow private constructor(
    override var context: Context,
    private val contentView: View,
    private var animationStyles: Int,
    override var gravity: Int,
    override var mDismissListener: PopupWindow.OnDismissListener?


) : QMBasePopup(context) {


    private constructor(builder: Builder) : this(
        context = builder.mContentView.context,
        contentView = builder.mContentView,
        animationStyles = builder.animationStyle,
        gravity = builder.gravity,
        mDismissListener = builder.dismissListener
    ) {
        this.setContentView(contentView)
        this.setAnimationStyle(animationStyles)
        if (builder.windowWidth < 0)
            setWindowWidth(getScreenWidth(contentView.context) - context.dpTopx(30.0f))
        else
            setWindowWidth(builder.windowWidth)

        if (builder.windowHeight > 0) {
            setWindowHeight(builder.windowHeight)
        }
    }


    class Builder {

        internal lateinit var mContentView: View

        internal var gravity = Gravity.NO_GRAVITY

        internal var animationStyle = -1

        internal var dismissListener: PopupWindow.OnDismissListener? = null

        var windowWidth = -1
        var windowHeight = -1


        fun configContentView(contentView: View) = apply {
            this.mContentView = contentView
        }

        fun configWidowWith(windowWidth: Int) = apply {
            this.windowWidth = windowWidth
        }

        fun configWidowHeight(windowHeight: Int) = apply {
            this.windowHeight = windowHeight
        }


        fun configGravity(gravity: Int) = apply {
            this.gravity = gravity
        }

        fun configAnimationStyle(style: Int) = apply {
            this.animationStyle = style
        }

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

        fun build(): CommonPopWindow {

            return CommonPopWindow(this)
        }

    }


    override fun onShowBegin(parent: View, attachedView: View?): Point = Point(0, 0)
}