package ru.rsdn.defensive.sample;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.oval.guard.ConstraintsViolatedException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;

import ru.rsdn.patterns.mvc.model.Model;
import ru.rsdn.patterns.mvc.view.View;

/**
 * Представление компании
 */
public class CompanyView extends View<Company> {
	private final Text nameText;

	private final Text tinText;

	private final Text emailText;

	private List protocolList;

	/**
	 * Конструктор
	 * @param parent композит
	 */
	public CompanyView(Composite parent) {
		{
			final Composite composite = new Composite(parent, SWT.NONE);
			composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			composite.setLayout(new GridLayout(2, false));
			{
				final Label label = new Label(composite, SWT.NONE);
				label.setText("Название:");
				nameText = new Text(composite, SWT.BORDER | SWT.SINGLE);
				nameText
						.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			}
			{
				final Label label = new Label(composite, SWT.NONE);
				label.setText("ИНН:");
				tinText = new Text(composite, SWT.BORDER | SWT.SINGLE);
				tinText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			}
			{
				final Label label = new Label(composite, SWT.NONE);
				label.setText("Электронная почта:");
				emailText = new Text(composite, SWT.BORDER | SWT.SINGLE);
				emailText
						.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			}
		}
		{
			final Composite composite = new Composite(parent, SWT.NONE);
			composite.setLayout(new GridLayout(3, true));
			final Button editButton = new Button(composite, SWT.PUSH);
			editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			editButton.setText("Изменить");
			editButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					try {
						final Company _company = new Company(nameText
								.getText(), tinText.getText(), emailText
								.getText());
						edit(_company);
					} catch (final ConstraintsViolatedException e1) {
						update();
						protocolList.add(new SimpleDateFormat("HH:mm")
								.format(new Date()) +
								" Ошибка ввода: " + e1.getMessage());
					}
				}
			});
			final Button undoButton = new Button(composite, SWT.PUSH);
			undoButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL |
					GridData.GRAB_VERTICAL));
			undoButton.setText("Отмена");
			undoButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					undo();
				}
			});
			final Button redoButton = new Button(composite, SWT.PUSH);
			redoButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			redoButton.setText("Повтор");
			redoButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					redo();
				}
			});
		}
		{
			protocolList = new List(parent, SWT.BORDER | SWT.H_SCROLL |
					SWT.V_SCROLL | SWT.MULTI);
			protocolList.setLayoutData(new GridData(GridData.FILL_BOTH));
		}
	}

	public void modelChanged(Model<Company> model) {
		update();
	}

	private void update() {
		final Company company = getModel().getProperty();
		nameText.setText(company.getName());
		tinText.setText(company.getTin());
		emailText.setText(company.getEmail());
	}
}
