package com.zhaohaoting.framework.abs.menu;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.zhaohaoting.framework.R;


/**
 * author：Created by ZHT on 2017/12/1 11:48
 * email：526309416@qq.com
 * desc：菜单window
 */
public abstract class MenuPopupWindow extends PopupWindow {
    protected Activity context;
    private View windowView;

    public MenuPopupWindow(Activity context) {
        this.context = context;
        initWindowView();
    }

    private void initWindowView() {
        windowView = context.getLayoutInflater().inflate(R.layout.menu_window_view, null);
        windowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuPopupWindow.this.dismiss();
            }
        });
        FrameLayout contentView = (FrameLayout) windowView.findViewById(R.id.content_view);
        contentView.addView(View.inflate(context, getLayoutRes(), null));
        this.setContentView(windowView);
        initContentView(windowView);
        this.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 1f;
        context.getWindow().setAttributes(lp);      
        // 刷新状态
        this.update();
        this.setBackgroundDrawable(new ColorDrawable(0x50000000));
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        ScaleAnimation ta = new ScaleAnimation(0f, 1f, 0f, 1, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
        ta.setDuration(200);
        contentView.startAnimation(ta);
    }

    protected abstract void initContentView(View windowView);

    public void show() {
        this.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }

    protected abstract int getLayoutRes();
}
