package com.genlot.ninglottery.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;


import com.genlot.ninglottery.R;

import java.util.ArrayList;
import java.util.List;

public class MenuPopWindow extends PopupWindow implements View.OnClickListener {
    private View parentView;
    private Context mContext;
    private IOnBtnListener listener;
    private static MenuPopWindow instance;
    private List<Button> mButtons = new ArrayList<>();

    public static MenuPopWindow getInstance(Context context) {
        if (instance == null) {
            synchronized (MenuPopWindow.class) {
                if (instance == null) {
                    instance = new MenuPopWindow(context);
                }
            }
        }
        return instance;
    }

    public MenuPopWindow(Context context) {
        super(context);
        mContext = context;
        parentView = View.inflate(context, R.layout.menu_pop_view, null);
        mButtons.add(parentView.findViewById(R.id.rb_rpt));
        mButtons.add(parentView.findViewById(R.id.rb_function));
        mButtons.add(parentView.findViewById(R.id.rb_info));
        mButtons.add(parentView.findViewById(R.id.rb_hardware_check));
        mButtons.add(parentView.findViewById(R.id.rb_lock));
        mButtons.add(parentView.findViewById(R.id.rb_logout));
        mButtons.add(parentView.findViewById(R.id.rb_close));
        for (Button btn : mButtons) btn.setOnClickListener(this);
        setContentView(parentView);
        setPopConfig();
    }


    private void setPopConfig() {
        this.setWidth(180);
        this.setHeight(520);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
        this.setBackgroundDrawable(colorDrawable);
        // 设置外部触摸会关闭窗口
        this.setOutsideTouchable(true);
        //获取自身的长宽高
        parentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    }

    public void show(View view) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        showAsDropDown(view, Gravity.NO_GRAVITY, location[0], location[1]);
    }


    @Override
    public void onClick(View view) {
        if (listener != null) listener.onBtnListener(view.getId());
    }

    public interface IOnBtnListener {
        void onBtnListener(int id);
    }

    public void setBtnClick(IOnBtnListener listener) {
        this.listener = listener;
    }
}