package com.fs.watch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.utils.DateUtils;
import com.fs.watch.domain.*;
import com.fs.watch.domain.vo.AlarmClockObjectVo;
import com.fs.watch.domain.vo.DeviceSetupVo;
import com.fs.watch.mapper.WatchDeviceSetupMapper;
import com.fs.watch.service.WatchDeviceSetupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备设置信息Service业务层处理
 *
 * @author fs
 * @date 2024-09-19
 */
@Service
public class WatchDeviceSetupServiceImpl implements WatchDeviceSetupService
{
    @Autowired
    private WatchDeviceSetupMapper watchDeviceSetupMapper;

    /**
     * 查询设备设置信息
     *
     * @param deviceId 设备设置信息主键
     * @return 设备设置信息
     */
    @Override
    public WatchDeviceSetup selectDeviceSetupByDeviceId(String deviceId)
    {
        return watchDeviceSetupMapper.selectDeviceSetupByDeviceId(deviceId);
    }

    /**
     * 查询设备设置信息列表
     *
     * @param watchDeviceSetup 设备设置信息
     * @return 设备设置信息
     */
    @Override
    public List<WatchDeviceSetup> selectDeviceSetupList(WatchDeviceSetup watchDeviceSetup)
    {
        return watchDeviceSetupMapper.selectDeviceSetupList(watchDeviceSetup);
    }

    /**
     * 新增设备设置信息
     *
     * @param watchDeviceSetup 设备设置信息
     * @return 结果
     */
    @Override
    public int insertDeviceSetup(WatchDeviceSetup watchDeviceSetup)
    {
        watchDeviceSetup.setCreateTime(DateUtils.getNowDate());
        return watchDeviceSetupMapper.insertDeviceSetup(watchDeviceSetup);
    }

    /**
     * 修改设备设置信息
     *
     * @param watchDeviceSetup 设备设置信息
     * @return 结果
     */
    @Override
    public int updateDeviceSetup(WatchDeviceSetup watchDeviceSetup)
    {
        watchDeviceSetup.setUpdateTime(DateUtils.getNowDate());
        return watchDeviceSetupMapper.updateDeviceSetup(watchDeviceSetup);
    }

    /**
     * 批量删除设备设置信息
     *
     * @param deviceIds 需要删除的设备设置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceSetupByDeviceIds(String[] deviceIds)
    {
        return watchDeviceSetupMapper.deleteDeviceSetupByDeviceIds(deviceIds);
    }

    /**
     * 删除设备设置信息信息
     *
     * @param deviceId 设备设置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceSetupByDeviceId(String deviceId)
    {
        return watchDeviceSetupMapper.deleteDeviceSetupByDeviceId(deviceId);
    }

    @Override
    public DeviceSetupVo getSetUpInfoByDeviceId(String deviceId) {
        WatchDeviceSetup watchDeviceSetup = selectDeviceSetupByDeviceId(deviceId);
        DeviceSetupVo vo = new DeviceSetupVo();
        if (watchDeviceSetup != null) {
            vo.setDeviceId(watchDeviceSetup.getDeviceId());
            vo.setSmartDoctor(watchDeviceSetup.getSmartDoctor());
            vo.setSedentary(JSONObject.parseObject(watchDeviceSetup.getSedentary(), DeviceSedentary.class));
            vo.setFallcheck(watchDeviceSetup.getFallcheck());
            vo.setHeartHealth(watchDeviceSetup.getHeartHealth());
            vo.setHralarm(JSONObject.parseObject(watchDeviceSetup.getHralarm(), DeviceHralarm.class));
            vo.setSpo2alarm(JSONObject.parseObject(watchDeviceSetup.getSpo2alarm(), DeviceSpo2alarm.class));
            DeviceAlarmClock deviceAlarmClock = JSONObject.parseObject(watchDeviceSetup.getAlarmClock(), DeviceAlarmClock.class);
//            isdisplay(deviceAlarmClock, vo);
            vo.setAlarmClock(deviceAlarmClock);
            vo.setPower(JSONObject.parseObject(watchDeviceSetup.getPower(), DevicePower.class));
            vo.setPhonebooks(JSONObject.parseObject(watchDeviceSetup.getPhonebook(), DevicePhonebooks.class));
            vo.setTime(JSONObject.parseObject(watchDeviceSetup.getTime(), DeviceTime.class));
            vo.setLcdgesture(JSONObject.parseObject(watchDeviceSetup.getLcdgesture(),DeviceLcdgesture.class));
            vo.setUnit(JSONObject.parseObject(watchDeviceSetup.getUnit(), DeviceUnit.class));
            vo.setMode(JSONObject.parseObject(watchDeviceSetup.getMode(), DeviceMode.class));
            vo.setVersion(watchDeviceSetup.getVersion());
        }
        return vo;
    }

    private static void isdisplay(DeviceAlarmClock deviceAlarmClock, DeviceSetupVo vo) {
        if (deviceAlarmClock != null) {
            List<AlarmClockObjectVo> alarmClockObjectVos = deviceAlarmClock.getAlarmClockObjectVos();
            if (alarmClockObjectVos != null){
                for (AlarmClockObjectVo alarmClockObjectVo : alarmClockObjectVos) {
                    if (!alarmClockObjectVo.getStatus()){
                        continue;
                    }
                    if (alarmClockObjectVo.isRepeat()) {
                        alarmClockObjectVo.setStatus(true);
                    } else {
                        Date setupTime = alarmClockObjectVo.getSetupTime();
                        Date nowDate = DateUtils.getNowDate();
                        boolean sameDate = DateUtils.isSameDay(setupTime, nowDate);
                        if (sameDate){
                            if (nowDate.before(new Date(setupTime.getYear(), setupTime.getMonth(), setupTime.getDate(),
                                    alarmClockObjectVo.getHour(),alarmClockObjectVo.getMinute()))) {
                                alarmClockObjectVo.setStatus(true);
                            } else {
                                alarmClockObjectVo.setStatus(false);
                            }
                        } else {
                            alarmClockObjectVo.setStatus(false);
                        }

                    }
                }
            }

        }

        vo.setAlarmClock(deviceAlarmClock);
    }
}
