/*******************************************************************************
 * Copyright (c) 2009, 2010 Innovation Gate GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Innovation Gate GmbH - initial API and implementation
 ******************************************************************************/
package de.innovationgate.eclipse.editors.design.wizards;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.utils.ui.FolderSelectionPage;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class NewTMLModulePage extends ValidatedWizardPage {
  private FolderSelectionPage _selectionPage;
  private Text _moduleName;
  private Text _txtTargetLocation;
  private IContainer _selectedContainer;
  private Button _chkDirectAccess;
  private Button _chkCachable;
  private IFile _tmlFile;
  public NewTMLModulePage(String title, String description) {
    super(title);
    setTitle(title);
    setPageComplete(false);
    setDescription(description);
  }
  public NewTMLModulePage(String title, String description, IStructuredSelection selection) {
    super(title);
    setTitle(title);
    setPageComplete(false);
    setDescription(description);
    if (selection != null && selection.getFirstElement() instanceof IResource) {
      IResource selectedResource = (IResource) selection.getFirstElement();
      if (selectedResource instanceof IContainer) {
        _selectedContainer = (IContainer) selection.getFirstElement();
      }
      if(selectedResource instanceof IFile){
        _selectedContainer = ((IFile) selection.getFirstElement()).getParent();
      }
    }
  }
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    container.setLayout(layout);
    new Label(container, SWT.NONE).setText("Module Name: ");
    GridData innerGrpData = new GridData(GridData.FILL_HORIZONTAL);
    _moduleName = new Text(container, SWT.BORDER);
    _moduleName.setLayoutData(innerGrpData);
    _moduleName.addModifyListener(this);
    new Label(container, SWT.None).setText("Target location: ");
    _txtTargetLocation = new Text(container, SWT.READ_ONLY);
    Display display = getWizard().getContainer().getShell().getDisplay();
    _txtTargetLocation.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    _txtTargetLocation.setLayoutData(innerGrpData);
    if(_selectedContainer!=null){
      _txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
    }
    
    
    new Label(container, SWT.None).setText("DirectAccess:");
    _chkDirectAccess = new Button(container, SWT.CHECK);
    _chkDirectAccess.setText("enabled");
    _chkDirectAccess.setData("directAccess");
    new Label(container, SWT.None).setText("Caching:");
    _chkCachable = new Button(container, SWT.CHECK);
    _chkCachable.setText("enabled");
    _chkCachable.setData("browser cachable");
    setControl(container);
  }
  @Override
  public void computeResponse() {
    // TODO Auto-generated method stub
  }
  @Override
    public List<IStatus> validate() {
        List<IStatus> list = new ArrayList<IStatus>();
        String nameOfmodelToCreate = _moduleName.getText();
        
        
        if (nameOfmodelToCreate.equals("")) {
            list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Please specify a name for the module."));
        } else if (!WGADesignStructureHelper.isValidModuleName(nameOfmodelToCreate)) {
            list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "Invalid chars in module name."));
        } else {
            IFolder targetContainer = _selectedContainer.getFolder(new Path(nameOfmodelToCreate));
            
            if(nameOfmodelToCreate.contains(".")){
                String suffix = nameOfmodelToCreate.substring(nameOfmodelToCreate.lastIndexOf("."),nameOfmodelToCreate.length());
                if(suffix.equals(".tml")){
                    nameOfmodelToCreate = nameOfmodelToCreate.substring(0, nameOfmodelToCreate.lastIndexOf("."));
                }               
            }
            
            IFile targetFile = _selectedContainer.getFile(new Path(nameOfmodelToCreate + ".tml"));
            if (targetContainer.exists()) {
                list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a folder named \"" + nameOfmodelToCreate + "\" below \"" + _selectedContainer.getName() + "\"."));
            } else if (targetFile.exists()) {
                list.add(new Status(Status.ERROR, Plugin.PLUGIN_ID, "There is already a file named \"" + nameOfmodelToCreate + "\" below \"" + _selectedContainer.getName() + "\"."));
            } else {
                _tmlFile = targetFile;
            }
        }
        return list;
    }
  public IFile getTmlFile() {
    return _tmlFile;
  }
  public boolean getIsTmlFileDirectAccess() {
    return _chkDirectAccess.getSelection();
  }
  public boolean getIsTmlFileCachable() {
    return _chkCachable.getSelection();
  }
  @Override
  public void show() {
    IWizardPage prevPage = getWizard().getPreviousPage(this);
    if (prevPage != null) {
        if (prevPage instanceof FolderSelectionPage) {
          _selectionPage = (FolderSelectionPage) prevPage;
        }
        if (_selectionPage.getSelectedResource() instanceof IContainer) {
          _selectedContainer = (IContainer) _selectionPage.getSelectedResource();
        }
    
        _txtTargetLocation.setText(_selectedContainer.getFullPath().toString());
    }
    performValidation();
  }
}