package com.sooka.sponest.monitor.tvwall.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.sooka.sponest.monitor.LabelManagement.domain.CentermonitorSetLabel;
import com.sooka.sponest.monitor.LabelManagement.mapper.CentermonitorSetLabelServiceMapper;
import com.sooka.sponest.monitor.aidevicedept.domain.CenterdataTAidevicedept;
import com.sooka.sponest.monitor.aidevicedept.mapper.CenterdataTAidevicedeptMapper;
import com.sooka.sponest.monitor.base.service.impl.BaseService;
import com.sooka.sponest.monitor.camera.domain.CentermonitorTCamera;
import com.sooka.sponest.monitor.camera.domain.VisuForestCloudCameraBO;
import com.sooka.sponest.monitor.camera.domain.VisuForestMonitorCenterVO;
import com.sooka.sponest.monitor.camera.mapper.CentermonitorTCameraMapper;
import com.sooka.sponest.monitor.dahua.service.impl.DahuaServiceImpl;
import com.sooka.sponest.monitor.tvwall.domain.*;
import com.sooka.sponest.monitor.tvwall.mapper.TVWallMapper;
import com.sooka.sponest.monitor.tvwall.service.TVWallService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TVWallServiceImpl  extends BaseService implements TVWallService {

    private static org.slf4j.Logger log = LoggerFactory.getLogger(DahuaServiceImpl.class);

    @Resource
    TVWallMapper tvWallMapper;
    @Resource
    private CentermonitorTCameraMapper centermonitorTCameraMapper;
    @Resource
    private CentermonitorSetLabelServiceMapper centermonitorSetLabelServiceMapper;
    @Resource
    private CenterdataTAidevicedeptMapper centerdataTAidevicedeptMapper;

    /**
     * 根据当前登录人的部门获取摄像头
     * */
    @Override
    public AjaxResult init() {
        /*******************取登录人的部门ID组成数组************************/
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        List<SysDept> sysDepts = sysUser.getDepts();
        String[] deptArray = new String[sysDepts.size()];
        for (int i = 0; i < sysDepts.size(); i++) {
            deptArray[i] = sysDepts.get(i).getDeptId().toString();
        }
        /*******************取登录人的部门ID组成数组************************/
        List<treeLabels> treeLabelsList = tvWallMapper.gettreeLabelsList(deptArray);
        return buildDHTVWallData(treeLabelsList);
    }

    /**
     * 根据重点工程的ID获取绑定的摄像头
     * */
    @Override
    public AjaxResult initByProject(TVWallVO vo) {
        setSookaDataBase(vo);
        List<treeLabels> treeLabelsList = tvWallMapper.getCameraByProjectId(vo);
        return buildDHTVWallData(treeLabelsList);
    }

    /**
     * 根据重点工程的ID获取绑定的摄像头
     * */
    @Override
    public AjaxResult initByArea(TVWallVO vo) {
        setSookaDataBase(vo);
        List<treeLabels> treeLabelsList = tvWallMapper.getCameraByAreaId(vo);
        return buildDHTVWallData(treeLabelsList);
    }

    /**
     * 根据摄像头集合构建电视墙可用的数据
     * */
    @Override
    public AjaxResult initByCameras(TVWallVO vo) {
        vo.setList(centermonitorTCameraMapper.findCameraByEventCoordinate(vo.getLongitude(), vo.getLatitude(), vo.getType()));
        /*******************对象转换************************/
        List<treeLabels> treeLabelsList = new ArrayList<>();
        List<CentermonitorTCamera> list = vo.getList();
        for (int i = 0; i < list.size(); i++) {
            treeLabels t = new treeLabels();
            t.setId(list.get(i).getId());
            t.setLabelName(list.get(i).getCameraName());
            t.setLabelCode(list.get(i).getCameraCode());
            t.setCameraType(list.get(i).getCameraType());
            treeLabelsList.add(t);
        }
        /*******************对象转换************************/
        return buildDHTVWallData(treeLabelsList);
    }

    /**
     * 根据摄像头集合构建电视墙可用的数据
     * */
    @Override
    public AjaxResult getCamerasByDeptId(TVWallVO vo) {
        //查询条件：通过部门ID
        VisuForestMonitorCenterVO bo = new VisuForestMonitorCenterVO();
        bo.setDeptId(vo.getDeptId());
        bo.setType(vo.getType());
        /*******************对象转换************************/
        List<treeLabels> treeLabelsList = new ArrayList<>();
        List<VisuForestCloudCameraBO> list = centermonitorTCameraMapper.selectCameraByDepId(bo);
        for (int i = 0; i < list.size(); i++) {
            treeLabels t = new treeLabels();
            t.setId(list.get(i).getId());
            t.setLabelName(list.get(i).getCameraName());
            t.setLabelCode(list.get(i).getCameraCode());
            t.setCameraType(list.get(i).getCameraType());
            treeLabelsList.add(t);
        }
        /*******************对象转换************************/
        return buildDHTVWallData(treeLabelsList);
    }

    /**
     * 根据摄像头集合构建电视墙可用的数据
     * */
    @Override
    public AjaxResult getCamerasByLabelId(CentermonitorSetLabel centermonitorSetLabel) {
        //查询条件：通过部门ID
        /*******************对象转换************************/
        List<treeLabels> treeLabelsList = new ArrayList<>();
        List<CentermonitorSetLabel>  list = centermonitorSetLabelServiceMapper.getCameraByLabelId(centermonitorSetLabel);
        for (int i = 0; i < list.size(); i++) {
            treeLabels t = new treeLabels();
            t.setId(list.get(i).getSbId());
            t.setLabelName(list.get(i).getSbName());
            t.setLabelCode(list.get(i).getCameraCode());
            t.setCameraType(list.get(i).getCameraType());
            treeLabelsList.add(t);
        }
        /*******************对象转换************************/
        return buildDHTVWallData(treeLabelsList);
    }

    private AjaxResult buildDHTVWallData(List<treeLabels> treeLabelsList){
        TVWallBO tvWallBO = new TVWallBO();
        List<TVWallBO> tvWallBOList=new ArrayList();
        //没查到数据返回空集合
        if (treeLabelsList.size() == 0) return AjaxResult.success(tvWallBOList);
        //否则继续查通道 ,先用集合组成摄像头ID数组查通道
        String[] cameraArray = new String[treeLabelsList.size()];
        for (int i = 0; i < treeLabelsList.size(); i++) {
            cameraArray[i] = treeLabelsList.get(i).getId();
        }
        treeLabels treeLabels=new treeLabels();
        treeLabels.setLabelName("电视墙");
        treeLabels.setLabelCode("999");
        treeLabels.setParentLabelCode("");
        treeLabelsList.add(0,treeLabels);
        List<channelDates> channelDatesList = tvWallMapper.getChannelDatesList(cameraArray);
        List<labelChannels> labelChannelsList = new ArrayList();
        for (int i = 0; i < channelDatesList.size(); i++) {
            labelChannels labelChannel = new labelChannels();
            channelDates channelDate = channelDatesList.get(i);
            labelChannel.setLabelCode(channelDate.getCameraCode());
            String[] channelCodes = channelDate.getChannelCode().split(",");
            String[] channelNames = channelDate.getChannelName().split(",");
            List<channelDates> channelDatesList1 = new ArrayList<>();
            if (channelCodes.length == channelNames.length) {
                for (int a = 0; a < channelCodes.length; a++) {
                    channelDates channelDate1 = new channelDates();
                    channelDate1.setChannelCode(channelCodes[a]);
                    channelDate1.setChannelName(channelNames[a]);
                    channelDate1.setCameraType(channelDate.getCameraType());
                    channelDate1.setDeviceCode("10694394902218944");
                    channelDate1.setDeviceSn("34020000001320000103");
                    channelDate1.setDeviceProtocolType("3");
                    channelDate1.setDeviceLoginType("10");
                    channelDatesList1.add(channelDate1);
                }
            }
            labelChannel.setChannelDates(channelDatesList1);
            labelChannelsList.add(labelChannel);
        }
        tvWallBO.setTreeLabels(treeLabelsList);
        tvWallBO.setLabelChannels(labelChannelsList);
        tvWallBOList.add(tvWallBO);
        return AjaxResult.success(tvWallBOList);
    }


}
