package com.ifavine.pay.view;

/**
 * Created by Administrator on 2018/10/18.
 */


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.ifavine.pay.R;


/**
 * 弹窗视图
 */
public class PopWindowMainReport extends PopupWindow implements View.OnClickListener {


    private Context context;
    private View ll_1, ll_2, ll_3;
    private CallBackPosition call;

    public PopWindowMainReport(Context context, CallBackPosition call) {
        super(context);
        this.context = context;
        this.call = call;
        initalize();
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.pop_main_report, null);
        ll_1 = view.findViewById(R.id.ll_1);//发起群聊
        ll_2 = view.findViewById(R.id.ll_2);//添加好友
        ll_3 = view.findViewById(R.id.ll_3);//添加好友
        ll_1.setOnClickListener(this);
        ll_2.setOnClickListener(this);
        ll_3.setOnClickListener(this);
        setContentView(view);
        initWindow();
    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth((int) (d.widthPixels * 0.2));
        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.6f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        // showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 10);
        showAsDropDown(view);
        //showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_1:
                dismiss();
                if (call != null) {
                    call.selectIndex(0);
                }
                break;
            case R.id.ll_2:
                dismiss();
                if (call != null) {
                    call.selectIndex(1);
                }
                break;
            case R.id.ll_3:
                dismiss();
                if (call != null) {
                    call.selectIndex(2);
                }
                break;
            default:
                break;
        }
    }

    public interface CallBackPosition {
        void selectIndex(int index);
    }
}


