<?php

class ContactInformationAction extends CartAction {
	
	const OPEARION_SKIP = 'skip';
	const OPEARION_CLEAN = 'clean';
	const OPERAION_SIGN_IN = 'sign-in';
	const OPERAION_SIGN_OUT = 'sign-out';
	const OPEARION_REGISTER = 'register';
	
	public function execute($request) {
		$this->form = ResourceLocator::getInstance()->billingContactService->getCreateCustomerForm($this->_store->getId());
		
		if ($request->isMethod('post')) {
			
			$operation = $request->getParameter('operation');
			switch ($operation) {
				case self::OPEARION_SKIP:
					$this->_router->next();
					break;
				case self::OPEARION_CLEAN:
					$this->getUser()->setAttribute('customerData', null);
					$this->_router->refresh();
					break;
				case self::OPERAION_SIGN_IN:
					$router = new Router();
					$router->init(
						array(
							new RouterPage('sign-in'),
							new RouterPage('contact-information')
						),
						new RouterPage('contact-information')
					);
					ResourceLocator::getInstance()->routersStack->push($router);
					$router->next();
					break;
				case self::OPERAION_SIGN_OUT:
					$this->getUser()->getAttributeHolder()->remove('customer');
					$this->_router->refresh();
					break;
				default:
					try {
						$this->form->bind($request->getParameter($this->form->getName()));
						if ($this->form->isValid()) {
							if ($this->form->save()) {
								$this->_router->next();
							}
						}
					} catch (BillingFormException $exception) {
						$this->errorMessage = $exception->getMessage();
					}
			}
			
		}
			
		$this->customer = $this->getUser()->getAttribute('customer', null);
		$this->customerData = $this->getUser()->getAttribute('customerData', null);
		
	}
}