/*
 * Copyright (c) 2017, 2026, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8177076 8185426 8189595 8188072 8221759 8255273
 * @modules
 *     jdk.compiler/com.sun.tools.javac.api
 *     jdk.compiler/com.sun.tools.javac.main
 *     jdk.jshell/jdk.internal.jshell.tool:+open
 *     jdk.jshell/jdk.internal.jshell.tool.resources:open
 *     jdk.jshell/jdk.jshell:open
 * @library /tools/lib
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
 * @build Compiler UITesting
 * @build ToolTabSnippetTest
 * @run junit/timeout=300 ToolTabSnippetTest
 */

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import jdk.internal.jshell.tool.ConsoleIOContextTestSupport;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class ToolTabSnippetTest extends UITesting {

    public ToolTabSnippetTest() {
        super(true);
    }

    @ParameterizedTest
    @ValueSource(booleans={false, true})
    public void testExpression(boolean createCombinedJar) throws Exception {
        Path classes = prepareZip(createCombinedJar);
        doRunTest((inputSink, out) -> {
            inputSink.write("/env -class-path " + classes.toString() + "\n");
            waitOutput(out, resource("jshell.msg.set.restore") + "\n\\u001B\\[\\?2004h" + PROMPT);
            inputSink.write("import jshelltest.*;\n");
            waitOutput(out, "\n\\u001B\\[\\?2004l\\u001B\\[\\?2004h" + PROMPT);

            //-> <tab>
            inputSink.write(TAB);
            waitOutput(out, getMessage("jshell.console.completion.all.completions.number", "[0-9]+"));
            inputSink.write(TAB);
            waitOutput(out, ".*String.*StringBuilder.*" +
                            REDRAW_PROMPT + "");

            //new JShellTes<tab>
            inputSink.write("new JShellTes" + TAB);
            waitOutput(out, "\nJShellTest\\(      JShellTestAux\\(   " +
                            REDRAW_PROMPT + "new JShellTest");

            //new JShellTest<tab>
            inputSink.write(TAB);
            waitOutput(out, "JShellTest\\(      JShellTestAux\\(   \n" +
                            "\n" +
                            resource("jshell.console.completion.current.signatures") + "\n" +
                            "jshelltest.JShellTest\n" +
                            "\n" +
                            resource("jshell.console.see.documentation") +
                            REDRAW_PROMPT + "new JShellTest");
            inputSink.write(TAB);
            waitOutput(out, "\\u001B\\[1mjshelltest.JShellTest\\u001B\\[0m\n" +
                            "JShellTest 0" +
                            REDRAW_PROMPT + "new JShellTest");
            inputSink.write(TAB);
            waitOutput(out, "JShellTest\\(      JShellTestAux\\(   \n" +
                            "\n" +
                            resource("jshell.console.completion.current.signatures") + "\n" +
                            "jshelltest.JShellTest\n" +
                            "\n" +
                            resource("jshell.console.see.documentation") +
                            REDRAW_PROMPT + "new JShellTest");

            //new JShellTest(<tab>
            inputSink.write("(" + TAB);
            waitOutput(out, "\\(\n" +
                            resource("jshell.console.completion.current.signatures") + "\n" +
                            "JShellTest\\(\\u001B\\[1mString str\\u001B\\[0m\\)\n" +
                            "JShellTest\\(\\u001B\\[1mString str\\u001B\\[0m, int i\\)\n" +
                            "\n" +
                            resource("jshell.console.see.documentation") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "\\u001B\\[1mJShellTest\\(String str\\)\\u001B\\[0m\n" +
                            "JShellTest 1\n" +
                            "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" +
                            "\n" +
                            resource("jshell.console.see.next.page") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" +
                            "\n" +
                            resource("jshell.console.see.next.javadoc") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "\\u001B\\[1mJShellTest\\(String str, int i\\)\\u001B\\[0m\n" +
                            "JShellTest 2\n" +
                            "\n" +
                            getMessage("jshell.console.completion.all.completions.number", "[0-9]+") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, ".*String.*StringBuilder.*" +
                            REDRAW_PROMPT + "new JShellTest\\(");

            inputSink.write(INTERRUPT + "String str = \"\";\nnew JShellTest(");
            waitOutput(out, PROMPT + "new JShellTest\\(");

            inputSink.write(TAB);
            waitOutput(out, "\n" +
                            "str   \n" +
                            "\n" +
                            resource("jshell.console.completion.current.signatures") + "\n" +
                            "JShellTest\\(\\u001B\\[1mString str\\u001B\\[0m\\)\n" +
                            "JShellTest\\(\\u001B\\[1mString str\\u001B\\[0m, int i\\)\n" +
                            "\n" +
                            resource("jshell.console.see.documentation") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "\\u001B\\[1mJShellTest\\(String str\\)\\u001B\\[0m\n" +
                            "JShellTest 1\n" +
                            "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" +
                            "\n" +
                            resource("jshell.console.see.next.page") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" +
                            "\n" +
                            resource("jshell.console.see.next.javadoc") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, "\\u001B\\[1mJShellTest\\(String str, int i\\)\\u001B\\[0m\n" +
                            "JShellTest 2\n" +
                            "\n" +
                            getMessage("jshell.console.completion.all.completions.number", "[0-9]+") +
                            REDRAW_PROMPT + "new JShellTest\\(");
            inputSink.write(TAB);
            waitOutput(out, ".*String.*StringBuilder.*" +
                            REDRAW_PROMPT + "new JShellTest\\(");

            inputSink.write(INTERRUPT + "JShellTest t = new JShellTest" + TAB);
            waitOutput(out, PROMPT + "JShellTest t = new JShellTest\n" +
                            "JShellTest\\(   \n" +
                            "\n" +
                            resource("jshell.console.completion.current.signatures") + "\n" +
                            "jshelltest.JShellTest\n" +
                            "\n" +
                            resource("jshell.console.completion.all.completions") +
                            REDRAW_PROMPT + "JShellTest t = new JShellTest");
            inputSink.write(TAB);
            waitOutput(out, "JShellTest\\(      JShellTestAux\\(   \n" +
                            "\n" +
                            resource("jshell.console.see.documentation") +
                            REDRAW_PROMPT + "JShellTest t = new JShellTest");

            inputSink.write(INTERRUPT + "JShellTest t = new " + TAB);
            waitOutput(out, PROMPT + "JShellTest t = new \n" +
                            "JShellTest\\(   \n" +
                            "\n" +
                            getMessage("jshell.console.completion.all.completions.number", "[0-9]+") +
                            REDRAW_PROMPT + "JShellTest t = new ");
            inputSink.write(TAB);
            waitOutput(out, ".*String.*StringBuilder.*" +
                            REDRAW_PROMPT + "JShellTest t = new ");

            inputSink.write(INTERRUPT + "class JShelX{}\n");
            inputSink.write("new JShel" + TAB);
            waitOutput(out, PROMPT + "new JShel\n" +
                            "JShelX\\(\\)         JShellTest\\(      JShellTestAux\\(   " +
                            REDRAW_PROMPT + "new JShel");

            //no crash:
            inputSink.write(INTERRUPT + "new Stringbuil" + TAB);
            waitOutput(out, PROMPT + "new Stringbuil" + BELL);

            //no crash: 8188072
            inputSink.write(INTERRUPT + "for (int:" + TAB);
            waitOutput(out, PROMPT + "for \\(int:\n" +
                            getMessage("jshell.console.completion.all.completions.number", "[0-9]+") +
                            REDRAW_PROMPT + "for \\(int:");
        });
    }

    @Test
    public void testCleaningCompletionTODO() throws Exception {
        doRunTest((inputSink, out) -> {
            CountDownLatch testCompleteComputationStarted = new CountDownLatch(1);
            CountDownLatch testCompleteComputationContinue = new CountDownLatch(1);
            ConsoleIOContextTestSupport.IMPL = new ConsoleIOContextTestSupport() {
                @Override
                protected void willComputeCompletionCallback() {
                    if (testCompleteComputationStarted != null) {
                        testCompleteComputationStarted.countDown();
                    }
                    if (testCompleteComputationContinue != null) {
                        try {
                            testCompleteComputationContinue.await();
                        } catch (InterruptedException ex) {
                            throw new IllegalStateException(ex);
                        }
                    }
                }
            };
            //-> <tab>
            inputSink.write(TAB);
            testCompleteComputationStarted.await();
            //-> <tab><tab>
            inputSink.write(TAB + TAB);
            testCompleteComputationContinue.countDown();
            waitOutput(out, PROMPT);
            //-> <tab>
            inputSink.write(TAB);
            waitOutput(out, PROMPT);
            ConsoleIOContextTestSupport.IMPL = null;
        });
    }

    @Test
    public void testNoRepeat() throws Exception {
        doRunTest((inputSink, out) -> {
            inputSink.write("String xyzAA;\n");
            waitOutput(out, PROMPT);

            //xyz<tab>
            inputSink.write("String s = xyz" + TAB);
            waitOutput(out, "^String s = xyzAA");
            inputSink.write(".");
            waitOutput(out, "^\\.");

            inputSink.write(INTERRUPT);
            waitOutput(out, PROMPT);

            inputSink.write("double xyzAB;\n");
            waitOutput(out, PROMPT);

            //xyz<tab>
            inputSink.write("String s = xyz" + TAB);
            String allCompletions =
                    resource("jshell.console.completion.all.completions");
            waitOutput(out, ".*xyzAA.*" + allCompletions + ".*\u0005String s = xyzA");
        });
    }

    @Test
    public void testCrash8221759() throws Exception {
        doRunTest((inputSink, out) -> {
            inputSink.write("java.io.File.path" + TAB);
            waitOutput(out, "java.io.File.path\n" +
                            "pathSeparator       pathSeparatorChar   " +
                            REDRAW_PROMPT + "java.io.File.pathSeparator");
        });
    }

    private Path prepareZip(boolean createCombinedJar) {
        String clazz1 =
                "package jshelltest;\n" +
                "/**JShellTest 0" +
                " */\n" +
                "public class JShellTest {\n" +
                "    /**JShellTest 1\n" +
                "     * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" +
                "     * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" +
                "     * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" +
                "     */\n" +
                "    public JShellTest(String str) {}\n" +
                "    /**JShellTest 2" +
                "     */\n" +
                "    public JShellTest(String str, int i) {}\n" +
                "}\n";

        String clazz2 =
                "package jshelltest;\n" +
                "/**JShellTestAux 0" +
                " */\n" +
                "public class JShellTestAux {\n" +
                "    /**JShellTest 1" +
                "     */\n" +
                "    public JShellTestAux(String str) { }\n" +
                "    /**JShellTest 2" +
                "     */\n" +
                "    public JShellTestAux(String str, int i) { }\n" +
                "}\n";

        if (createCombinedJar) {
            Path combinedJar = Paths.get("combined.jar");

            try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(combinedJar))) {
                out.putNextEntry(new JarEntry("jshelltest/JShellTest.java"));
                out.write(clazz1.getBytes());
                out.putNextEntry(new JarEntry("jshelltest/JShellTestAux.java"));
                out.write(clazz2.getBytes());

                compiler.compile(clazz1, clazz2);

                for (String clazz : new String[] {"jshelltest/JShellTest.class", "jshelltest/JShellTestAux.class"}) {
                    out.putNextEntry(new JarEntry(clazz));

                    try (InputStream in = Files.newInputStream(compiler.getClassDir().resolve(clazz))) {
                        in.transferTo(out);
                    }
                }
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }

            //create a broken combined-sources.jar, which should not be used:
            Path srcZip = Paths.get("combined-sources.jar");

            try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
                out.putNextEntry(new JarEntry("jshelltest/JShellTest.java"));
                out.write("""
                          package jshelltest;
                          /** wrong */
                          public class JShellTest {
                              /** wrong
                               */
                              public JShellTest(String str) {}
                              /**JShellTest 2     */
                              public JShellTest(String str, int i) {}
                          }
                          """.getBytes());

                out.putNextEntry(new JarEntry("jshelltest/JShellTestAux.java"));
                out.write("""
                          package jshelltest;
                          /** wrong */
                          public class JShellTestAux {
                              /** wrong */
                              public JShellTestAux(String str) { }
                              /** wrong */
                              public JShellTestAux(String str, int i) { }
                          }
                          """.getBytes());
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }

            return combinedJar;
        } else {
            Path srcZip = Paths.get("test-sources.jar");

            try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
                out.putNextEntry(new JarEntry("jshelltest/JShellTest.java"));
                out.write(clazz1.getBytes());
                out.putNextEntry(new JarEntry("jshelltest/JShellTestAux.java"));
                out.write(clazz2.getBytes());
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }

            compiler.compile(clazz1, clazz2);

            Path binaryJar = Paths.get("test.jar");
            compiler.jar(compiler.getClassDir(), binaryJar, "jshelltest/JShellTest.class", "jshelltest/JShellTestAux.class");

            return binaryJar;
        }
    }
    //where:
        private final Compiler compiler = new Compiler();

    @Test
    public void testDocumentationAfterInsert() throws Exception {
        doRunTest((inputSink, out) -> {
            inputSink.write("import java.time.*\n");
            waitOutput(out, PROMPT);

            inputSink.write("new Instant" + TAB);
            waitOutput(out, PROMPT + "new InstantiationE");
        });
    }

    @Test
    public void testAnnotation() throws Exception {
        doRunTest((inputSink, out) -> {
            inputSink.write("@interface Ann1 { public java.lang.annotation.Retention[] value(); }\n");
            waitOutput(out, "\n\\u001B\\[\\?2004h" + PROMPT);

            //-> <tab>
            inputSink.write("@Ann1(" + TAB);
            waitOutput(out, ".*@java.lang.annotation.Retention\\(.*value =.*" +
                            REDRAW_PROMPT + "@Ann1\\(");
            inputSink.write("@" + TAB);
            waitOutput(out, "^@java.lang.annotation.Retention\\(");
            inputSink.write(TAB);
            waitOutput(out, ".*java.lang.annotation.RetentionPolicy.*java.lang.annotation.RetentionPolicy.CLASS.*" +
                            REDRAW_PROMPT + "@Ann1\\(@java.lang.annotation.Retention\\(");
            inputSink.write("CL" + TAB);
            waitOutput(out, "CL\\u001B\\[2Djava.lang.annotation.RetentionPolicy.CLASS \\u0008");
        });
    }

    @BeforeAll
    public static void noJDKSources() {
        setJDKSourcesOverride(List.of());
    }

    @AfterAll
    public static void restoreJDKSources() {
        setJDKSourcesOverride(null);
    }

    private static void setJDKSourcesOverride(List<Path> paths) throws IllegalStateException {
        try {
            //to ensure test stability, don't use JDK's src.zip:
            Field availableSources = Class.forName("jdk.jshell.SourceCodeAnalysisImpl").getDeclaredField("jdkSourcesOverride");
            availableSources.setAccessible(true);
            availableSources.set(null, paths);
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | ClassNotFoundException ex) {
            throw new IllegalStateException(ex);
        }
    }
}
