<?php

class SelectDomainAction extends CartAction {
	
	const OPERATION_REGISTER = 'register';
	const OPERATION_TRANSFER = 'transfer';
	const OPERATION_USE = 'use';

	public function execute($request) {
		
		if ($request->isMethod('post')) {
			
			try {
				if (!$operation = $request->getParameter('operation')) {
					throw new InputException('TRANS_YOU_MUST_SPECIFY_DOMAIN_NAME');
				}
				if ($operation != self::OPERATION_USE) {
					$domainPlanId = $request->getParameter('domainPlanId');
					$tmp = mb_split('\.', $request->getParameter('SLD'), 2);
					$SLD = isset($tmp[0]) ? trim($tmp[0]) : false;
					if (!($domainPlanId) ||	!($SLD)) {
						throw new InputException('TRANS_YOU_MUST_SPECIFY_DOMAIN_NAME');
					}
					$masterTLD = $request->getParameter('masterTLD');
					if (!ResourceLocator::getInstance()->billingDomainService->isDomainValid($SLD.".".$masterTLD)) {
						if (self::OPERATION_REGISTER == $request->getParameter('operation')) {
							$this->registerSLD = $SLD;
						} else {
							$this->transferSLD = $SLD;
						}
						throw new InputException('TRANS_YOU_MUST_SPECIFY_DOMAIN_NAME');
					}

					$selectDomainParameters = new sfParameterHolder();
					$selectDomainParameters->add(
						array(
							'domainPlanId' => $domainPlanId,
							'masterTLD' => $masterTLD,
							'SLD' => $SLD,
							'operation' => $operation
						)
					);
					sfContext::getInstance()->getUser()->setAttribute('selectDomainParameters', $selectDomainParameters);
					$this->_router->next();
				} else {

					$tmp = mb_split('\.', $request->getParameter('domainName'), 2);
					$SLD = isset($tmp[0]) ? trim($tmp[0]) : false;
					$TLD = isset($tmp[1]) ? trim($tmp[1]) : false;
					if (!$SLD || !$TLD) {
						throw new InputException('TRANS_YOU_MUST_SPECIFY_DOMAIN_NAME_WITH_TLD');
					}
					
					$domainName = $SLD . '.' . $TLD;
					if (!ResourceLocator::getInstance()->billingDomainService->isDomainValid($domainName)) {
						$this->useDomainName = $domainName;
						throw new InputException('TRANS_YOU_MUST_SPECIFY_DOMAIN_NAME');
					}
					
					if ($currentCartItem = $this->_cart->getCurrentItem()) {
						foreach ($currentCartItem->getPurchases(true) as $purchase) {
							if ($purchase->getType() == BillingPlan::TYPE_DOMAIN) {
								$data = $purchase->getParameter('domainData');
								if ($domainName == $data['name']) {
									throw new InputException('TRANS_STORE_ALREADY_REGISTER_OR_TRANSFER_DOMAIN');
								}
							}
						}
					}
					
					$purchase = ResourceLocator::getInstance()->cart->getCurrentItem()->getMasterPurchase();
					$domainData = array(
						'SLD' => $SLD,
						'TLD' => $TLD,
						'name' => $domainName,
					);
					$purchase->addOwnDomain($domainData);
					$this->_router->next(array('register-domain'));
				}
				
			} catch (InputException $exception) {
				$this->errorMessage = $exception->getMessage();
			}
			
		}
		
		$cartItem = $this->_cart->getCurrentItem();
		
		$plan = $this->_store->getPlan($cartItem->getPlanId(), true);
		
		$this->allowRegister = $allowRegister = in_array('dom_reg', $this->_store->domainOptions);
		$this->allowTransfer = $allowTransfer = in_array('dom_transfer', $this->_store->domainOptions);
		$this->allowUseOwn = in_array('dom_existing', $this->_store->domainOptions);
		
		if ($allowRegister || $allowTransfer) {
			if ($domainPlan = $plan instanceof BillingDomainPlan ? $plan : $this->_store->getDefaultDomainPlan(true)) {
				try {
					if (!$domains = $domainPlan->getDomains($this->_store->getId())) {
						throw new BillingObjectException('Empty domains list');
					}
					$TLDs = array();
					foreach ($domains as $domain) {
						$TLDs[] = $domain->tldExtension;
					}
					$this->TLDs = $TLDs;
					$lowestCyclePrice = $domainPlan->getLowestCyclePrice($this->_store->getId(), $this->_store->currencyID);
					$this->lowestCyclePrice = $lowestCyclePrice;
					$lowestTransferPrice = $domainPlan->getLowestTransferPrice($this->_store->getId(), $this->_store->currencyID);
					$this->lowestTransferPrice = $lowestTransferPrice;
					$this->cycles = $domainPlan->getCycles();
					$this->domainPlan = $domainPlan;
				} catch (StoreException $exception) {
					$this->logMessage('Unable to get domains for domain plan [id=' . $domainPlan->getId() . ']; ' . get_class($exception) . ' message: ' . $exception->getMessage(), 'err');
				}
			} else {
				$this->logMessage('Unable to get domain plan for purchasing plan [id=' . $plan->getId() . '] via store [id=' . $this->_store->getId() . ']', 'warning');
			}
		}
		
		if (!isset($operation) || !$operation) {
			if (isset($domainPlan) && $allowRegister && isset($lowestCyclePrice)) {
				$operation = self::OPERATION_REGISTER;
			} else if (isset($domainPlan) && $allowTransfer && isset($lowestTransferPrice)) {
				$operation = self::OPERATION_TRANSFER;
			} else {
				$operation = self::OPERATION_USE;
			}
		}
		$this->operation = $operation;
		
		$this->plan = $plan;
		if ($defaultTaxZoneId = isset($this->_store->defaultTaxZoneID) ? $this->_store->defaultTaxZoneID : false) {
			$this->taxZone = ResourceLocator::getInstance()->billingTaxZoneService->getById($defaultTaxZoneId);
		}
		
	}
}