<?php

declare(strict_types=1);

namespace mirzaev\huesos\models;

// Files of the project
use mirzaev\huesos\models\core,
	mirzaev\huesos\models\enumerations\delivery as company;

// Built-in libraries
use exception;

/**
 * Model of settings
 *
 * @package mirzaev\huesos\models
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class delivery extends core
{
	/**
	 * Ready
	 *
	 * @param company $company Delivery company
	 * @param array $parameters Delivery company parameters
	 * @param array &$errors Registry of errors
	 *
	 * @return bool|null Is delivery data ready for creating order?
	 */
	public static function ready(company $company, array $parameters, array &$errors = []): ?bool
	{
		try {
			if (!empty($parameters)) {
				// Initialized delivery company data

				if (
					!empty($parameters['type'])
					&& !empty($parameters['tariff'])
					&& (!empty($parameters['location']['name']) || (!empty($parameters['longitude']) && !empty($parameters['latitude'])))
					&& !empty($parameters['street'])
				) {
					// Initialized required parameters: company, type, tariff, location (or longitude with latitude), street

					if ($parameters['type'] === 'door') {
						// Delivery to the door

						if (!empty($parameters['apartament'])) {
							// Required parameters initialized: apartament

							// Exit (success)
							return true;
						} else {
							// Not initialized required parameters

							// Exit (success)
							return false;
						}
					} else if ($parameters['type'] === 'office') {
						// Delivery to the office

						// Exit (success)
						return true;
					} else {
						// Not identified delivery type

						// Exit (success)
						return false;
					}
				} else {
					// Not initialized required parameters
			
					// Exit (success)
					return false;
				}
			}
		} catch (exception $e) {
			// Writing to the registry of errors
			$errors[] = [
				'text' => $e->getMessage(),
				'file' => $e->getFile(),
				'line' => $e->getLine(),
				'stack' => $e->getTrace()
			];
		}

		// Exit (fail)
		return null;
	}
}
