<?php

class MainCtrl extends Ctrl {



	public static $products = array(
		1 => array('name' => 'Whistle', 'image' => 'whistle.jpeg'),
		2 => array('name' => 'Stapler', 'image' => 'stapler.jpeg'),
		3 => array('name' => 'Duck', 'image' => 'duck.jpeg')
	);



	public function show() {
		$v = new VBox();
		$v->add($this->getShop());
		$v->add($this->getCart());
		$v->add($this->getBin());
		return $v;
	}



	public function getShop() {
		$shop = new HBox('shop');
		foreach (self::$products as $id => $product) {
			$p = &new View();
			$p->template = 'app/Product.tpl';
			$p->name = $product['name'];
			$p->image = $product['image'];
			$p->addListener(new DragStartListener('to-cart', new Action('MainCtrl/toCart', $id)));
			$shop->add($p);
		}
		return $shop;
	}



	public function getCart() {
		$cart = new View('cart');
		$products = &$_SESSION['cart'];
		foreach ((array)$products as $k=>$id) {
			$p = &new View();
			$p->template = 'app/ProductInCart.tpl';
			$p->name = self::$products[$id]['name'];
			$p->image = self::$products[$id]['image'];
			$p->addListener(new DragStartListener('to-bin', new Action('MainCtrl/toBin', $k)));
			$cart->add($p);
		}
		$cart->addListener(new DropOntoListener('to-cart', new Action('MainCtrl/toCart')));
		return $cart;
	}



	public function getBin() {
		$bin = new View('bin');
		$bin->addText('Drop item from cart on this area to remove');
		$bin->addListener(new DropOntoListener('to-bin', new Action('MainCtrl/toBin')));
		return $bin;
	}



	public function toCart($id) {
		$_SESSION['cart'][] = $id;
		return $this->getCart();
	}



	public function toBin($key) {
		unset($_SESSION['cart'][$key]);
		return $this->getCart();
	}



} // class MainCtrl

?>