package com.example.blu.toys.ble.receiver;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;

import com.blankj.utilcode.util.LogUtils;
import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.clj.fastble.scan.BleScanRuleConfig;
import com.example.blu.toys.app.App;
import com.example.blu.toys.ble.Notify;
import com.example.blu.toys.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


@SuppressWarnings("all")
public class VehiclePlay {


	String UUID_SERVICE = "00001812-0000-1000-8000-00805f9b34fb";
	String UUID_WRITE = "0000180f-0000-1000-8000-00805f9b34fb";

	String UUID_NOTIFY = "0000ae02-0000-1000-8000-00805f9b34fb";


	private static VehiclePlay instance;

	private SpUtils mSpUtils;


	public static VehiclePlay getInstance() {
		if (instance == null) {
			synchronized (VehiclePlay.class) {
				if (instance == null) {
					instance = new VehiclePlay();
				}
			}
		}
		return instance;
	}


	private VehiclePlay() {
		mSpUtils = SpUtils.getSpUtils(App.getInstance());
	}


	public void scanBle() {

		BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
				.setAutoConnect(false)
				.setScanTimeOut(30000)
				.build();
		BleManager.getInstance().initScanRule(scanRuleConfig);

		BleManager.getInstance().scan(new BleScanCallback() {
			@Override
			public void onScanStarted(boolean success) {
				LogUtils.e("===========0000");
			}

			@Override
			public void onScanning(BleDevice bleDevice) {

			}

			@Override
			public void onScanFinished(List<BleDevice> scanResultList) {
				EventBus.getDefault().post(scanResultList);
			}
		});
	}


	/**
	 * @param mac 地址
	 */
	public void connectedCac(String mac) {

	}


	/**
	 * @param name 名字
	 */
	public void connectedName(String name) {

	}

	/**
	 * @param bleDevice 设备
	 */
	public void connectedBleDevice(BleDevice bleDevice) {

		BleManager.getInstance().connect(bleDevice, new BleGattCallback() {

			//开始链接
			@Override
			public void onStartConnect() {

			}

			//链接失败
			@Override
			public void onConnectFail(BleDevice bleDevice, BleException exception) {

			}

			//链接成功
			@Override
			public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

			}

			//关闭链接
			@Override
			public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {

			}
		});

	}


	/**
	 * 暂停
	 */
	public void sendCance(BleDevice bleDevice, byte[] data) {
		BleManager.getInstance().write(
				bleDevice,
				UUID_SERVICE,
				UUID_WRITE,
				data,
				new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {

					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});

	}


	/**
	 * 开始/继续
	 */
	public void sendStard(BleDevice bleDevice, byte[] data) {
		BleManager.getInstance().write(
				bleDevice,
				UUID_SERVICE,
				UUID_WRITE,
				data,
				new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {

					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});


	}


	/**
	 * 暂停
	 */
	public void sendPause(BleDevice bleDevice, byte[] data) {
		BleManager.getInstance().write(
				bleDevice,
				UUID_SERVICE,
				UUID_WRITE,
				data,
				new BleWriteCallback() {
					@Override
					public void onWriteSuccess(int current, int total, byte[] justWrite) {

					}

					@Override
					public void onWriteFailure(BleException exception) {

					}
				});


	}


	/**
	 * 取消扫描
	 */
	public void canceScan() {
		BleManager.getInstance().cancelScan();
	}


	/**
	 * 蓝牙是否连接
	 *
	 * @rturn
	 */
	public boolean isConnected(BleDevice bleDevice) {
		return BleManager.getInstance().isConnected(bleDevice);
	}

	/**
	 * 断开蓝牙链接
	 */
	public void closeBle(BleDevice bleDevice) {
		BleManager.getInstance().disconnect(bleDevice);
	}

	/**
	 * 是否打开蓝牙
	 *
	 * @return
	 */
	public boolean isBleOpen() {
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		return adapter == null ? false : true;
	}


}
