package com.iformall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;

import com.iformall.common.ResultData;
import com.iformall.domain.po.WxDevice;
import com.iformall.enums.EnumDeviceStatus;
import com.iformall.enums.EnumDeviceType;
import com.iformall.mapper.WxDeviceMapper;
import com.iformall.service.WxDeviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class WxDeviceServiceImpl implements WxDeviceService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxDeviceMapper wxDeviceMapper;

    final String DEVICE_DEFAULT_CONFIG_SCREEN_AD = "{" +
            "\"heartbeatInterval\":300," +   //5MIN
            "\"dataRefreshInterval\":3600," +  //1HOUR
            "\"pageChangeInterval\":10," +       //10SEC
            "\"animationTheme\":0" +
            "}";


    @Override
    public PageInfo<WxDevice> listAsPage(WxDevice record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxDeviceMapper.findList(record));
    }

    @Override
    public WxDevice getById(Long id) {
        return wxDeviceMapper.selectById(id);
    }


    private String getDefaultConfig(int type) {

        if (type == EnumDeviceType.SCREEN.getCode()) {
            return DEVICE_DEFAULT_CONFIG_SCREEN_AD;
        }
        return null;
    }

    private boolean checkConfig(int type, String config) {
        JSONObject jConfig;
        try {
            jConfig = JSONObject.parseObject(config);
        } catch (Exception e) {
            return false;
        }

        if (type == EnumDeviceType.SCREEN.getCode()) {
            Integer heartBeatInterval = jConfig.getInteger("heartbeatInterval");
            Integer dataRefreshInterval = jConfig.getInteger("dataRefreshInterval");
            Integer pageChangeInterval = jConfig.getInteger("pageChangeInterval");
            Integer animationTheme = jConfig.getInteger("animationTheme");
            if (heartBeatInterval == null ||
                    dataRefreshInterval == null ||
                    pageChangeInterval == null ||
                    animationTheme == null) {
                return false;
            }
        }
        return true;
    }

    @Override
    public ResultData saveOrUpdate(WxDevice record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
        	record.setCreateDate(new Date());
            record.setUpdateDate(new Date());
            record.setConfig(getDefaultConfig(record.getType()));
            wxDeviceMapper.insert(record);
        } else {
            record.setUpdateDate(new Date());
            if (record.getConfig() != null && record.getType() != null){
                if (!checkConfig(record.getType(),record.getConfig())) {
                    return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
                }
            }
            wxDeviceMapper.updateById(record);
        }
        return new ResultData();
    }


    @Override
    public void deleteById(Long id) {
        wxDeviceMapper.deleteById(id);
    }
    @Override
    public List<WxDevice> findList(WxDevice wxDevice) {
        return wxDeviceMapper.findList(wxDevice);
    }


}
