/*******************************************************************************
 * Copyright (c) 2010, 2019 Red Hat Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.linuxtools.changelog.ui.tests.swtbot;

import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.ByteArrayInputStream;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ChangeLogTestProject;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorer;
import org.eclipse.linuxtools.changelog.ui.tests.utils.ProjectExplorerTreeItemAppearsCondition;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.IEditorReference;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * UI tests for formatting ChangeLog files.
 */
public class FormatChangeLogSWTBotTest extends AbstractSWTBotTest {

    private ChangeLogTestProject project;
    // The name of the test project, we create
    private final String PROJECT_NAME = "org.eclipse.linuxtools.changelog.ui.formattestproject";

    @Before
    public void setUp() throws Exception {
        project = new ChangeLogTestProject(PROJECT_NAME);
		projectExplorerViewTree = ProjectExplorer.getTree();
    }

    @After
    public void tearDown() throws Exception {
        this.project.getTestProject().delete(true, null);
    }

    /**
     * Simple test for ChangeLog formatting.
     *
     * @throws Exception
     */
    @Test
    public void canFormatChangeLogFile() throws Exception {
        // add a ChangeLog file
        assertNull(project.getTestProject().findMember(new Path("/ChangeLog")));
        final String changelogContent = "2010-12-14  Severin Gehwolf  <sgehwolf@redhat.com>\n\n" +
            "\tAdded org.eclipse.linuxtools.changelog.tests.ui plug-in.\n" +
            "\t* .classpath: New file.\n" +
            "\t* .project: New file.\n" +
            "\t* .settings/org.eclipse.jdt.core.prefs: New file.\n" +
            "\t* build.properties: New file.\n" +
            "\t* src/log4j.xml: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ContextMenuHelper.java: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ProjectExplorer.java: New file.\n" +
            "\t* src/org/eclipse/linuxtools/changelog/tests/ui/utils/ProjectExplorerTreeItemAppearsCondition.java: New file.\n";
        project.addFileToProject("/", "ChangeLog", new ByteArrayInputStream(changelogContent.getBytes()));
        assertNotNull(project.getTestProject().findMember(new Path("/ChangeLog")));

        // select ChangeLog file
        String teamProviderString = "n/a";
        SWTBotTreeItem projectItem = ProjectExplorer.expandProject(projectExplorerViewTree, PROJECT_NAME, teamProviderString);
        long oldTimeout = SWTBotPreferences.TIMEOUT;
        SWTBotPreferences.TIMEOUT = 5000;
        bot.waitUntil(new ProjectExplorerTreeItemAppearsCondition(projectExplorerViewTree, PROJECT_NAME, teamProviderString, "ChangeLog"));
        SWTBotPreferences.TIMEOUT = oldTimeout;
        SWTBotTreeItem changeLogItem = ProjectExplorer.getProjectItem(projectItem, "ChangeLog");
        changeLogItem.doubleClick(); // should open ChangeLog file

        oldTimeout = SWTBotPreferences.TIMEOUT;
        SWTBotPreferences.TIMEOUT = 3 * 5000;
        // Wait for ChangeLog editor to open
        Matcher<IEditorReference> editorMatcher = allOf(
                IsInstanceOf.instanceOf(IEditorReference.class),
                withPartName("ChangeLog")
                );
        bot.waitUntil(Conditions.waitForEditor(editorMatcher));
        SWTBotEditor swtBoteditor = bot.activeEditor();
        assertEquals("ChangeLog", swtBoteditor.getTitle());

        SWTBotEclipseEditor swtBotEclipseEditor = swtBoteditor.toTextEditor();

        // Add two extra lines after the first date line
        swtBotEclipseEditor.insertText(1, 0, "\n\n");
        // Should have 3 empty lines between date-line and first file entry
        swtBotEclipseEditor.selectRange(1, 0, 3);

        // format: ESC CTRL+F
        swtBotEclipseEditor.pressShortcut(Keystrokes.ESC);
        swtBotEclipseEditor.pressShortcut(Keystrokes.CTRL, KeyStroke.getInstance("F"));
        swtBoteditor.save();
    }

}
