/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.wicket.ui;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import com.pragprog.dhnako.carserv.dom.customer.Customer;
import com.pragprog.dhnako.carserv.dom.customer.CustomerRepository;
import com.pragprog.dhnako.carserv.wicket.ui.security.CarServSignOutPage;
import com.pragprog.dhnako.carserv.wicket.ui.security.UserPanel;

@AuthorizeInstantiation("USER")
public class Index extends NakedObjectsWebPage {
	
	private abstract class ReplaceLink<T> extends Link {
		private static final long serialVersionUID = 1L;

		private ReplaceLink(String id, IModel object) {
			super(id, object);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onClick() {
			T selected = (T) getModelObject();
			Panel replacement = getReplacementPanel(detailsRegion.getId(), selected);
			detailsRegion.replaceWith(replacement);
			detailsRegion = replacement;
		}

		protected abstract Panel getReplacementPanel(String id, T selected);

	}
	
	private transient CustomerRepository customerRepository;
	private Panel detailsRegion = new EmptyPanel("detailsRegion");

	
	public Index() {
		PageableListView customerList = new PageableListView("customerList",
				new LoadableDetachableModel() {
					private static final long serialVersionUID = 1L;
					@Override
					protected Object load() {
						return getCustomerRepository().allCustomers();
					}}, 5) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				Customer customer = (Customer) item.getModelObject();
				String customerName = customer.getPerson().getName().title();
				item.add(new Label("name", customerName));

				item.add(new ReplaceLink<Customer>("showVehiclesLink", item.getModel()){
					private static final long serialVersionUID = 1L;
					@Override
					protected Panel getReplacementPanel(String id, Customer selected) {
						return new CustomerVehiclesPanel(id, selected);
					}
				});

				item.add(new ReplaceLink<Customer>("editCustomerLink", item.getModel()) {
					private static final long serialVersionUID = 1L;
					@Override
					protected Panel getReplacementPanel(String id, Customer selected) {
						return new EditCustomerPanel(id, selected);
					}
				});
			}
		};
		add(customerList);
		
		add(detailsRegion);
		
	    add(new UserPanel("userPanel", CarServSignOutPage.class));
	}

	public CustomerRepository getCustomerRepository() {
		if (customerRepository == null) {
			injectServices();
		}
		return customerRepository;
	}
	public void setCustomerRepository(CustomerRepository customerRepository) {
		this.customerRepository = customerRepository;
	}
	

}
