package info.bukova.isspst.ui.main.orders.created;

import info.bukova.isspst.AddressUtils;
import info.bukova.isspst.data.Address;
import info.bukova.isspst.data.AddressEmb;
import info.bukova.isspst.data.Order;
import info.bukova.isspst.data.OrderItem;
import info.bukova.isspst.data.SettingsData;
import info.bukova.isspst.data.User;
import info.bukova.isspst.filters.OrderFilter;
import info.bukova.isspst.services.addressbook.AdbService;
import info.bukova.isspst.services.orders.OrderService;
import info.bukova.isspst.services.settings.GlobalSettingsService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.ui.ListViewModel;
import info.bukova.isspst.ui.renderers.OrderCreatedItemRenderer;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Listbox;

public class OrderList extends ListViewModel<Order>
{
	@SuppressWarnings("unused")
	private final static Logger log = LoggerFactory.getLogger(OrderList.class.getName());

	@WireVariable
	protected OrderService orderService;

	@WireVariable
	protected AdbService adbService;

	@WireVariable
	protected GlobalSettingsService settingsService;

	@WireVariable
	protected UserService userService;

	protected OrderItem selectedOrderItem;

	protected List<OrderItem> orderItems;

	protected OrderCreatedItemRenderer orderCreatedItemRenderer;

	@Init(superclass = true)
	public void initOrderList()
	{
		service = orderService;
		dataClass = Order.class;
		formZul = "orderForm.zul";
		dataFilter = new OrderFilter(getFilterTemplate());
		this.orderItems = new ArrayList<OrderItem>();
		this.orderCreatedItemRenderer = new OrderCreatedItemRenderer();
	}

	public List<AddressEmb> getSuppAddresses()
	{
		List<Address> list = adbService.getAll();
		List<AddressEmb> listEmb = AddressUtils.convertListAddressToListAddressEmb(list);
		return listEmb;
	}

	public List<AddressEmb> getDeliveryAddresses()
	{
		SettingsData data = settingsService.getSettings();

		if (data == null)
		{
			return null;
		}

		List<Address> list = data.getAllShippingAddrs();
		List<AddressEmb> listEmb = AddressUtils.convertListAddressToListAddressEmb(list);
		return listEmb;
	}

	public AddressEmb getInvoiceAddress()
	{
		SettingsData data = settingsService.getSettings();

		if (data == null)
		{
			return null;
		}

		Address addr = data.getMainAddress();
		AddressEmb addrEmb = AddressUtils.convertAddressToAddressEmb(addr);
		return addrEmb;
	}

	public List<User> getUsers()
	{
		return userService.getAll();
	}

	public OrderItem getSelectedOrderItem()
	{
		return selectedOrderItem;
	}

	public void setSelectedOrderItem(OrderItem selectedOrderItem)
	{
		this.selectedOrderItem = selectedOrderItem;
	}

	public List<OrderItem> getOrderItems()
	{
		return orderItems;
	}

	public void setOrderItems(List<OrderItem> orderItems)
	{
		this.orderItems = orderItems;
	}

	public OrderCreatedItemRenderer getOrderCreatedItemRenderer()
	{
		return orderCreatedItemRenderer;
	}

	public void setOrderCreatedItemRenderer(OrderCreatedItemRenderer orderCreatedItemRenderer)
	{
		this.orderCreatedItemRenderer = orderCreatedItemRenderer;
	}

	@Command
	@NotifyChange("orderItems")
	public void onChangeSelectOrder(@BindingParam("ctrl") Listbox lb)
	{
		if (lb == null)
		{
			return;
		}

		if (lb.getSelectedIndex() > -1)
		{
			this.orderItems = this.getDataBean().getItems();
		}
		else
		{
			this.orderItems = new ArrayList<OrderItem>();
		}
	}
}
