/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * 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:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.selenium.languageserver;

import static org.eclipse.che.selenium.core.constant.TestMenuCommandsConstants.Assistant.ASSISTANT;
import static org.eclipse.che.selenium.core.constant.TestMenuCommandsConstants.Assistant.FIND_DEFINITION;
import static org.eclipse.che.selenium.pageobject.CodenvyEditor.MarkerLocator.ERROR_OVERVIEW;

import com.google.inject.Inject;
import java.net.URL;
import java.nio.file.Paths;
import org.eclipse.che.selenium.core.client.TestProjectServiceClient;
import org.eclipse.che.selenium.core.project.ProjectTemplates;
import org.eclipse.che.selenium.core.workspace.InjectTestWorkspace;
import org.eclipse.che.selenium.core.workspace.TestWorkspace;
import org.eclipse.che.selenium.core.workspace.WorkspaceTemplate;
import org.eclipse.che.selenium.pageobject.CodenvyEditor;
import org.eclipse.che.selenium.pageobject.Consoles;
import org.eclipse.che.selenium.pageobject.Ide;
import org.eclipse.che.selenium.pageobject.Loader;
import org.eclipse.che.selenium.pageobject.Menu;
import org.eclipse.che.selenium.pageobject.NotificationsPopupPanel;
import org.eclipse.che.selenium.pageobject.ProjectExplorer;
import org.eclipse.che.selenium.pageobject.debug.DebugPanel;
import org.eclipse.che.selenium.pageobject.debug.PhpDebugConfig;
import org.openqa.selenium.Keys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/** @author Musienko Maxim */
public class PhpFileEditingTest {
  private static final Logger LOG = LoggerFactory.getLogger(PhpFileEditingTest.class);
  private static final String PROJECT = "php-tests";
  private static final String PATH_TO_INDEX_PHP = PROJECT + "/index.php";
  private static final URL RESOURCE =
      PhpFileEditingTest.class.getResource("/projects/plugins/DebuggerPlugin/php-tests");

  @InjectTestWorkspace(template = WorkspaceTemplate.ECLIPSE_PHP)
  private TestWorkspace ws;

  @Inject private Ide ide;

  @Inject private ProjectExplorer projectExplorer;
  @Inject private Loader loader;
  @Inject private DebugPanel debugPanel;
  @Inject private PhpDebugConfig debugConfig;
  @Inject private NotificationsPopupPanel notificationPopup;
  @Inject private Menu menu;
  @Inject private CodenvyEditor editor;
  @Inject private Consoles consoles;
  @Inject private TestProjectServiceClient testProjectServiceClient;

  @BeforeClass
  public void setup() throws Exception {
    testProjectServiceClient.importProject(
        ws.getId(), Paths.get(RESOURCE.toURI()), PROJECT, ProjectTemplates.PHP);

    // open IDE
    ide.open(ws);
    loader.waitOnClosed();
    projectExplorer.waitItem(PROJECT);
    notificationPopup.waitProgressPopupPanelClose();

    // open project tree
    projectExplorer.quickExpandWithJavaScript();
    projectExplorer.openItemByPath(PATH_TO_INDEX_PHP);
    editor.waitActive();
  }

  @Test
  public void checkMainFeaturesPhpLS() {
    String intitPhpLanguageServerMessage =
        String.format(
            "Finished language servers initialization, file path '/%s'", PATH_TO_INDEX_PHP);

    consoles.waitExpectedTextIntoConsole(intitPhpLanguageServerMessage);

    checkCodeValidation();
    checkAutocompletion();
    checkCodeAssistant();
    checkGoToDefinition();
  }

  private void checkGoToDefinition() {
    editor.deleteCurrentLine();

    editor.goToPosition(14, 9);

    menu.runCommand(ASSISTANT, FIND_DEFINITION);
    editor.waitActiveTabFileName("lib.php");
    editor.waitCursorPosition(14, 2);
  }

  private void checkCodeAssistant() {
    editor.goToCursorPositionVisible(15, 7);
    editor.typeTextIntoEditor(Keys.ENTER.toString());
    editor.typeTextIntoEditor("e");
    editor.launchAutocomplete();
    editor.waitTextIntoAutocompleteContainer(
        "expm1 float\nexp float\nerror_log bool\nexplode array\nexec string");
  }

  private void checkAutocompletion() {
    editor.goToCursorPositionVisible(13, 1);
    editor.typeTextIntoEditor("$color = \"blue\";");

    editor.goToCursorPositionVisible(14, 22);
    editor.typeTextIntoEditor(Keys.ENTER.toString());
    editor.waitCursorPosition(15, 1);

    editor.typeTextIntoEditor("$");
    editor.launchAutocomplete();
    editor.waitTextIntoEditor("$color = \"blue\";\necho sayHello(\"man\");\n$color\n?>", 3);
    editor.closeAutocomplete();
  }

  private void checkCodeValidation() {
    editor.goToPosition(14, 2);
    editor.typeTextIntoEditor(" ");
    editor.clickOnMarker(ERROR_OVERVIEW, 14);
    editor.waitTextInToolTipPopup("';' expected.");

    editor.goToPosition(14, 3);
    editor.typeTextIntoEditor(Keys.BACK_SPACE.toString());
    editor.waitAllMarkersInvisibility(ERROR_OVERVIEW);
  }
}

@@ -710,16 +710,64 @@
 RVIEW;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -840,16 +840,39 @@
 .Paths;%0A
+import java.util.List;%0A
 import o
@@ -1594,211 +1594,28 @@
 org.
-eclipse.che.selenium.pageobject.debug.DebugPanel;%0Aimport org.eclipse.che.selenium.pageobject.debug.PhpDebugConfig;%0Aimport org.openqa.selenium.Keys;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+openqa.selenium.Keys
 ;%0Aim
@@ -1760,95 +1760,8 @@
 t %7B%0A
-  private static final Logger LOG = LoggerFactory.getLogger(PhpFileEditingTest.class);%0A
   pr
@@ -2012,16 +2012,173 @@
 sts%22);%0A%0A
+  private List%3CString%3E expectedProposals =%0A      ImmutableList.of(%0A          %22expm1 float%22, %22exp float%22, %22error_log bool%22, %22explode array%22, %22exec string%22);%0A%0A
   @Injec
@@ -2379,95 +2379,8 @@
 er;%0A
-  @Inject private DebugPanel debugPanel;%0A  @Inject private PhpDebugConfig debugConfig;%0A
   @I
@@ -3950,39 +3950,52 @@
 lete();%0A
+%0A
     
-editor.waitTextInto
+// check expected proposals in 
 Autocomp
@@ -4002,17 +4002,18 @@
 lete
-C
+ c
 ontainer
 (%0A  
@@ -4012,86 +4012,133 @@
 iner
-(%0A        %22expm1 float%5Cnexp float%5Cnerror_log bool%5Cnexplode array%5Cnexec string%22
+%0A    expectedProposals.forEach(%0A        proposal -%3E %7B%0A          editor.waitTextIntoAutocompleteContainer(proposal);%0A        %7D
 );%0A 

fix testing code assistant feature in PhpFileEditingTest selenium test (#10344)