package com.jiayue.ipfcst.dataexchange.tunnelworker.workassist;

import com.jiayue.ipfcst.common.data.entity.BaseTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.ProtocolSenderDataPoint;
import com.jiayue.ipfcst.common.data.entity.gathertunnelinfo.*;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.Sender104TcpTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderCdtRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusTcpTunnelInfo;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.BaseDataGainer;
import com.jiayue.ipfcst.dataexchange.exception.DataExchangeException;
import com.jiayue.ipfcst.dataexchange.tunnelworker.BaseProtocolTunnel;
import com.jiayue.ipfcst.dataexchange.tunnelworker.gathers.iml.*;
import com.jiayue.ipfcst.dataexchange.tunnelworker.senders.iml.CdtRtuDataSender;
import com.jiayue.ipfcst.dataexchange.tunnelworker.senders.iml.Iec104TcpDataSender;
import com.jiayue.ipfcst.dataexchange.tunnelworker.senders.iml.ModbusRtuDataSender;
import com.jiayue.ipfcst.dataexchange.tunnelworker.senders.iml.ModbusTcpDataSender;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

/**
 * 用Tunnel Info 创建通达
 *
 * @author: xiuwei
 * @version:
 */
public class TunnelBuilder {

  public static BaseProtocolTunnel buildTunnel(BaseTunnelInfo tunnelInfo) throws DataExchangeException {
    switch (tunnelInfo.getTunnelType()) {
      case IEC104TCPMASTER:
        return new Iec104TcpDataGather((Gather104TcpTunnelInfo) tunnelInfo).buildTunnel();
      case IEC104TCPSLAVE:
        return new Iec104TcpDataSender((Sender104TcpTunnelInfo) tunnelInfo).buildTunnel();
      case MODBUSRTUMASTER:
        return new ModbusRtuDataGather((GatherModbusRtuTunnelInfo) tunnelInfo).buildTunnel();
      case MODBUSTCPMASTER:
        return new ModbusTcpDataGather((GatherModbusTcpTunnelInfo) tunnelInfo).buildTunnel();
      case MODBUSRTUSLAVE:
        return new ModbusRtuDataSender((SenderModbusRtuTunnelInfo) tunnelInfo).buildTunnel();
      case MODBUSTCPSLAVE:
        return new ModbusTcpDataSender((SenderModbusTcpTunnelInfo) tunnelInfo).buildTunnel();
      case CDTRTUSLAVE:
        return new CdtRtuDataSender((SenderCdtRtuTunnelInfo) tunnelInfo).buildTunnel();
      case CDTRTUMASTER:
        return new CdtRtuDataGather((GatherCdtRtuTunnelInfo) tunnelInfo).buildTunnel();
      case TCPSERVERASMODBUSMASTER:
        return new ModbusRtuMasterWithTcpServerDataGather((GatherModbusRtuWithTcpServerTunnelInfo) tunnelInfo).buildTunnel();
      default: throw new IllegalArgumentException();
    }

  }


  /**
   * 为转发通道构建  for 104sender 和 CDT
   *
   * @param yx yx
   * @param yc yc
   * @return {@link String}
   */
  public static String buildSenderDataStr( Map<Integer, BaseDataGainer<Boolean>> yx,Map<Integer, BaseDataGainer<Number>> yc){
    StringBuffer s = new StringBuffer();
    TreeSet<Integer> set = new TreeSet<>(yx.keySet());
    set.addAll(yc.keySet());
    Iterator<Integer> iter = set.iterator();
    Integer key;
    while (iter.hasNext()) {
      key = iter.next();
      try {
        if(yx.containsKey(key)){
        s.append(yx.get(key).getData());}
        else if(yc.containsKey(key)){
          s.append(yc.get(key).getData());
        }
      } catch (DataExchangeException e) {
        e.printStackTrace();
      }
      s.append(",");
    }
    if (s.length()>0&&','==(s.charAt(s.length()-1))){
      s.deleteCharAt(s.length()-1);
    }
    return s.toString();
  }



  /**
   * 为转发通道构建  for 104sender 和 CDT
   *
   * @param yx yx
   * @param yc yc
   * @return {@link String}
   */
  public static String buildSenderDataStr( Map<Integer, BaseDataGainer<Boolean>> yx,Map<Integer, BaseDataGainer<Number>> yc, Map<Integer, ProtocolSenderDataPoint> dataPointMap){
    StringBuffer s = new StringBuffer();
    TreeSet<Integer> set = new TreeSet<>(dataPointMap.keySet());
    Iterator<Integer> iter = set.iterator();
    Integer key;
    while (iter.hasNext()) {
      key = dataPointMap.get(iter.next()).getId();
      try {
        if(yx.containsKey(key)){
          s.append(yx.get(key).getData());}
        else if(yc.containsKey(key)){
          s.append(yc.get(key).getData());
        }
      } catch (DataExchangeException e) {
        e.printStackTrace();
      }
      s.append(",");
    }
    if (s.length()>0&&','==(s.charAt(s.length()-1))){
      s.deleteCharAt(s.length()-1);
    }
    return s.toString();
  }


}


