package org.gcube.portlets.user.reportgenerator.client.targets;

import java.util.ArrayList;

import org.gcube.portlets.d4sreporting.common.shared.BasicComponent;
import org.gcube.portlets.d4sreporting.common.shared.ComponentType;
import org.gcube.portlets.d4sreporting.common.shared.Metadata;
import org.gcube.portlets.d4sreporting.common.shared.RepeatableSequence;
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;
import org.gcube.portlets.user.reportgenerator.client.model.TemplateComponent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SequenceWidget extends Composite {

	private VerticalPanel myPanel;

	private Button addAnotherB = new Button("Add another Entry");
	private Button removeThisB = new Button("Remove Entry");
	private Button selectEntryB = new Button("Select New");
	private Presenter p;
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	HorizontalPanel buttonsWrapperPanel = new HorizontalPanel();
	private ClientSequence owner;
	private RepeatableSequence repSequence;
	private ArrayList<TemplateComponent> seqGroupedComponents = new ArrayList<TemplateComponent>();

	public SequenceWidget(Presenter p, ClientSequence owner, RepeatableSequence repSequence, boolean notRemovable, boolean isNew) {
		this.p = p;
		this.owner = owner;
		this.repSequence = repSequence;
		addAnotherB.getElement().getStyle().setMargin(10, Unit.PX);
		addAnotherB.getElement().getStyle().setWidth(130, Unit.PX);
		addAnotherB.addStyleName("addEntryButton");
		myPanel = new VerticalPanel();
		myPanel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		myPanel.setStyleName("seqWidget");
		int size = repSequence.getGroupedComponents().size();
		GWT.log("SequenceWidget Size = " + size);
		for (int j = 0; j < size; j++) {
			BasicComponent sComp = repSequence.getGroupedComponents().get(j);
			if (j == size-1) {
				myPanel.add(buttonPanel); //add the button before the last element
				buttonPanel.setWidth("100%");
				buttonPanel.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);

				buttonPanel.add(buttonsWrapperPanel);
			}
			//if isNew and is not locked and is of type BODY then put fields to blank
			if (isNew && (!sComp.isLocked()) && 
					(sComp.getType() == ComponentType.BODY || 
					sComp.getType() == ComponentType.BODY_NOT_FORMATTED ||  
					sComp.getType() == ComponentType.DYNA_IMAGE) ) {
				sComp.setPossibleContent("");
				sComp.setMetadata(new ArrayList<Metadata>());
			}
			add(new TemplateComponent(p.getModel(), sComp, p, false, null));
		}
		initWidget(myPanel);

		if (notRemovable) {
			buttonsWrapperPanel.add(addAnotherB);
			addAnotherB.addClickHandler(new ClickHandler() {			
				@Override
				public void onClick(ClickEvent event) {
					addAnother();
				}
			});
		}
		else {
			
			removeThisB.getElement().getStyle().setWidth(130, Unit.PX);
			removeThisB.getElement().getStyle().setMargin(10, Unit.PX);
			removeThisB.addStyleName("deleteEntryButton");
			buttonsWrapperPanel.add(removeThisB);

			removeThisB.addClickHandler(new ClickHandler() {			
				@Override
				public void onClick(ClickEvent event) {
					removeMe();
				}
			});
			
			buttonsWrapperPanel.add(addAnotherB);
			addAnotherB.addClickHandler(new ClickHandler() {			
				@Override
				public void onClick(ClickEvent event) {
					addAnother();
				}
			});
		}
	}
	
	public void enableClear() {
		Button clearB = new Button("Clear Association");
		clearB.getElement().getStyle().setWidth(130, Unit.PX);
		clearB.getElement().getStyle().setMargin(10, Unit.PX);
		clearB.addStyleName("deleteAssociationButton");

		clearB.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				clearAssociation();
			}
		});
		
		buttonsWrapperPanel.insert(clearB, 0);
		
	}
	
	public void clearAssociation() {
		
		GWT.log("Clearing Association");		
		//in the MODEL
		for (BasicComponent bc : repSequence.getGroupedComponents()) {
			if (bc.getType() == ComponentType.BODY_NOT_FORMATTED) {
				bc.setPossibleContent("");
				bc.setMetadata(new ArrayList<Metadata>());
			}			
		}
		//in the VIEW
		for (TemplateComponent tc : seqGroupedComponents) {
			if (tc.getType() == ComponentType.BODY_NOT_FORMATTED) {
				((HTML) tc.getContent()).setText("");				
			}			
		}
		buttonsWrapperPanel.clear();
		selectEntryB.getElement().getStyle().setWidth(130, Unit.PX);
		selectEntryB.getElement().getStyle().setMargin(10, Unit.PX);
		selectEntryB.addStyleName("deleteAssociationButton");
		buttonsWrapperPanel.add(selectEntryB);
	}

	public void add(TemplateComponent toAdd) {
		if (toAdd.getType() != ComponentType.REPEAT_SEQUENCE_INNER) {
			owner.getGroupedComponents().add(toAdd);
			seqGroupedComponents.add(toAdd);
			GWT.log("ToAdd= getType " + toAdd.getType());
			myPanel.add(toAdd.getContent());
		}
	}

	public ArrayList<TemplateComponent> getSeqGroupedComponents() {
		return seqGroupedComponents;
	}

	public void removeAddAnotherButton() {
		buttonsWrapperPanel.remove(addAnotherB);
	}

	private void addAnother() {
		owner.add(repSequence);
	}

	private void removeMe() {
		owner.remove(this);
	}
	
	public RepeatableSequence getSequence() {
		return repSequence;
	}
}
