package org.optaplanner.examples.projectscheduling.solver.score.capacity;

import java.util.HashMap;
import java.util.Map;

import org.optaplanner.examples.projectscheduling.domain.Allocation;
import org.optaplanner.examples.projectscheduling.domain.ResourceRequirement;
import org.optaplanner.examples.projectscheduling.domain.resource.Resource;

public class RenewableResourceCapacityTracker extends ResourceCapacityTracker {

    protected int capacity;

    protected Map<Integer, Integer> usedPerDay;
    protected int hardScore;

    public RenewableResourceCapacityTracker(Resource resource) {
        super(resource);
        if (!resource.isRenewable()) {
            throw new IllegalArgumentException("The resource (" + resource + ") is expected to be renewable.");
        }
        capacity = resource.getCapacity();
        usedPerDay = new HashMap<Integer, Integer>();
        hardScore = 0;
    }

    @Override
    public void insert(ResourceRequirement resourceRequirement, Allocation allocation) {
        Integer startDate = allocation.getStartDate();
        Integer endDate = allocation.getEndDate();
        if (startDate != null && endDate != null) {
            int requirement = resourceRequirement.getRequirement();
            for (int i = startDate; i < endDate; i++) {
                Integer used = usedPerDay.get(i);
                if (used == null) {
                    used = 0;
                }
                if (used > capacity) {
                    hardScore += (used - capacity);
                }
                used += requirement;
                if (used > capacity) {
                    hardScore -= (used - capacity);
                }
            }
        }
    }

    @Override
    public void retract(ResourceRequirement resourceRequirement, Allocation allocation) {
        Integer startDate = allocation.getStartDate();
        Integer endDate = allocation.getEndDate();
        if (startDate != null && endDate != null) {
            int requirement = resourceRequirement.getRequirement();
            for (int i = startDate; i < endDate; i++) {
                Integer used = usedPerDay.get(i);
                if (used == null) {
                    used = 0;
                }
                if (used > capacity) {
                    hardScore += (used - capacity);
                }
                used -= requirement;
                if (used > capacity) {
                    hardScore -= (used - capacity);
                }
            }
        }
    }

    @Override
    public int getHardScore() {
        return hardScore;
    }

}

@@ -448,16 +448,24 @@
 capacity
+EveryDay
 ;%0A%0A    p
@@ -804,16 +804,24 @@
 capacity
+EveryDay
  = resou
@@ -1469,32 +1469,40 @@
 (used %3E capacity
+EveryDay
 ) %7B%0A            
@@ -1530,32 +1530,40 @@
 (used - capacity
+EveryDay
 );%0A             
@@ -1631,32 +1631,40 @@
 (used %3E capacity
+EveryDay
 ) %7B%0A            
@@ -1692,32 +1692,40 @@
 (used - capacity
+EveryDay
 );%0A             
@@ -2307,32 +2307,40 @@
 (used %3E capacity
+EveryDay
 ) %7B%0A            
@@ -2368,32 +2368,40 @@
 (used - capacity
+EveryDay
 );%0A             
@@ -2477,16 +2477,24 @@
 capacity
+EveryDay
 ) %7B%0A    
@@ -2538,16 +2538,24 @@
 capacity
+EveryDay
 );%0A     

rename field to capacityEveryDay