package com.mytatvarnsdk.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;

import com.mytatvarnsdk.myView.dialog.DialogType;
import com.mytatvarnsdk.myView.dialog.WarnDialog;
import com.mytatvarnsdk.myView.dialog.WarnDialogUtils;

import cgmblelib.custom.livedata.connect.OperationConnectLiveData;
import cgmblelib.custom.livedata.scan.OperationScanLiveData;
import cgmblelib.custom.livedata.scan.PocDeviceAndRssi;

/**
 * LiveData数据绑定事件分发
 *
 * @author minyuchun
 */
public abstract class BaseLiveDataEventsDistributeActivity extends BasePermissionActivity {

  /**
   * 页面以及数据是否加载完成
   */
  protected boolean isViewAndDataLoaded = false;

  /***
   * 订阅蓝牙搜索事件
   */
  protected void subscribeBLEScan() {
    OperationScanLiveData.get().observe(this, bean -> {
      if (!isViewAndDataLoaded) {
        return;
      }
      try {
        Log.d("subscribeBLEScan", "subscribeBLEScan: " + bean.getScan());

        switch (bean.getScan()) {
          case SCAN_START:
            scanStart();
            break;
          case SCAN_IN:
            scanIn(bean.getDeviceRssi());
            break;
          case SCAN_END:
            scanEnd();
            break;
          case SCAN_ERROR_BLUETOOTH_OFF:
            bluetoothStateOFF();
            break;
          case SCAN_ERROR_BLUETOOTH_ON:
            bluetoothStateON();
            break;
          case SCAN_ERROR_GPS_OFF:
            failGPS();
            break;
          case SCAN_ERROR_LOCAL_OFF:
          case SCAN_ERROR_CONNECT_OFF:
            failLocal();
            break;
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    });
  }

  /**
   * 订阅蓝牙连接事件
   */
  protected void subscribeBLEConnect() {
    OperationConnectLiveData.get().observe(this, operationConnectBean -> {
      if (!isViewAndDataLoaded) {
        return;
      }
      try {

        Log.d("subscribeBLEConnect", "subscribeBLEConnect: " + operationConnectBean.getConnect());

        switch (operationConnectBean.getConnect()) {
          case CONNECT_START:
            connectStart();
            break;
          case CONNECT_SUCCESS:
            connectSuccess();
            break;
          case CONNECT_BIND_FAIL:
            bindFail();
            break;
          case CONNECT_DISCONNECT:
          case CONNECT_FAIL_133:
          case CONNECT_FAIL_257:
          case CONNECT_FAIL_59:
            disconnect();
            break;
          case CONNECT_DISCONNECT_TIMEOUT:
            disconnectForTimeout();
            break;
          case CONNECT_BIND_CHECK_ERROR_TEMPERATURE:
            checkFailErrorTemperature();
            break;
          case CONNECT_BIND_CHECK_LOW_POWER:
            checkFailForLowPower();
            break;
          case CONNECT_PULL_START:
            pullStart();
            break;
          case CONNECT_MISMATCHED_SENSOR_TRANSMITTER:
            checkFailForMismatchedSensorAndTransmitter();
            break;
          case CONNECT_PULL_IN:
            pullIn();
            break;
          case CONNECT_PULL_END:
            pullEnd();
            break;
          case CONNECT_UNBIND_START:
            unbindStart();
            break;
          case CONNECT_UNBIND_RESET:
            unbindReset();
            break;
          case CONNECT_UNBIND_SUCCESS:
            unbindCommand();
            break;
          case CONNECT_BIND_SUCCESS:
            bindSuccess();
            break;
          case CONNECT_ONLINE_LOST:
            //网络断开连接了
            onlineLost();
            break;
          case CONNECT_ONLINE_RECONNECT:
            //网络重连
            WarnDialogUtils.getInstance().dismissWarnDialog();
            break;
        }
      } catch (Exception e) {
        Log.d("subscribeBLEConnect", "Exception: " + e.getMessage());
        e.printStackTrace();
      }
    });
  }

  private void connectSuccess() {
  }

  protected void connectStart() {

  }

  protected void unbindStart() {

  }

  /**
   * 检查到gps未开启时的弹框
   */
  protected void warn_gpsFail() {
    WarnDialogUtils.getInstance().showWarnDialog(this, DialogType.TYPE_GPS_SERVICE, new WarnDialog.OnSelectListener() {
      @Override
      public void confirm(DialogInterface dialog) {
        WarnDialogUtils.getInstance().dismissWarnDialog();
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(settingsIntent);
      }

      @Override
      public void cancel(DialogInterface dialog) {
        WarnDialogUtils.getInstance().dismissWarnDialog();
      }
    });
  }

  /**
   * 搜索开始
   */
  public void scanStart() {

  }

  /**
   * 搜索中
   *
   * @param device 发射器
   */
  public void scanIn(PocDeviceAndRssi device) {
  }

  /**
   * 搜索结束
   */
  public void scanEnd() {
  }

  /**
   * 蓝牙关闭
   */
  public void bluetoothStateOFF() {
  }

  /**
   * 蓝牙开启
   */
  public void bluetoothStateON() {

  }

  /**
   * GPS未开启
   */
  public void failGPS() {
  }

  /**
   * 定位未开启
   * android 12+ 动态蓝牙连接权限未开启
   */
  public void failLocal() {
  }

  /**
   * 绑定失败
   */
  public void bindFail() {
  }

  /**
   * 绑定成功
   */
  public void bindSuccess() {
  }

  /**
   * 普通的断开连接
   */
  public void disconnect() {
  }

  /**
   * 连接超时断开连接
   */
  public void disconnectForTimeout() {
  }

  /**
   * 点亮过低自检失败
   */
  public void checkFailForLowPower() {
  }

  /**
   * 温度异常自检失败
   */
  public void checkFailErrorTemperature() {
  }

  /**
   * 开始同步
   */
  public void pullStart() {
  }

  /**
   * 同步中
   */
  public void pullIn() {
  }

  /**
   * 同步结束
   */
  public void pullEnd() {
  }

  /**
   * 传感器与发射器匹配失败
   */
  public void checkFailForMismatchedSensorAndTransmitter() {
  }

  /**
   * 解绑 - 重置
   */
  public void unbindReset() {
  }

  /**
   * 解绑 - 命令
   */
  public void unbindCommand() {
  }

  /**
   * 网络连接断开 - 没有网络
   */
  public void onlineLost() {
  }
}
