<?php
namespace App\Updater;

class NewVersion extends \App\Updater\Base
{

	/**
	 * Check if new version for current entity is available and save into database some information
	 * @throws \App\MessagesException
	 */
	public function checkNextVersion()
	{
		// load global CFG
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// instantiate messages collector
		$msgs = new \App\MessagesCollector();

		if (!\DB::isInsideTransaction()) {
			throw new \Exception("Must start a transaction before starting working with database");
		}

		// CHECK CURRENT status
		if ($this->getCurrentStatus() != 'START') {
			throw new \App\MessagesException($msgs->addError('Invalid current status'));
		}

		// GET VERSIONS LIST
		$versions = $this->getVersionsDetails();

		if (!$versions) {
			$this->saveMessages($msgs->addInfo('There is no available version for this instance'));
		}
		else {
			$updaterCfg = \App\Config\UpdaterVars::getAdmin();

			$currentBuild = $CFG['global']['application']['versionBuild'];

			// filter all versions bigger than current build
			$versions = array_filter($versions, function($value) use ($currentBuild) {
    			return $value['build'] > $currentBuild;
			});


			if (!$versions) {
				$this->saveMessages($msgs->addInfo('There is no available version for this instance'));
			}
			else {
				foreach ($versions as $key => $row) {
					$build[$key]  = $row['build'];
					$idInstanceKit[$key] = $row['idInstanceKit'];
				}

				// sort by build and id instance kit
				array_multisort($build, SORT_ASC, $idInstanceKit, SORT_DESC, $versions);

				// CHANGE status IN AVAILABLE_VERSION
				$updaterCfg->set('status', 'AVAILABLE_VERSION');

				// save current build
				$updaterCfg->set('currentBuild', $currentBuild);

				// save next version
				$updaterCfg->set('nextVersion', $versions[0]['version']);

				// save next build
				$updaterCfg->set('nextBuild', $versions[0]['build']);

				// save close date
				$updaterCfg->set('versionClosingDate', $versions[0]['closeDate']);

				// save add time
				$updaterCfg->set('requestTime', \Engine\DateTime::create()->formatDatetimeIso());

				// save add time of kit
				$updaterCfg->set('addTime', $versions[0]['addTime']);

				// save file size
				$updaterCfg->set('fileSize', $versions[0]['fileSize']);

				// save md5 file
				$updaterCfg->set('md5File', $versions[0]['md5File']);

				// save product version comments
				$updaterCfg->set('productVersionComments', $versions[0]['productVersionComments']);

				// save product version patch comments
				if ($versions[0]['productVersionPatchComments']) {
					$updaterCfg->set('productVersionPatchComments', $versions[0]['productVersionPatchComments']);
				}

				// save id kit
				$updaterCfg->set('newIdInstanceKit', $versions[0]['idInstanceKit']);

				// save id patch
				$updaterCfg->set('newIdProductVersionPatch', (String)$versions[0]['idProductVersionPatch']);

				// remove message
				$updaterCfg->delete('message');

				$updaterCfg->save();
			}
		}
	}

}