package com.cm.smartgate.service.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.smartgate.config.NetManagementConfig;
import com.cm.smartgate.dao.ISmartGateDao;
import com.cm.smartgate.enums.DoorTipsTypeEnum;
import com.cm.smartgate.manager.SmartGateManager;
import com.cm.smartgate.manager.SmartGateOnlineBO;
import com.cm.smartgate.pojo.dtos.SmartGateDTO;
import com.cm.smartgate.pojo.dtos.check.SmartGateCheckResultDTO;
import com.cm.smartgate.pojo.vos.SmartGateVO;
import com.cm.smartgate.pojo.vos.check.SmartGateCheckVO;
import com.cm.smartgate.service.ISmartGateIdCardCheckService;
import com.cm.smartgate.service.ISmartGateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import netmanager.lib.PlatUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: SmartGateServiceImpl
 * @Description: 智慧闸机
 * @Author: WangGeng
 * @Date: 2020/8/27 9:42
 * @Version: 1.0
 **/
@Service
public class SmartGateServiceImpl extends AbstractService implements ISmartGateService {

    @Autowired
    private ISmartGateDao smartGateDao;
    @Autowired
    private ISmartGateIdCardCheckService smartGateIdCardCheckService;
    @Autowired
    private NetManagementConfig netManagementConfig;

    @Override
    public SuccessResult saveSmartGate(SmartGateVO smartGateVO) throws Exception {
        SmartGateDTO smartGateDTO = getSmartGateBySerialNo(smartGateVO.getSmartGateSerialNo());
        if (smartGateDTO != null) {
            throw new SearchException("设备编码已经存在");
        }
        Map<String, Object> params = HashMapUtil.beanToMap(smartGateVO);
        params.put("smartGateId", UUIDUtil.getUUID());
        setSaveInfo(params);
        smartGateDao.saveSmartGate(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeSmartGate(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("smartGateIds", Arrays.asList(ids.split("_")));
        setUpdateInfo(params);
        smartGateDao.removeSmartGate(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateSmartGate(String smartGateId, SmartGateVO smartGateVO) throws Exception {
        SmartGateDTO smartGateDTO = getSmartGateBySerialNo(smartGateVO.getSmartGateSerialNo());
        if (smartGateDTO != null) {
            if (!StringUtils.equals(smartGateDTO.getSmartGateId(), smartGateId)) {
                throw new SearchException("设备编码已经存在");
            }
        }
        Map<String, Object> params = HashMapUtil.beanToMap(smartGateVO);
        params.put("smartGateId", smartGateId);
        setUpdateInfo(params);
        smartGateDao.updateSmartGate(params);
        return new SuccessResult();
    }

    @Override
    public SmartGateDTO getSmartGateById(String smartGateId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("smartGateId", smartGateId);
        return smartGateDao.getSmartGate(params);
    }


    @Override
    public SmartGateDTO getSmartGateBySerialNo(String smartGateSerialNo) throws SearchException {
        Map<String, Object> params = super.getHashMap(2);
        params.put("smartGateSerialNo", smartGateSerialNo);
        return smartGateDao.getSmartGate(params);
    }

    @Override
    public SuccessResultData<String> checkSmartGateBySerialNo(String smartGateSerialNo) throws SearchException {
        SmartGateDTO smartGateDTO = getSmartGateBySerialNo(smartGateSerialNo);
        if (smartGateDTO == null) {
            return new SuccessResultData<>(ISystemConstant.IS_FALSE);
        }
        return new SuccessResultData<>(ISystemConstant.IS_TRUE);
    }

    @Override
    public SuccessResultData<String> checkSmartGateBySerialNo(String smartGateId, String smartGateSerialNo) throws SearchException {
        SmartGateDTO smartGateDTO = getSmartGateBySerialNo(smartGateSerialNo);
        if (smartGateDTO != null) {
            if (!StringUtils.equals(smartGateDTO.getSmartGateId(), smartGateId)) {
                throw new SearchException("设备编码已经存在");
            }
        }
        if (smartGateDTO != null) {
            if (!StringUtils.equals(smartGateDTO.getSmartGateId(), smartGateId)) {
                return new SuccessResultData<>(ISystemConstant.IS_TRUE);
            }
        }
        return new SuccessResultData<>(ISystemConstant.IS_FALSE);
    }

    @Override
    public List<SmartGateDTO> listSmartGate(Map<String, Object> params) throws SearchException {
        return smartGateDao.listSmartGate(params);
    }

    @Override
    public SuccessResultList<List<SmartGateDTO>> listPageSmartGate(ListPage page) throws SearchException {
        System.out.println(PlatUtils.sdk_plat_getCfgProperty(netManagementConfig.getConfigPath(), "123"));
        PageHelper.startPage(page.getPage(), page.getRows());
        List<SmartGateDTO> smartGateDTOs = smartGateDao.listSmartGate(page.getParams());
        SmartGateManager smartGateManager = SmartGateManager.getInstance();
        List<SmartGateOnlineBO> smartGateOnlineBOs = smartGateManager.listSmartGate();
        for (SmartGateDTO smartGateDTO : smartGateDTOs) {
            for (SmartGateOnlineBO smartGateOnlineBO : smartGateOnlineBOs) {
                if (!StringUtils.equals(smartGateDTO.getSmartGateId(), smartGateOnlineBO.getSmartGateId())) {
                    continue;
                }
                smartGateDTO.setLastOnlineTime(new DateTime(smartGateOnlineBO.getLastOnlineTime()).toString(DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS)));
                smartGateDTO.setOnlineState(smartGateManager.isGateOffline(smartGateOnlineBO) ? 0 : 1);
                break;
            }
        }
        PageInfo<SmartGateDTO> pageInfo = new PageInfo<>(smartGateDTOs);
        return new SuccessResultList<>(smartGateDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public SuccessResultData<Integer> countSmartGate(Map<String, Object> params) throws SearchException {
        Integer count = smartGateDao.countSmartGate(params);
        return new SuccessResultData<>(count == null ? 0 : count);
    }

    @Override
    public SuccessResult heartbeat(String serialNo) {
        SmartGateDTO smartGateDTO = getSmartGateBySerialNo(serialNo);
        if (smartGateDTO == null) {
            LOG.error("设备编码不存在");
            throw new SearchException("设备编码不存在");
        }
        SmartGateManager.getInstance().updateSmartGateManager(smartGateDTO.getSmartGateId(), serialNo);
        return new SuccessResult();
    }

    @Override
    public SmartGateCheckResultDTO getOpenDoorStateCheckByIdCard(SmartGateCheckVO smartGateCheckVO, MultipartFile picture) throws SearchException {
        savePicture(picture);
        SmartGateCheckResultDTO.PictureAck pictureAck = new SmartGateCheckResultDTO.PictureAck();
        pictureAck.setPicID(smartGateCheckVO.getPicID());
        pictureAck.setResult(true);
        SmartGateCheckResultDTO.DoorOpenAndShowTips doorOpenAndShowTips = new SmartGateCheckResultDTO.DoorOpenAndShowTips();
        doorOpenAndShowTips.setType(DoorTipsTypeEnum.BOTH.getValue());
        doorOpenAndShowTips.setTipstime(3000L);
        doorOpenAndShowTips.setBackgroundColor(Arrays.asList(new Integer[]{255, 0, 0, 204}));
        doorOpenAndShowTips.setBorderColor(Arrays.asList(new Integer[]{255, 255, 255, 204}));
        doorOpenAndShowTips.setDoorIndex(0);
        doorOpenAndShowTips.setOpendelay(0L);

        SmartGateCheckResultDTO smartGateCheckResultDTO = new SmartGateCheckResultDTO();
        smartGateCheckResultDTO.setPictureAck(pictureAck);
        smartGateCheckResultDTO.setDoorOpenAndShowTips(doorOpenAndShowTips);

        List<SmartGateCheckVO.EventVO> events = smartGateCheckVO.getEvents();
        if (events.isEmpty()) {
            LOG.debug("校验人证事件列表为空");
            doorOpenAndShowTips.setCustomtip("校验失败，请重新校验");
            return smartGateCheckResultDTO;
        }
        SmartGateCheckVO.EventVO event = events.get(0);
        if (!StringUtils.equals("EventFaceAndCardsCompare", event.getCode())) {
            LOG.debug("非人证对比模式");
            doorOpenAndShowTips.setCustomtip("非人证对比模式，请检查设置");
            return smartGateCheckResultDTO;
        }
        if (event.getPass() != 1) {
            LOG.debug("人证校验不失败");
            doorOpenAndShowTips.setCustomtip("人证校验不失败，请重新校验");
            return smartGateCheckResultDTO;
        }
        SmartGateCheckVO.EventVO.IDCardsInfoVO idCardsInfo = event.getIDCardsInfo();
        boolean passState = smartGateIdCardCheckService.getPassState(idCardsInfo.getNumber());
        if (!passState) {
            LOG.debug("该身份证没有预约，不通过");
            doorOpenAndShowTips.setCustomtip("该身份证没有预约");
            return smartGateCheckResultDTO;
        }
        LOG.debug("有预约，放行");
        doorOpenAndShowTips.setCustomtip("校验通过");
        doorOpenAndShowTips.setOpendelay(1000L);
        return smartGateCheckResultDTO;
    }

    /**
     * 操作图片，暂不做处理
     *
     * @param picture
     */
    private void savePicture(MultipartFile picture) {
        try (InputStream inputStream = picture.getInputStream()) {
            // 暂时不做处理
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
