//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.gx.obe.evaluation.step.calculator.sampling;

import com.gx.obe.evaluation.step.calculator.PriceScoreCalculator;
import com.gx.obe.formula.BasePriceRangeParam;
import com.gx.obe.formula.SpecimenFilterConditionItem;
import com.gx.obe.util.utils.BigDecimalUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DispersionRangeSamplingImpl implements ISampling {
    public DispersionRangeSamplingImpl() {
    }

    public void start(PriceScoreCalculator tool) {
        BasePriceRangeParam basePriceRangeParam = tool.getSpecimenParam();
        BigDecimal avgBigdecimal = tool.getAvgEvalPrice();
        BigDecimal priceScore = basePriceRangeParam.getPriceScore();
        List<BigDecimal> effectEvaPriceList = new ArrayList();
        Iterator var7 = tool.getEffectiveEvalPriceList().iterator();

        while(var7.hasNext()) {
            BigDecimal bigDecimal = (BigDecimal)var7.next();
            effectEvaPriceList.add(bigDecimal);
        }

        boolean removeMin = false;
        boolean removeMax = false;
        BigDecimal minEvalPrice = tool.getMinEvalPrice();
        BigDecimal minNextEvalPrice = tool.getNextMinEvalPrice();
        BigDecimal thirdMinEvalPrice = tool.getThirdMinEvalPrice();
        BigDecimal maxEvalPrice = tool.getMaxEvalPrice();
        BigDecimal nextMaxEvalPrice = tool.getNextMaxEvalPrice();
        BigDecimal thirdMaxEvalPrice = tool.getThirdMaxEvalPrice();
        BigDecimal downFloatPercent;
        boolean isIncludeDispersionFilterMinRange;
        BigDecimal upFloatPercent;
        boolean isIncludeDispersionFilterMaxRange;
        BigDecimal effectPrice;
        BigDecimal deviationPercent;
        BigDecimal minDifferenceRate;
        BigDecimal minDifference;
        if (tool.getEffectiveSupplierCount() > tool.getSpecimenParam().getSupplierNum()) {
            downFloatPercent = basePriceRangeParam.getDispersionFilterMinRange();
            isIncludeDispersionFilterMinRange = basePriceRangeParam.isIncludeDispersionFilterMinRange();
            upFloatPercent = basePriceRangeParam.getDispersionFilterMinRange();
            isIncludeDispersionFilterMaxRange = basePriceRangeParam.isIncludeDispersionFilterMaxRange();

            for(int i = 0; i < effectEvaPriceList.size(); ++i) {
                effectPrice = (BigDecimal)effectEvaPriceList.get(i);
                if (downFloatPercent != null && isIncludeDispersionFilterMinRange) {
                    if (BigDecimalUtils.compareTo(BigDecimalUtils.mul(avgBigdecimal, BigDecimalUtils.div(downFloatPercent, new BigDecimal(100))), effectPrice) < 0) {
                        effectEvaPriceList.remove(i--);
                    } else if (BigDecimalUtils.compareTo(BigDecimalUtils.mul(avgBigdecimal, BigDecimalUtils.div(downFloatPercent, new BigDecimal(100))), effectPrice) <= 0) {
                        effectEvaPriceList.remove(i--);
                    }
                } else if (upFloatPercent != null && isIncludeDispersionFilterMaxRange) {
                    if (BigDecimalUtils.compareTo(BigDecimalUtils.mul(avgBigdecimal, BigDecimalUtils.div(upFloatPercent, new BigDecimal(100))), effectPrice) > 0) {
                        effectEvaPriceList.remove(i--);
                    } else if (BigDecimalUtils.compareTo(BigDecimalUtils.mul(avgBigdecimal, BigDecimalUtils.div(upFloatPercent, new BigDecimal(100))), effectPrice) >= 0) {
                        effectEvaPriceList.remove(i--);
                    }
                }
            }

            if (effectEvaPriceList.size() >= 1) {
                tool.setEffectiveEvalPriceList(effectEvaPriceList);
            }

            List<SpecimenFilterConditionItem> specimenFilterConditionItemList = basePriceRangeParam.getSpecimenFilterConditionItemList();
            Iterator var20 = specimenFilterConditionItemList.iterator();

            while(var20.hasNext()) {
                SpecimenFilterConditionItem specimenFilterConditionItem = (SpecimenFilterConditionItem)var20.next();
                deviationPercent = specimenFilterConditionItem.getDeviationPercent();
                minDifferenceRate = BigDecimal.ZERO;
                minDifference = BigDecimal.ZERO;
                if (deviationPercent != null) {
                    if (specimenFilterConditionItem.isRemoveLow()) {
                        if ("SECOND_FIRST".equals(specimenFilterConditionItem.getCompareType())) {
                            minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(minNextEvalPrice, deviationPercent), new BigDecimal(100));
                            minDifference = BigDecimalUtils.sub(minNextEvalPrice, minEvalPrice);
                        } else if ("THIRD_SECOND".equals(specimenFilterConditionItem.getCompareType())) {
                            minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(thirdMinEvalPrice, deviationPercent), new BigDecimal(100));
                            minDifference = BigDecimalUtils.sub(thirdMinEvalPrice, minNextEvalPrice);
                        } else if ("THIRD_FIRST".equals(specimenFilterConditionItem.getCompareType())) {
                            minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(thirdMinEvalPrice, deviationPercent), new BigDecimal(100));
                            minDifference = BigDecimalUtils.sub(thirdMinEvalPrice, minEvalPrice);
                        }
                    } else if ("SECOND_FIRST".equals(specimenFilterConditionItem.getCompareType())) {
                        minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(nextMaxEvalPrice, deviationPercent), new BigDecimal(100));
                        minDifference = BigDecimalUtils.sub(maxEvalPrice, nextMaxEvalPrice);
                    } else if ("THIRD_SECOND".equals(specimenFilterConditionItem.getCompareType())) {
                        minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(thirdMaxEvalPrice, deviationPercent), new BigDecimal(100));
                        minDifference = BigDecimalUtils.sub(nextMaxEvalPrice, thirdMaxEvalPrice);
                    } else if ("THIRD_FIRST".equals(specimenFilterConditionItem.getCompareType())) {
                        minDifferenceRate = BigDecimalUtils.div(BigDecimalUtils.mul(thirdMaxEvalPrice, deviationPercent), new BigDecimal(100));
                        minDifference = BigDecimalUtils.sub(maxEvalPrice, thirdMaxEvalPrice);
                    }

                    if (specimenFilterConditionItem.isInclude()) {
                        if (minDifference.compareTo(minDifferenceRate) >= 0) {
                            if (specimenFilterConditionItem.isRemoveLow()) {
                                removeMin = true;
                            } else {
                                removeMax = true;
                            }
                        }
                    } else if (minDifference.compareTo(minDifferenceRate) > 0) {
                        if (specimenFilterConditionItem.isRemoveLow()) {
                            removeMin = true;
                        } else {
                            removeMax = true;
                        }
                    }
                }
            }

            if (removeMin) {
                if (priceScore != null) {
                    tool.setDevaultScore((BigDecimal)tool.getEffectiveEvalPriceList().get(tool.getEffectiveEvalPriceList().size() - 1), tool.getSpecimenParam().getPriceScore());
                }

                tool.removeInvalidHighOrLowEvalPrice(0, 1);
            }

            if (removeMax) {
                if (priceScore != null) {
                    tool.setDevaultScore((BigDecimal)tool.getEffectiveEvalPriceList().get(0), tool.getSpecimenParam().getPriceScore());
                }

                tool.removeInvalidHighOrLowEvalPrice(1, 0);
            }
        } else {
            downFloatPercent = basePriceRangeParam.getHeightSecondFirstDeviationPercent();
            isIncludeDispersionFilterMinRange = basePriceRangeParam.isIncludeHeightSecondFirstDeviationPercent();
            upFloatPercent = basePriceRangeParam.getHeightAvgDeviationPercent();
            isIncludeDispersionFilterMaxRange = basePriceRangeParam.isIncludeHeightAvgDeviationPercent();
            BigDecimal minDifferenceRate1 = BigDecimalUtils.div(BigDecimalUtils.mul(nextMaxEvalPrice, downFloatPercent), new BigDecimal(100));
            effectPrice = BigDecimalUtils.sub(maxEvalPrice, nextMaxEvalPrice);
            boolean max_nextMax = false;
            if (isIncludeDispersionFilterMinRange) {
                if (effectPrice.compareTo(minDifferenceRate1) >= 0) {
                    max_nextMax = true;
                } else if (effectPrice.compareTo(minDifferenceRate1) > 0) {
                    max_nextMax = true;
                }
            }

            deviationPercent = this.getAvgEvaPriceRemoveMax(tool.getEffectiveEvalPriceList());
            minDifferenceRate1 = BigDecimalUtils.div(BigDecimalUtils.mul(deviationPercent, upFloatPercent), new BigDecimal(100));
            minDifference = BigDecimalUtils.sub(maxEvalPrice, deviationPercent);
            boolean avgMax = false;
            if (isIncludeDispersionFilterMaxRange) {
                if (minDifference.compareTo(minDifferenceRate1) >= 0) {
                    avgMax = true;
                } else if (minDifference.compareTo(minDifferenceRate1) > 0) {
                    avgMax = true;
                }
            }

            if (max_nextMax && avgMax && priceScore != null) {
                tool.setDevaultScore((BigDecimal)tool.getEffectiveEvalPriceList().get(0), tool.getSpecimenParam().getPriceScore());
                tool.removeInvalidHighOrLowEvalPrice(1, 0);
            }
        }

    }

    private BigDecimal getAvgEvaPriceRemoveMax(List<BigDecimal> effectEvaPriceList) {
        List<BigDecimal> newEffectEvaPriceList = new ArrayList();
        Iterator var4 = effectEvaPriceList.iterator();

        BigDecimal total;
        while(var4.hasNext()) {
            total = (BigDecimal)var4.next();
            newEffectEvaPriceList.add(total);
        }

        newEffectEvaPriceList.remove(0);
        total = BigDecimal.ZERO;

        BigDecimal evaluationPrice;
        for(Iterator var5 = newEffectEvaPriceList.iterator(); var5.hasNext(); total = BigDecimalUtils.add(total, evaluationPrice)) {
            evaluationPrice = (BigDecimal)var5.next();
        }

        return BigDecimalUtils.div(total, BigDecimal.valueOf((long)newEffectEvaPriceList.size()));
    }
}
