<?php
App::uses('ModelBehavior', 'Model');

App::uses('Modifier', 'Model');
App::uses('Safeguard', 'Model');
App::uses('ProgramChildPlacement', 'Model');

class TrainingBehavior extends ModelBehavior
{
	private $heartInit = 180;

	public function setup(Model $Model, $settings = array())
	{
		$this->Modifier = new Modifier();
	}

	public function getHeartRates(Model $Model, $programId, $age)
	{
		$age = new DateTime($age); $now = new DateTime();
		$age = $now->diff($age);

		$ageModifier = -round($age->y - abs($age->y-40)*0.25);

		$program = $Model->Program->find('first', array(
			'Program.id' => $programId,
		));

		$workoutModifier = 0;

		if($program['Program']['workouts_now'] <= 0)
		{
			$workoutModifier -= 5;
		}
		if($program['Program']['workouts_last_year'] >= 6)
		{
			$workoutModifier += 5;
		}
		if($program['Program']['workouts_previous_year'] >= 6)
		{
			$workoutModifier += 1;
		}
		if($program['Program']['workouts_year_before'] >= 6)
		{
			$workoutModifier += 1;
		}

		$totalMaxAerobic= $this->heartInit + $ageModifier + $workoutModifier;

		$zones = $Model->Program->Workout->Zone->find('all', array('recursive' => -1));

		foreach ($zones as $zone)
		{
			switch ($zone['Zone']['value'])
			{
				case 'recovery' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic - 35;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = $totalMaxAerobic - 10;
					breaK;
				case 'aerobic_development' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic - 25;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = $totalMaxAerobic - 5;
					breaK;
				case 'endurance_development' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic - 20;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = $totalMaxAerobic;
					breaK;
				case 'aerobic_stimulation' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic - 10;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = $totalMaxAerobic;
					breaK;
				case 'tempo' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = $totalMaxAerobic + 15;
					breaK;
				case 'speed' :
					$zoneHeartRates[$zone['Zone']['id']]['min'] = $totalMaxAerobic;
					$zoneHeartRates[$zone['Zone']['id']]['max'] = abs($age->y - 220);
					breaK;
			}
			$zoneHeartRates[$zone['Zone']['id']]['value'] = $zone['Zone']['value'];
			$zoneHeartRates[$zone['Zone']['id']]['initial'] = $zone['Zone']['initial'];
			$zoneHeartRates[$zone['Zone']['id']]['name'] = $zone['Zone']['name'];
			$zoneHeartRates[$zone['Zone']['id']]['id'] = $zone['Zone']['id'];
		}

		$races = $Model->Program->Race->find('all', array('recursive' => -1));

		foreach ($races as $race)
		{
			switch ($race['Race']['value'])
			{
				case 'sprint' :
					$raceHeartRates[$race['Race']['id']]['min'] = $totalMaxAerobic + 10;
					$raceHeartRates[$race['Race']['id']]['max'] = $totalMaxAerobic + 25;
					breaK;
				case 'olympic' :
					$raceHeartRates[$race['Race']['id']]['min'] = $totalMaxAerobic + 5;
					$raceHeartRates[$race['Race']['id']]['max'] = $totalMaxAerobic + 20;
					breaK;
				case 'half-ironman' :
					$raceHeartRates[$race['Race']['id']]['min'] = $totalMaxAerobic;
					$raceHeartRates[$race['Race']['id']]['max'] = $totalMaxAerobic + 15;
					breaK;
				case 'ironman' :
					$raceHeartRates[$race['Race']['id']]['min'] = $totalMaxAerobic - 15;
					$raceHeartRates[$race['Race']['id']]['max'] = $totalMaxAerobic;
					breaK;
			}
			$raceHeartRates[$race['Race']['id']]['value'] = $race['Race']['value'];
			$raceHeartRates[$race['Race']['id']]['name'] = $race['Race']['name'];
			$raceHeartRates[$race['Race']['id']]['id'] = $race['Race']['id'];
		}

		return array(
			'totalMaxAerobic' => $totalMaxAerobic,
			'Race' => $raceHeartRates,
			'Zone' => $zoneHeartRates,
		);
	}

}

?>
