package com.YuyeTech.HeartRate.game.bluetoothinfo.controller;

import com.YuyeTech.HeartRate.VO.ResultVO;
import com.YuyeTech.HeartRate.game.bluetoothinfo.projo.BluetoothInfo;
import com.YuyeTech.HeartRate.dto.BLToothDTO;
import com.YuyeTech.HeartRate.enums.DeviceEnum;
import com.YuyeTech.HeartRate.game.bluetoothinfo.service.BluetoothInfoService;
import com.YuyeTech.HeartRate.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作设备相关
 * @author:slambb
 * @date:2020/6/12
 */
@RestController
@RequestMapping("/bluetooth")
@Slf4j
public class BluetoothController {

    @Autowired
    private BluetoothInfoService bluetoothInfoService;

    @GetMapping(value = "/findHasBind")
    public ResultVO findByBluetooth(@RequestParam(value = "mac") String mac,
                                    @RequestParam(value = "userId") String userId){
        //1.未绑定过，判断mac 是否合法(是否存在数据库中)
        BluetoothInfo bluetoothInfo = bluetoothInfoService.findByMac(mac);
        if(bluetoothInfo == null){
            //不合法
            return  ResultVOUtil.error(DeviceEnum.NOT_BLE_MAC.getCode(),
                    DeviceEnum.NOT_BLE_MAC.getMessage());
        }

        //2.合法后判断是否绑定使用过，一个账户可以绑定多个蓝牙
        if(bluetoothInfo.getBluetoothStatus().equals(1)){
            //已经使用过或者共享的，都可以返回
            if(bluetoothInfo.getUserId().equals(userId) || bluetoothInfo.getIsShare().equals(1)){
                //已经使用过，是对应的用户，返回
                return ResultVOUtil.error(DeviceEnum.HAS_BLE_BIND.getCode(),
                        DeviceEnum.HAS_BLE_BIND.getMessage());
            }else{
                //不是对应的用户
                return ResultVOUtil.error(DeviceEnum.HAS_BLE_USED.getCode(),
                        DeviceEnum.HAS_BLE_USED.getMessage());
            }
        }


        return  ResultVOUtil.success(DeviceEnum.CAN_BLE_BIND.getMessage());
    }

    @GetMapping(value = "/bind")
    public ResultVO generateDeviceInfo(@RequestParam(value = "mac") String mac,
                                       @RequestParam(value = "userId") String userId){


        //1.未绑定过，判断mac 是否合法(是否存在数据库中)
        BluetoothInfo bluetoothInfo = bluetoothInfoService.findByMac(mac);
        if(bluetoothInfo == null){
            //不合法
            return  ResultVOUtil.error(DeviceEnum.NOT_BLE_MAC.getCode(),
                    DeviceEnum.NOT_BLE_MAC.getMessage());
        }

        //2.合法后判断是否是对应用户绑定使用过，一个账户可以绑定多个蓝牙
        if(bluetoothInfo.getBluetoothStatus().equals(1)){
            if(bluetoothInfo.getUserId().equals(userId)){
                //已经使用过，是对应的用户，返回
                return ResultVOUtil.error(DeviceEnum.HAS_BLE_BIND.getCode(),
                        DeviceEnum.HAS_BLE_BIND.getMessage());
            }else{
                //不是对应的用户
                return ResultVOUtil.error(DeviceEnum.HAS_BLE_USED.getCode(),
                        DeviceEnum.HAS_BLE_USED.getMessage());
            }

        }

        //3.合法，绑定用户信息
        bluetoothInfo.setUserId(userId);
        bluetoothInfo.setBluetoothStatus(1);

        BluetoothInfo backBluetoothInfo = bluetoothInfoService.saveBluetoothInfo(bluetoothInfo);
        Map map = new HashMap();
        map.put("BindMac",backBluetoothInfo.getBluetoothMac());
        return ResultVOUtil.success(map);

    }

    @GetMapping(value = "/binding_list")
    public ResultVO getBindDeviceInfo(@RequestParam(value = "userId") String userId){

        List<BLToothDTO> blToothDTOS = bluetoothInfoService.findAllByUserId(userId);
        Map map = new HashMap();
        map.put("devices",blToothDTOS);
        return ResultVOUtil.success(map);

    }

}
