package com.jiayue.ipfcst.dataexchange.dataprocesser.datapacker.powerofreference.iml;

import com.jiayue.ipfcst.common.data.constant.enums.DataSourceEnum;
import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.InverterInfo;
import com.jiayue.ipfcst.common.data.entity.ProtocolGatherDataPoint;
import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
import com.jiayue.ipfcst.console.service.InverterInfoService;
import com.jiayue.ipfcst.console.service.WindTurbineInfoService;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.BaseDataGainer;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.NumOrBool;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.iml.GetDataFromContainer;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datagainerhandler.iml.PointDataCalculator;
import com.jiayue.ipfcst.dataexchange.dataprocesser.datapacker.powerofreference.BasePowerOfReferenceCalculator;
import com.jiayue.ipfcst.dataexchange.service.ProtocolGatherDataPointService;
import lombok.NonNull;
import lombok.SneakyThrows;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 每个样板机的平均功率*  风机或逆变器个数
 *
 * @author: xiuwei
 * @version:
 */
public class PowerOfReferenceCalculator2 extends BasePowerOfReferenceCalculator {

  Map<Integer, BaseDataGainer<BigDecimal>> samples = new HashMap();

  public PowerOfReferenceCalculator2(WindTurbineInfoService windTurbineInfoService, InverterInfoService inverterInfoService, ElectricFieldService electricFieldService, ProtocolGatherDataPointService protocolGatherDataPointService) {
    super(windTurbineInfoService, inverterInfoService, electricFieldService, protocolGatherDataPointService);
  }

  /**
   * 添加设备
   *
   * @param equipmentId             设备id
   * @param protocolGatherDataPoint 设备数据获取器
   */
  public void addSampleEquipment(@NonNull Integer equipmentId, @NonNull ProtocolGatherDataPoint protocolGatherDataPoint) {
    BaseDataGainer<BigDecimal> dg = null;
    if (protocolGatherDataPoint != null) {
      if (DataSourceEnum.GATHER.equals(protocolGatherDataPoint.getDataSource())) {
        dg = new GetDataFromContainer(NumOrBool.NUM, protocolGatherDataPoint.getId(), protocolGatherDataPoint.getMagnification());
      } else if (DataSourceEnum.GENERATE.equals(protocolGatherDataPoint.getDataSource())) {
        dg = new PointDataCalculator(NumOrBool.NUM, protocolGatherDataPoint.getGenerationRegulation(), protocolGatherDataPoint.getMagnification());
      }
      samples.put(equipmentId, dg);
    }
  }

  @SneakyThrows
  @Override
  public BigDecimal getEstimatePower() {
    BigDecimal total = BigDecimal.valueOf(0);
    if(samples.size()==0){
      return total;
    }
    for (BaseDataGainer<BigDecimal> dg : samples.values()) {
      total = total.add(dg.getData());
    }
    return total.divide(BigDecimal.valueOf(samples.size()),2).multiply(BigDecimal.valueOf(electricFieldService.getElectricField().getGridCE()));
  }

  @SneakyThrows
  @Override
  public BasePowerOfReferenceCalculator init() {
    ElectricField electricField = electricFieldService.getElectricField();
    this.electricFieldType = electricField.getElectricFieldTypeEnum();
    ProtocolGatherDataPoint point;
    if (ElectricFieldTypeEnum.E1.equals(this.electricFieldType)) {
      List<InverterInfo> inverterInfos = inverterInfoService.getAllSample();
      for (InverterInfo i : inverterInfos) {
        point = protocolGatherDataPointService.get(i.getId(), EquipmentTypeEnum.INVERTER, "activePower");
        if (point != null) {
          this.addSampleEquipment(i.getId(), point);
        }
      }
    } else if (ElectricFieldTypeEnum.E2.equals(this.electricFieldType)) {
      List<WindTurbineInfo> windTurbineInfos = windTurbineInfoService.getAllSample();
      for (WindTurbineInfo i : windTurbineInfos) {
        point = protocolGatherDataPointService.get(i.getId(), EquipmentTypeEnum.INVERTER, "activePower");
        if (point != null) {
          this.addSampleEquipment(i.getId(), point);
        }
      }
    }
    return this;
  }
}
