package com.inspect.patrol.service.impl;

import com.inspect.base.core.utils.ByteUtil;
import com.inspect.patrol.domain.PatrolDevice;
import com.inspect.patrol.domain.PatrolLinkagePos;
import com.inspect.patrol.domain.PatrolLinkageStrategy;
import com.inspect.patrol.domain.PatrolLinkageStrategyRef;
import com.inspect.patrol.domain.PatrolLinkageStrategyReverse;
import com.inspect.patrol.domain.PatrolPoint;
import com.inspect.patrol.linkage.block.IBlock;
import com.inspect.patrol.linkage.frame.LinkageSignEntity;
import com.inspect.patrol.mapper.PatrolDeviceMapper;
import com.inspect.patrol.mapper.PatrolLinkagePosMapper;
import com.inspect.patrol.mapper.PatrolLinkageStrategyMapper;
import com.inspect.patrol.mapper.PatrolLinkageStrategyRefMapper;
import com.inspect.patrol.mapper.PatrolLinkageStrategyReverseMapper;
import com.inspect.patrol.model.LinkageActionConfig;
import com.inspect.patrol.service.IPatrolLinkagePosService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PatrolLinkagePosServiceImpl implements IPatrolLinkagePosService {
    private final PatrolLinkagePosMapper patrolLinkagePosMapper;
    private final PatrolDeviceMapper patrolDeviceMapper;
    private final PatrolLinkageStrategyMapper patrolLinkageStrategyMapper;
    private final PatrolLinkageStrategyRefMapper patrolLinkageStrategyRefMapper;
    private final PatrolLinkageStrategyReverseMapper patrolLinkageStrategyReverseMapper;

    public PatrolLinkagePosServiceImpl(PatrolLinkagePosMapper patrolLinkagePosMapper, PatrolDeviceMapper patrolDeviceMapper, PatrolLinkageStrategyMapper patrolLinkageStrategyMapper, PatrolLinkageStrategyRefMapper patrolLinkageStrategyRefMapper, PatrolLinkageStrategyReverseMapper patrolLinkageStrategyReverseMapper) {
        this.patrolLinkagePosMapper = patrolLinkagePosMapper;
        this.patrolDeviceMapper = patrolDeviceMapper;
        this.patrolLinkageStrategyMapper = patrolLinkageStrategyMapper;
        this.patrolLinkageStrategyRefMapper = patrolLinkageStrategyRefMapper;
        this.patrolLinkageStrategyReverseMapper = patrolLinkageStrategyReverseMapper;
    }

    public PatrolLinkagePos selectPatrolLinkagePosByLinkagePosId(Long linkagePosId) {
        return this.patrolLinkagePosMapper.selectPatrolLinkagePosByLinkagePosId(linkagePosId);
    }

    public List<PatrolPoint> selectPatrolPointList(String deviceId) {
        return this.patrolLinkagePosMapper.selectPatrolPointList(deviceId);
    }

    public List<PatrolLinkagePos> selectPatrolLinkagePosList(PatrolLinkagePos patrolLinkagePos) {
        return this.patrolLinkagePosMapper.selectPatrolLinkagePosList(patrolLinkagePos);
    }

    public List<PatrolDevice> selectDeviceByAreaId(String areaId) {
        return this.patrolDeviceMapper.selectDeviceByAreaId(areaId);
    }

    @Transactional
    public int insertPatrolLinkagePos(List<IBlock> patrolLinkagePos) {
        List<PatrolLinkagePos> patrolLinkagePosList = this.patrolLinkagePosMapper.selectPatrolLinkagePosList(new PatrolLinkagePos());
        List<IBlock> update = new ArrayList<>();
        List<IBlock> insert = new ArrayList<>();
        patrolLinkagePos.forEach((j) -> {
            if (patrolLinkagePosList.stream().anyMatch((item) -> String.valueOf(item.getMonitorIndexNo()).equals(j.getMonitorIndexNo()))) {
                update.add(j);
            } else {
                insert.add(j);
            }

        });
        int count = 0;
        if (!update.isEmpty()) {
            count += this.patrolLinkagePosMapper.updatePatrolLinkagePos(update);
        }

        if (!insert.isEmpty()) {
            count += this.patrolLinkagePosMapper.insertPatrolLinkagePos(insert);
        }

        return count;
    }

    public int updatePatrolLinkagePos(PatrolLinkagePos patrolLinkagePos) {
        return -1;
    }

    @Transactional
    public int updateLinkageActionConfig(List<LinkageActionConfig> list) {

        for (LinkageActionConfig linkageActionConfig : list) {
            this.patrolLinkagePosMapper.updateLinkageActionConfig(linkageActionConfig);
        }

        return 0;
    }

    public int deletePatrolLinkagePosByLinkagePosIds(Long[] linkagePosIds) {
        return this.patrolLinkagePosMapper.deletePatrolLinkagePosByLinkagePosIds(linkagePosIds);
    }

    public int deletePatrolLinkagePosByLinkagePosId(Long linkagePosId) {
        return this.patrolLinkagePosMapper.deletePatrolLinkagePosByLinkagePosId(linkagePosId);
    }

    public List<PatrolLinkagePos> selectPatrolLinkagePosByIds(List<Long> linkagePosIds) {
        return this.patrolLinkagePosMapper.selectPatrolLinkagePosByIds(linkagePosIds);
    }

    public static void main(String[] args) {
        short s = 1;
        byte[] bytes = ByteUtil.getBytes(s);
        short aShort = ByteUtil.getShort(bytes);
        System.out.println(bytes[0]);
        System.out.println(bytes[1]);
        System.out.println(aShort);
    }

    public PatrolLinkagePos selectPatrolLinkagePos(LinkageSignEntity linkageSignEntity) {
        PatrolLinkagePos patrolLinkagePos = this.patrolLinkagePosMapper.selectPatrolLinkagePos(linkageSignEntity);
        PatrolLinkageStrategy patrolLinkageStrategy = new PatrolLinkageStrategy();
        patrolLinkageStrategy.setLinkagePosId(patrolLinkagePos.getLinkagePosId());
        List<PatrolLinkageStrategy> patrolLinkageStrategies = this.patrolLinkageStrategyMapper.selectPatrolLinkageStrategyList(patrolLinkageStrategy);
        patrolLinkagePos.setPatrolLinkageStrategies(patrolLinkageStrategies);
        PatrolLinkageStrategy patrolLinkageStrategy1 = patrolLinkageStrategies.get(0);
        PatrolLinkageStrategyRef patrolLinkageStrategyRef = new PatrolLinkageStrategyRef();
        patrolLinkageStrategyRef.setLinkageStrategyId(patrolLinkageStrategy1.getLinkageStrategyId());
        List<PatrolLinkageStrategyRef> patrolLinkageStrategyRefs = this.patrolLinkageStrategyRefMapper.selectPatrolLinkageStrategyRefList(patrolLinkageStrategyRef);
        patrolLinkageStrategy1.setPatrolLinkageStrategyRefs(patrolLinkageStrategyRefs);
        PatrolLinkageStrategyReverse patrolLinkageStrategyReverse = new PatrolLinkageStrategyReverse();
        patrolLinkageStrategyReverse.setLinkageStrategyId(patrolLinkageStrategy1.getLinkageStrategyId());
        List<PatrolLinkageStrategyReverse> patrolLinkageStrategyReverses = this.patrolLinkageStrategyReverseMapper.selectPatrolLinkageStrategyReverseList(patrolLinkageStrategyReverse);
        patrolLinkageStrategy1.setPatrolLinkageStrategyReverses(patrolLinkageStrategyReverses);
        return patrolLinkagePos;
    }
}
