package com.eactive.eai.rms.onl.manage.serviceinfo.addqueue;

import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitor;
import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterGroupService;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationEntity;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationId;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationSearch;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationService;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorSearch;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorService;
import com.eactive.eai.rms.data.entity.onl.server.EAIServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("addQueueManService")
public class AddQueueManService extends BaseService {

   @Autowired
   @Qualifier("chnlInfoIntegrationService")
   private ChnlInfoIntegrationService chnlInfoIntegrationService;

   @Autowired
   private AdapterGroupService adapterGroupService;

   @Autowired
   private AddQueueUIMapper addQueueUIMapper;

   @Autowired
   EAIServerService eaiServerService;

   @Autowired
   QueueMonitorService queueMonitorService;

   @Transactional(readOnly = false, transactionManager = "transactionManager")
   public Page<AddQueueUI> selectList(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch, Pageable pageable) throws Exception {

      chnlInfoIntegrationSearch.setSearchDataDstcd("QUEUE");

      Page<ChnlInfoIntegrationEntity> addQueueUIs = chnlInfoIntegrationService.selectList(chnlInfoIntegrationSearch, pageable);

      return addQueueUIs.map(tuple -> {
         AddQueueUI addQueueUI = addQueueUIMapper.toVo(tuple);

         if( addQueueUI.getCodeName().length() >= 3 ){
            addQueueUI.setChnnl(addQueueUI.getCodeName().substring(0, addQueueUI.getCodeName().length()-2));
            addQueueUI.setGubun(addQueueUI.getCodeName().substring(addQueueUI.getCodeName().length()-2, addQueueUI.getCodeName().length()));
         }

         return addQueueUI;
      });
   }

   public List<AddQueueUI> selectDetail(String searchChnnl, String serviceType) throws Exception {

      ChnlInfoIntegrationSearch chnlInfoIntegrationSearch = new ChnlInfoIntegrationSearch();
      chnlInfoIntegrationSearch.setSearchDataDstcd("QUEUE");
      chnlInfoIntegrationSearch.setSearchChnnl(searchChnnl);
      chnlInfoIntegrationSearch.setServiceType(serviceType);

      List<ChnlInfoIntegrationEntity> addQueueUIs = chnlInfoIntegrationService.selectDetail(chnlInfoIntegrationSearch);

      List<AddQueueUI> retunQueues = new ArrayList<>();
      for( ChnlInfoIntegrationEntity tmpQueue : addQueueUIs ){
         AddQueueUI addQueueUI = addQueueUIMapper.toVo(tmpQueue);

         if( addQueueUI.getCodeName().length() >= 3 ){
            addQueueUI.setChnnl(addQueueUI.getCodeName().substring(0, addQueueUI.getCodeName().length()-2));
            addQueueUI.setGubun(addQueueUI.getCodeName().substring(addQueueUI.getCodeName().length()-2, addQueueUI.getCodeName().length()));
         }

         retunQueues.add(addQueueUI);
      }

      return retunQueues;
   }

   public void update(HashMap hm,HashMap<String, Object>[] list) throws Exception {

      String serviceType = (String) hm.get("serviceType");

      for(HashMap<String, Object> map : list){
         String chnnl = (String)map.get("CHNNL");
         String gubun = (String)map.get("GUBUN");

         String fullNm = chnnl+gubun;

         ChnlInfoIntegrationEntity chnlInfoIntegrationEntity = new ChnlInfoIntegrationEntity();
         ChnlInfoIntegrationId chnlInfoIntegrationId = new ChnlInfoIntegrationId();

         chnlInfoIntegrationId.setDatadstcd("QUEUE");
         chnlInfoIntegrationId.setServicetype(serviceType);
         chnlInfoIntegrationId.setCodename(fullNm);

         Optional<ChnlInfoIntegrationEntity> findEntity =  chnlInfoIntegrationService.findById(chnlInfoIntegrationId);

         chnlInfoIntegrationEntity = findEntity.get();

         chnlInfoIntegrationEntity.setNodename(map.get("NODENAME").toString());
         chnlInfoIntegrationEntity.setQueuename(map.get("QUEUENAME").toString());

         chnlInfoIntegrationService.save(chnlInfoIntegrationEntity);
      }
   }

   public void insert(HashMap hm ,HashMap<String, Object>[] list) throws Exception {

      String serviceType = (String) hm.get("serviceType");

      for(HashMap<String, Object> map : list){
         String chnnl = (String)map.get("CHNNL");
         String gubun = (String)map.get("GUBUN");

         String fullNm = chnnl+gubun;

         ChnlInfoIntegrationEntity chnlInfoIntegrationEntity = new ChnlInfoIntegrationEntity();
         ChnlInfoIntegrationId chnlInfoIntegrationId = new ChnlInfoIntegrationId();

         chnlInfoIntegrationId.setDatadstcd("QUEUE");
         chnlInfoIntegrationId.setServicetype(serviceType);
         chnlInfoIntegrationId.setCodename(fullNm);

         chnlInfoIntegrationEntity.setChnlInfoIntegrationId(chnlInfoIntegrationId);
         chnlInfoIntegrationEntity.setNodename(map.get("NODENAME").toString());
         chnlInfoIntegrationEntity.setQueuename(map.get("QUEUENAME").toString());

         chnlInfoIntegrationService.save(chnlInfoIntegrationEntity);
      }
   }

   public List selectChnnlList() throws Exception {

      List<String> resultList = adapterGroupService.findAdptrNode();
      List<Map<String, String>> returnList = new ArrayList<>();

      for(String tmpStr : resultList){
         Map<String, String> addListItem = new HashMap<>();

         addListItem.put("CHNNL", tmpStr);
         returnList.add(addListItem);
      }

      return returnList;
   }

   public void delete(HashMap paramMap) throws Exception {
      chnlInfoIntegrationService.delete("QUEUE", paramMap.get("serviceType").toString(), paramMap.get("codeName").toString());
   }



   public List selectNodeList() {

      List<EAIServer> eaiServers = eaiServerService.selectEaiServerIpList();
      List<Map<String, String>> resultList = new ArrayList<>();

      eaiServers.forEach(map -> {
         Map<String, String> tmpMap = new HashMap<String, String>();
         tmpMap.put("CODE", map.getEaisevrinstncname());
         tmpMap.put("NAME", map.getEaisevrinstncname());

         resultList.add(tmpMap);
      });

      return resultList;
   }

   public List selectUserQueueList() {

      QueueMonitorSearch queueMonitorSearch = new QueueMonitorSearch();
      queueMonitorSearch.setSearchNonSynczTranQueName("UserQueue");

      Iterable<QueueMonitor> queueMonitors = queueMonitorService.findAll(queueMonitorSearch);

      List<Map<String,String>> resultList = new ArrayList<>();

      queueMonitors.forEach(map -> {
         Map<String, String> tmpMap = new HashMap<String, String>();
         tmpMap.put("CODE", map.getId().getNonsyncztranquename().toString().replace("UserQueue", ""));
         tmpMap.put("NAME", map.getId().getNonsyncztranquename().toString());

         resultList.add(tmpMap);
      });

      return resultList;
   }

}