package com.mq.mylibrary.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.mq.mylibrary.R;
import com.mq.mylibrary.tool.DPTool;


/**
 * 创建人 mQ
 * 创建时间 2018/12/14 0014 16:54
 * 文件名称 BasePopupWindow
 * 说明:
 **/
public abstract class BasePopupWindow {

    private int mLeft;

    protected View mView;

    protected abstract View creatView();

    private PopupWindow mPopupWindow;
    public Context mContext;

    private boolean widthMatch = false;

    public BasePopupWindow(Context context) {
        mContext = context;
    }

    protected void initPopupWindow(Context context) {
        mLeft = DPTool.dp2px(mContext, 16);

        View view = LayoutInflater.from(mContext).inflate(R.layout.pop_container_layout, null);
        CardView cardView = (CardView) view.findViewById(R.id.cardView);
        cardView.addView(creatView());
        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    public BasePopupWindow setOnDissmissListener(PopupWindow.OnDismissListener dissmissListener) {
        mPopupWindow.setOnDismissListener(dissmissListener);
        return this;
    }

    public void show(View view) {

        mView = view;
        if (widthMatch) {
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            mPopupWindow.setWidth(view.getWidth());
        }
//        mPopupWindow.showAsDropDown(view, -mLeft, 0);
        mPopupWindow.showAsDropDown(view);
    }

    public void dismiss() {
        mPopupWindow.dismiss();
    }

    public void setWidthMatch() {
        widthMatch = true;
    }

}

