package roart.calculate;

import java.util.Random;

public class CalcComplexNode extends CalcNode {

    double minMutateThresholdRange;
    double maxMutateThresholdRange;

    double threshold;
    boolean useminmaxthreshold;
    boolean usethreshold;

    boolean divideminmaxthreshold;

    int weight;

    boolean changeSignWhole;

    // not mutatable

    boolean useMax;

    //boolean threshMaxValue; //?
    //boolean norm;

    public double getMinMutateThresholdRange() {
        return minMutateThresholdRange;
    }

    public void setMinMutateThresholdRange(double minMutateThresholdRange) {
        this.minMutateThresholdRange = minMutateThresholdRange;
    }

    public double getMaxMutateThresholdRange() {
        return maxMutateThresholdRange;
    }

    public void setMaxMutateThresholdRange(double maxMutateThresholdRange) {
        this.maxMutateThresholdRange = maxMutateThresholdRange;
    }

    public double getThreshold() {
        return threshold;
    }

    public void setThreshold(double threshold) {
        this.threshold = threshold;
    }

    public boolean isUseminmaxthreshold() {
        return useminmaxthreshold;
    }

    public void setUseminmaxthreshold(boolean useminmaxthreshold) {
        this.useminmaxthreshold = useminmaxthreshold;
    }

    public boolean isUsethreshold() {
        return usethreshold;
    }

    public void setUsethreshold(boolean usethreshold) {
        this.usethreshold = usethreshold;
    }

    public boolean isDivideminmaxthreshold() {
        return divideminmaxthreshold;
    }

    public void setDivideminmaxthreshold(boolean divideminmaxthreshold) {
        this.divideminmaxthreshold = divideminmaxthreshold;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isChangeSignWhole() {
        return changeSignWhole;
    }

    public void setChangeSignWhole(boolean changeSignWhole) {
        this.changeSignWhole = changeSignWhole;
    }

    public boolean isUseMax() {
        return useMax;
    }

    public void setUseMax(boolean doBuy) {
        this.useMax = doBuy;
    }

    @Override
    public double calc(double val, double minmaxthreshold2) {
        double minmaxthreshold = maxMutateThresholdRange;
        if (!useMax) {
            minmaxthreshold = minMutateThresholdRange;
        }
        double mythreshold = 0;
        if (usethreshold) {
            mythreshold = threshold;
        }
        if (useminmaxthreshold) {
            mythreshold = minmaxthreshold;
        }
        double myvalue = val - mythreshold;
        if (false && divideminmaxthreshold) {
            myvalue = myvalue / minmaxthreshold;
        }
        
        myvalue = myvalue / (maxMutateThresholdRange - minMutateThresholdRange);
        
        if (changeSignWhole) {
            myvalue = -myvalue;
        }
        return myvalue * weight;
    }

    @Override
    public void randomize() {
        Random rand = new Random();
        getUseminmaxthreshold(rand);
        getUsethreshold(rand);
        getDivideminmaxthreshold(rand);
        getChangeSignWhole(rand);
        getWeight(rand);
        getThreshold(rand);
    }

    private void getThreshold(Random rand) {
        if (useMax) {
            threshold = rand.nextDouble() * maxMutateThresholdRange;
        } else {
            threshold = rand.nextDouble() * minMutateThresholdRange;       
        }
    }

    private void getWeight(Random rand) {
        weight = 1 + rand.nextInt(100);
    }

    private void getChangeSignWhole(Random rand) {
        changeSignWhole = rand.nextBoolean();
    }

    private void getDivideminmaxthreshold(Random rand) {
        divideminmaxthreshold = rand.nextBoolean();
    }

    private void getUsethreshold(Random rand) {
        usethreshold = rand.nextBoolean();
    }

    private void getUseminmaxthreshold(Random rand) {
        useminmaxthreshold = rand.nextBoolean();
    }

    @Override
    public void mutate() {
        Random rand = new Random();
        int task = rand.nextInt(6);
        switch (task) {
        case 0:
            getUseminmaxthreshold(rand);
            break;
        case 1:
            getUsethreshold(rand);
            break;
        case 2:
            getDivideminmaxthreshold(rand);
            break;
        case 3:
            getChangeSignWhole(rand);
            break;
        case 4:
            getWeight(rand);
            break;
        case 5: 
            getThreshold(rand);
            break;
        default:
            System.out.println("Too many");
            break;
        }

    }
}
@@ -248,24 +248,26 @@
 shold;%0A%0A    
+//
 boolean divi
@@ -303,24 +303,26 @@
 eight;%0A%0A    
+//
 boolean chan
@@ -1467,32 +1467,39 @@
 reshold;%0A    %7D%0A%0A
+    /*%0A
     public boole
@@ -1706,32 +1706,39 @@
 hreshold;%0A    %7D%0A
+*/%0A    
 %0A    public int 
@@ -1850,32 +1850,39 @@
  weight;%0A    %7D%0A%0A
+    /*%0A
     public boole
@@ -2053,32 +2053,39 @@
 ignWhole;%0A    %7D%0A
+*/%0A    
 %0A    public bool
@@ -2662,32 +2662,68 @@
  - mythreshold;%0A
+        // if usemax...?%0A        /*%0A
         if (fals
@@ -2811,32 +2811,34 @@
       %7D%0A        
+*/
 %0A        myvalue
@@ -2904,32 +2904,43 @@
 ange);%0A        %0A
+        /*%0A
         if (chan
@@ -2988,32 +2988,43 @@
 alue;%0A        %7D%0A
+        */%0A
         return m
@@ -3195,32 +3195,34 @@
 (rand);%0A        
+//
 getDivideminmaxt
@@ -3228,32 +3228,39 @@
 threshold(rand);
+ // not
 %0A        getChan
@@ -3244,32 +3244,34 @@
  // not%0A        
+//
 getChangeSignWho
@@ -3271,32 +3271,39 @@
 SignWhole(rand);
+ // not
 %0A        getWeig
@@ -3674,32 +3674,39 @@
 nt(100);%0A    %7D%0A%0A
+    /*%0A
     private void
@@ -3899,32 +3899,39 @@
 Boolean();%0A    %7D
+%0A    */
 %0A%0A    private vo
@@ -4237,9 +4237,9 @@
 Int(
-6
+4
 );%0A 
@@ -4423,16 +4423,163 @@
 case 2:%0A
+            getWeight(rand);%0A            break;%0A        case 3: %0A            getThreshold(rand);%0A            break;%0A            /*%0A        case 2:%0A
         
@@ -4718,131 +4718,14 @@
     
-case 4:%0A            getWeight(rand);%0A            break;%0A        case 5: %0A            getThreshold(rand);%0A            break;
+    */
 %0A   

Cut bad ideas from complex node.