<?php
App::uses('AppModel', 'Model');
/**
 * Workout Model
 *
 * @property Program $Program
 * @property Sport $Sport
 * @property Zone $Zone
 * @property TempoType $TempoType
 * @property StrengthType $StrengthType
 */
class Workout extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'id';

	public $actsAs = array('Containable', 'Adjustment');

	public $virtualFields = array
	(
		'day' => 'DAY(Workout.date)',
		'month' => 'MONTH(Workout.date)',
		'year' => 'YEAR(Workout.date)',
	);

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'heart_min' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'heart_max' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'value' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'complete' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				'allowEmpty' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'brick' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'program_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'sport_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'zone_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'strength_type_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'date' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Program' => array(
			'className' => 'Program',
			'foreignKey' => 'program_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Sport' => array(
			'className' => 'Sport',
			'foreignKey' => 'sport_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Zone' => array(
			'className' => 'Zone',
			'foreignKey' => 'zone_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'TassPlacementType' => array(
			'className' => 'TassPlacementType',
			'foreignKey' => 'tass_placement_type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'StrengthType' => array(
			'className' => 'StrengthType',
			'foreignKey' => 'strength_type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	public function getComplete($id, $userId)
	{
		$workout = $this->find('first', array(
				'conditions' => array(
					'Workout.id' => $id,
					'Program.user_id' => $userId,
				),
                'contain' => array(
                    'Program',
                )
		));

		if (empty($workout)) {
			return false;
		}

        if (isset($workout['Workout']['shape']) && is_null($workout['Workout']['complete'])) {
            return 100;
        }

		return intval($workout['Workout']['complete']);
	}

	public function getShape($id, $userId)
	{
		$workout = $this->find('first', array(
				'conditions' => array(
					'Workout.id' => $id,
					'Program.user_id' => $userId,
				),
                'contain' => array(
                    'Program',
                )
		));

		if (empty($workout)) {
			return false;
		}

        if (is_null($workout['Workout']['shape']) && isset($workout['Workout']['complete'])) {
            return 100;
        }

		return intval($workout['Workout']['shape']);
	}

	public function updateComplete($id, $userId, $complete)
	{
        ini_set('memory_limit', '256M');
		$workout = $this->find('first', array(
				'conditions' => array(
					'Workout.id' => $id,
					'Program.user_id' => $userId,
				),
                'contain' => array(
                    'Program',
                    'Sport',
                )
		));

		return $this->_updateAdjust($workout, 'complete', $complete);
	}

	public function updateShape($id, $userId, $shape)
	{
        ini_set('memory_limit', '256M');
		$workout = $this->find('first', array(
				'conditions' => array(
					'Workout.id' => $id,
					'Program.user_id' => $userId,
				),
                'contain' => array(
                    'Program',
                    'Sport',
                )
		));

		$shapeValue = $this->shape2Value($shape);

		return $this->_updateAdjust($workout, 'shape', $shapeValue);
	}

	protected function _updateAdjust($workout, $field, $value = 100)
	{
        ini_set('memory_limit', '256M');
		if(!empty($workout)) {
			$this->clear();
			$this->id = $workout['Workout']['id'];
			if ($this->saveField($field, $value)) {
				$adjustedWorkouts = $this->adjust($workout);

				if($this->saveAll($adjustedWorkouts)) {

					$data = array();

					foreach ($adjustedWorkouts as $adjustedWorkout) {
						if($adjustedWorkout['month'] <= $workout['Workout']['month'] + 1 &&
						$adjustedWorkout['month'] >= $workout['Workout']['month'] - 1) {
							array_push($data, $adjustedWorkout);
						}
					}

					return array(
						'adjustment' => $data,
						'scheda' => array(
							'hide' => $this->getScheda($workout, 'hide'),
							'as' => $this->getScheda($workout, 'as'),
						)
					);
				}
			}
		}

		return false;
	}
}
