package com.mytatvarnsdk.myView.popupwindow;

import android.app.Activity;
import android.view.Gravity;

import com.mytatvarnsdk.R;

import java.io.File;

import cgmblelib.utils.MyLog;

/**
 * 底部弹出框管理工具
 *
 * @author minyuchun
 */
public class PopupWindowManagement {
  /**
   * 底部弹出框
   */
  private BPopupWindow mBPopupWindow;

  private BPopupWindow.OnChooseListener mOnChooseListener;

  private BPopupWindow.OnSelectListener mOnSelectListener;

  private BPopupWindow.OnConfirmListener mOnConfirmListener;

  private String[] messages;

  private int popupWindowType = 0;

  private int showWay;
  private int showPosition = Gravity.CENTER;

  private static class SingletonInstance {
    private static final PopupWindowManagement INSTANCE = new PopupWindowManagement();
  }

  public static PopupWindowManagement getInstance() {
    return SingletonInstance.INSTANCE;
  }

  public PopupWindowManagement setOnChooseListener(BPopupWindow.OnChooseListener onChooseListener) {
    mOnChooseListener = onChooseListener;
    mOnSelectListener = null;
    mOnConfirmListener = null;
    return this;
  }

  public PopupWindowManagement setOnSelectListener(BPopupWindow.OnSelectListener onSelectListener) {
    mOnSelectListener = onSelectListener;
    mOnChooseListener = null;
    mOnConfirmListener = null;
    return this;
  }

  public PopupWindowManagement setOnConfirmListener(BPopupWindow.OnConfirmListener onConfirmListener) {
    mOnConfirmListener = onConfirmListener;
    mOnChooseListener = null;
    mOnSelectListener = null;
    return this;
  }

  public PopupWindowManagement setMessages(String... messages) {
    this.messages = messages;
    return this;
  }

  /**
   * 显示底部弹出框
   *
   * @param activity  当前的Activity
   * @param popupType 弹出框类型
   */
  public void show(Activity activity, @PopupType int popupType) {
    if (activity.isFinishing()) {
      MyLog.e("以来的页面已经结束");
      return;
    }
    if ((mBPopupWindow != null && mBPopupWindow.isShowing()) && popupType == popupWindowType) {
      MyLog.e("弹框情况一样");
      return;
    }
    if (isShowing()) {
      mBPopupWindow.dismiss();
      mBPopupWindow = null;
    }
    popupWindowType = popupType;
    switch (popupType) {
      case PopupType.POPUP_CONNECT_1:
      case PopupType.POPUP_CONNECT_3:
        mBPopupWindow = new TransmitterConnectPopWindow(activity, R.layout.popup_window_transmitter_connect, messages[0], messages[1]);
        mBPopupWindow.setOnSelectListener(mOnSelectListener);
        showPosition = Gravity.CENTER;
        break;
      case PopupType.POPUP_CONNECT_2:
        mBPopupWindow = new TransmitterConnectPopWindow(activity, R.layout.popup_window_transmitter_connect, messages[0], "");
        mBPopupWindow.setOnChooseListener(mOnChooseListener);
        showPosition = Gravity.CENTER;
        break;
      case PopupType.POPUP_PERMISSION_FAIL:
      case PopupType.POPUP_PERMISSION_FAIL_FOREVER:
      case PopupType.POPUP_PERMISSION_RATIONALE_LOCATION:
      case PopupType.POPUP_PERMISSION_RATIONALE_LOCATION_BACKGROUND:
      case PopupType.POPUP_PERMISSION_RATIONALE_BLUETOOTH_S:
      case PopupType.POPUP_PERMISSION_RATIONALE_CAMERA:
        mBPopupWindow = new PermissionRequestPopWindow(activity, R.layout.popup_window_request_permisson, messages[0]);
        if (mOnSelectListener != null) {
          mBPopupWindow.setOnSelectListener(mOnSelectListener);
        } else if (mOnConfirmListener != null) {
          mBPopupWindow.setOnConfirmListener(mOnConfirmListener);
        }
        showPosition = Gravity.CENTER;
        break;
      default:
        break;
    }
    showAtLocation(activity);
  }


  /**
   * 特殊的底部弹出框 - 日志分享
   *
   * @param activity  上下文
   * @param popupType 弹出框类型
   * @param file      文件
   */
  public void showLogShare(Activity activity, @PopupType int popupType, File file) {
    if (isShowing()) {
      mBPopupWindow.dismiss();
      mBPopupWindow = null;
    }
    showAtLocation(activity);
  }

  /**
   * 显示底部弹出框的位置
   *
   * @param activity 依赖的界面
   */
  private void showAtLocation(Activity activity) {
    try {
      if (mBPopupWindow != null && !activity.isFinishing()) {
        mBPopupWindow.setAnimationStyle(0);
        if (showWay == PopShowWay.BOTH_SIDES) {
          mBPopupWindow.setAnimationStyle(R.style.AnimSide);
        } else if (showWay == PopShowWay.UP_DOWN) {
          mBPopupWindow.setAnimationStyle(R.style.AnimBottom);
        }
        mBPopupWindow.showAtLocation(activity.findViewById(R.id.rl_pop_bottom), showPosition | Gravity.CENTER_HORIZONTAL, 0, 0);
        showWay = -1;
      }
    } catch (Exception e) {
      e.printStackTrace();
      dismiss();
      MyLog.e("错误 " + activity.isFinishing());
      MyLog.e("错误 " + e.getMessage());
    }

  }

  /**
   * 隐藏弹出框
   */
  public void dismiss() {
    if (isShowing()) {
      mBPopupWindow.dismiss();
      mBPopupWindow = null;
    }
    mOnChooseListener = null;
    mOnSelectListener = null;
    mOnConfirmListener = null;
    popupWindowType = 0;
  }


  /**
   * 弹出框是否正在显示
   *
   * @return true正在显示;否则false
   */
  public boolean isShowing() {
    return mBPopupWindow != null && mBPopupWindow.isShowing();
  }

  public BPopupWindow getmBPopupWindow() {
    return mBPopupWindow;
  }
}
