package com.mytatvarnsdk.myView.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.mytatvarnsdk.R;


/**
 * 基础的底部弹出框
 *
 * @author minyuchun
 * @date 2018/12/20
 * 基础的弹出框
 */
public class BPopupWindow extends PopupWindow {
  /**
   * 依赖的activity
   */
  protected Activity mActivity;
  /**
   * 显示的页面
   */
  protected View mView;

  protected OnConfirmListener mOnConfirmListener;
  /**
   * 点击监听回调
   */
  protected OnSelectListener mOnSelectListener;

  protected OnChooseListener mOnChooseListener;

  public BPopupWindow(Activity activity, int viewId) {
    if (activity == null) {
      return;
    }
    mActivity = activity;
    dismiss();
    LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (inflater == null) {
      return;
    }
    mView = inflater.inflate(viewId, null);
    setContentView(mView);
    //设置SelectPicPopupWindow弹出窗体的宽
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //设置SelectPicPopupWindow弹出窗体的高
    setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    //设置SelectPicPopupWindow弹出窗体可点击
    setFocusable(false);
    //设置SelectPicPopupWindow弹出窗体动画效果
    setAnimationStyle(R.style.AnimBottom);
    //外部点击可取消
    setOutsideTouchable(false);
    //被继承的初始化页面其他控件
    initView();
    //在dismiss中恢复透明度
    setOnDismissListener(() -> {
      WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
      lp.alpha = 1f;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
      }
      mActivity.getWindow().setAttributes(lp);
    });
  }


  /***
   * 初始化界面
   */
  protected void initView() {
    if (mView == null) {
    }
  }

  @Override
  public void showAtLocation(View parent, int gravity, int x, int y) {
    //显示窗口 背景灰度
    if (mActivity != null) {
      WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
      lp.alpha = 0.4f;
      //不加这一行在某些手机上不能背景模糊
      lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
      mActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);
      mActivity.getWindow().setAttributes(lp);
    }
    super.showAtLocation(parent, gravity, x, y);
  }

  public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
    mOnConfirmListener = onConfirmListener;
  }

  public void setOnSelectListener(OnSelectListener onSelectListener) {
    mOnSelectListener = onSelectListener;
  }

  public void setOnChooseListener(OnChooseListener onChooseListener) {
    mOnChooseListener = onChooseListener;
  }

  /**
   * 三选一选择按钮
   */
  public interface OnConfirmListener {
    /**
     * 确认
     *
     * @param view
     */
    void confirm(View view);
  }

  /**
   * 选择按钮
   */
  public interface OnSelectListener {
    /**
     * 确认
     *
     * @param view
     */
    void confirm(View view);

    /**
     * 取消
     *
     * @param view
     */
    void cancel(View view);
  }

  /**
   * 三选一选择按钮
   */
  public interface OnChooseListener {
    /**
     * 确认
     *
     * @param view
     */
    void confirm(View view);

    /**
     * 跳过
     *
     * @param view
     */
    void skip(View view);

    /**
     * 取消
     *
     * @param view
     */
    void cancel(View view);
  }
}
