package AbsenceSchedules;

import ScheduleUtil.EmployeeCostCalculator;
import ScheduleUtil.SchedulesArray;
import jdistlib.rng.RandomEngine;

public class AbsenceSchedulesArray extends SchedulesArray{

    private final SchedulesArray initialSchedulesArray;
    public int numberOfRecoveringActionsToPerform;
    
    public AbsenceSchedulesArray(SchedulesArray initialSchedulesArray, RandomEngine r) {
        super(initialSchedulesArray);
        this.initialSchedulesArray = initialSchedulesArray;
        generateAbsenceSimulation(r);
        this.totalScheduleCost = EmployeeCostCalculator.getFullAbsenceScheduleCost(this, myParametersInitialSchedules);
    } 

    private void generateAbsenceSimulation(RandomEngine r) {
        
        for (int i = 0; i < getNumberPartTimeEmployee(); i++) {
            boolean[] a = new AbsencesVector(myParametersInitialSchedules.getWorkPeriodsPerSchedule(), r).getAbsencesVector();
            for (int j = 0; j < myParametersInitialSchedules.getWorkPeriodsPerSchedule(); j++) {
                this.partTimeSchedules[i][j] = this.initialSchedulesArray.isPartTimeEmployeeWorking(i, j) && a[j];
            }
        }

        for (int i = 0; i < getNumberFullTimeEmployee(); i++) {
            boolean[] a = new AbsencesVector(myParametersInitialSchedules.getWorkPeriodsPerSchedule(), r).getAbsencesVector();
            for (int j = 0; j < myParametersInitialSchedules.getWorkPeriodsPerSchedule(); j++) {
                this.fullTimeSchedules[i][j] = this.initialSchedulesArray.isFullTimeEmployeeWorking(i, j) && a[j];
            }
        }

        calculateNumberOfRecoveringActionsToPerform();

    }
    
    public int getAbsencePeriodsPerPartTimeEmployees(int employee) {
        int absencePeriodsPerPartTimeEmployees = 0;
        for (int workPeriod = 0; workPeriod < myParametersInitialSchedules.getWorkPeriodsPerSchedule(); workPeriod++) {
            if (this.isPartTimeEmployeeAbsent(employee, workPeriod)) {absencePeriodsPerPartTimeEmployees += 1;}
        }
        return absencePeriodsPerPartTimeEmployees;
    }
    
    public int getAbsencePeriodsPerFullTimeEmployees(int employee) {
        int absencePeriodsPerFullTimeEmployees = 0;
        for (int workPeriod = 0; workPeriod < myParametersInitialSchedules.getWorkPeriodsPerSchedule(); workPeriod++) {
            if (this.isFullTimeEmployeeAbsent(employee, workPeriod)) {absencePeriodsPerFullTimeEmployees += 1;}
        }
        return absencePeriodsPerFullTimeEmployees;
    }

    public boolean isPartTimeEmployeeAbsent(int employee, int workPeriod) {
        return this.partTimeSchedules[employee][workPeriod] != this.initialSchedulesArray.isPartTimeEmployeeWorking(employee, workPeriod);
    }
    
    public boolean isFullTimeEmployeeAbsent(int employee, int workPeriod) {
        return this.fullTimeSchedules[employee][workPeriod] != this.initialSchedulesArray.isFullTimeEmployeeWorking(employee, workPeriod);
    }
    
    private void calculateNumberOfRecoveringActionsToPerform() {
        this.numberOfRecoveringActionsToPerform = 0;
        for (int workPeriod = 0; workPeriod < myParametersInitialSchedules.getWorkPeriodsPerSchedule(); workPeriod++) {
            this.numberOfRecoveringActionsToPerform += this.myParametersInitialSchedules.getRequiredWorkForce(workPeriod) - this.getEmployeesWorkingPerWorkPeriod(workPeriod);
        }
    }
    
}