

package org.powertac.householdcustomer.appliances;

import java.util.Properties;
import java.util.Vector;

import org.powertac.common.repo.RandomSeedRepo;
import org.powertac.common.spring.SpringApplicationContext;
import org.powertac.householdcustomer.configurations.VillageConstants;

/**
 * ICT are the appliances that are utilized mainly for work or to communicate
 * with others from the household such as computers, telephone devices etc. They
 * works only when someone is at home and uses them. So it's a not shifting
 * appliance.
 * 
 * @author Antonios Chrysopoulos
 * @version 1.5, Date: 2.25.12
 */
public class ICT extends NotShiftingAppliance
{

  @Override
  public void initialize (String household, Properties conf, int seed)
  {
    // Filling the base variables
    name = household + " ICT";
    randomSeedRepo =
      (RandomSeedRepo) SpringApplicationContext.getBean("randomSeedRepo");
    gen =
      randomSeedRepo.getRandomSeed(toString(), seed, "Appliance Model" + seed);
    saturation = 1;
    power =
      (int) (VillageConstants.ICT_POWER_VARIANCE * gen.nextGaussian() + VillageConstants.ICT_POWER_MEAN);
    cycleDuration = VillageConstants.ICT_DURATION_CYCLE;
    times =
      Integer.parseInt(conf.getProperty("ICTDailyTimes"))
              + applianceOf.getMembers().size();

  }

  @Override
  public void fillDailyOperation (int weekday)
  {
    // Initializing and Creating auxiliary variables
    loadVector = new Vector<Integer>();
    dailyOperation = new Vector<Boolean>();

    for (int i = 0; i < VillageConstants.QUARTERS_OF_DAY; i++) {

      dailyOperation.add(false);
      loadVector.add(0);

    }

    Vector<Integer> temp = new Vector<Integer>();

    for (int i = 0; i < VillageConstants.QUARTERS_OF_DAY; i++) {

      int count = applianceOf.tenantsNumber(weekday, i);
      for (int j = 0; j < count; j++) {
        temp.add(i);
      }

    }

    if (temp.size() > 0) {
      for (int i = 0; i < times; i++) {
        int rand = gen.nextInt(temp.size());
        int quarter = temp.get(rand);

        dailyOperation.set(quarter, true);
        loadVector.set(quarter, (loadVector.get(quarter) + power));
        temp.remove(rand);
        if (temp.size() == 0)
          break;
      }
    }

    weeklyLoadVector.add(loadVector);
    weeklyOperation.add(dailyOperation);
  }

  @Override
  public void refresh ()
  {

    fillWeeklyOperation();
    createWeeklyPossibilityOperationVector();
  }

}
