/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.test;

import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.stream.Collectors;

public final class InTextDirectivesUtils {

    public static final String DIRECTIVES_FILE_NAME = "directives.txt";

    public static final String IGNORE_BACKEND_DIRECTIVE_PREFIX = "// IGNORE_BACKEND: ";
    public static final String IGNORE_BACKEND_K1_DIRECTIVE_PREFIX = "// IGNORE_BACKEND_K1: ";
    public static final String IGNORE_BACKEND_K2_DIRECTIVE_PREFIX = "// IGNORE_BACKEND_K2: ";

    public static final String[] IGNORE_BACKEND_DIRECTIVE_PREFIXES = { IGNORE_BACKEND_DIRECTIVE_PREFIX, IGNORE_BACKEND_K1_DIRECTIVE_PREFIX };

    public static final String TARGET_BACKEND_DIRECTIVE_PREFIX = "// TARGET_BACKEND: ";
    public static final String DORT_TARGET_EXACT_BACKEND_DIRECTIVE_PREFIX = "// DONT_TARGET_EXACT_BACKEND: ";

    private InTextDirectivesUtils() {
    }

    @Nullable
    public static Integer getPrefixedInt(@NotNull String fileText, @NotNull String prefix) {
        String[] strings = findArrayWithPrefixes(fileText, prefix);
        if (strings.length > 0) {
            assert strings.length == 1;
            return Integer.parseInt(strings[0]);
        }

        return null;
    }

    @Nullable
    public static Boolean getPrefixedBoolean(@NotNull String fileText, @NotNull String prefix) {
        String[] strings = findArrayWithPrefixes(fileText, prefix);
        if (strings.length > 0) {
            assert strings.length == 1;
            return Boolean.parseBoolean(strings[0]);
        }

        return null;
    }

    @NotNull
    public static String[] findArrayWithPrefixes(@NotNull String fileText, @NotNull String... prefixes) {
        return ArrayUtil.toStringArray(findListWithPrefixes(fileText, prefixes));
    }

    @NotNull
    public static List<String> findListWithPrefixes(@NotNull String fileText, @NotNull String... prefixes) {
        List<String> result = new ArrayList<>();

        for (String line : findLinesWithPrefixesRemoved(fileText, prefixes)) {
            splitValues(result, line);
        }

        return result;
    }

    public static List<String> splitValues(@NotNull List<String> result, @NotNull String line) {
        String unquoted = StringUtil.unquoteString(line);
        if (!unquoted.equals(line)) {
            result.add(unquoted);
        }
        else{
            String[] variants = line.split(",");
            for (String variant : variants) {
                result.add(variant.trim());
            }
        }
        return result;
    }

    public static boolean isDirectiveDefined(@NotNull String fileText, @NotNull String directive) {
        return !findListWithPrefixes(fileText, directive).isEmpty();
    }

    @Nullable
    public static String findStringWithPrefixes(@NotNull String fileText, @NotNull String... prefixes) {
        List<String> strings = findListWithPrefixes(fileText, prefixes);
        if (strings.isEmpty()) {
            return null;
        }

        if (strings.size() != 1) {
            throw new IllegalStateException("There is more than one string with given prefixes " +
                                            Arrays.toString(prefixes) + ":\n" +
                                            StringUtil.join(strings, "\n") + "\n" +
                                            "Use findListWithPrefixes() instead.");
        }

        return strings.get(0);
    }

    @NotNull
    public static List<String> findLinesWithPrefixesRemoved(@NotNull String fileText, @NotNull String... prefixes) {
        return findLinesByPrefixRemoved(fileText, true, true, prefixes).values().stream().flatMap(Collection::stream).collect(Collectors.toList());
    }

    @NotNull
    public static Map<String, List<String>> findLinesByPrefixRemoved(@NotNull String fileText, @NotNull String... prefixes) {
        return findLinesByPrefixRemoved(fileText, true, true, prefixes);
    }

    @NotNull
    public static Map<String, List<String>> findLinesByPrefixRemoved(@NotNull String fileText, boolean trim, boolean strict, @NotNull String... prefixes) {
        return findLinesByPrefixRemoved(fileText, true, strict, false, prefixes);
    }

    @NotNull
    public static Map<String, List<String>> findLinesByPrefixRemoved(@NotNull String fileText, boolean trim, boolean strict, boolean separatedValues, @NotNull String... prefixes) {
        if (prefixes.length == 0) {
            throw new IllegalArgumentException("Please specify the prefixes to check");
        }
        Map<String, List<String>> result = new HashMap<>();
        List<String> cleanedPrefixes = cleanDirectivesFromComments(Arrays.asList(prefixes));

        for (String line : fileNonEmptyCommentedLines(fileText)) {
            for (String prefix : cleanedPrefixes) {
                if (line.startsWith(prefix)) {
                    String noPrefixLine = line.substring(prefix.length());

                    if (noPrefixLine.isEmpty() ||
                            Character.isWhitespace(noPrefixLine.charAt(0)) ||
                            Character.isWhitespace(prefix.charAt(prefix.length() - 1))) {

                        List<String> resultList = result.computeIfAbsent(prefix, s -> new ArrayList<>());
                        String notSplit = trim ? noPrefixLine.trim() : StringUtil.trimTrailing(StringsKt.drop(noPrefixLine, 1));
                        if (separatedValues) {
                            List<String> split = Arrays.asList(notSplit.split(", *"));
                            resultList.addAll(split);
                        } else {
                            resultList.add(notSplit);
                        }
                        break;
                    } else if (strict) {
                        throw new AssertionError(
                                "Line starts with prefix \"" + prefix + "\", but doesn't have space symbol after it: " + line);
                    }
                }
            }
        }

        return result;
    }

    public static void assertHasUnknownPrefixes(@NotNull String fileText, @NotNull Collection<String> knownPrefixes) {
        Set<String> prefixes = new HashSet<>();

        for (String line : fileNonEmptyCommentedLines(fileText)) {
            String prefix = probableDirective(line);
            if (prefix != null) {
                prefixes.add(prefix);
            }
        }

        prefixes.removeAll(cleanDirectivesFromComments(knownPrefixes));

        KtAssert.assertTrue("File contains some unexpected directives" + prefixes, prefixes.isEmpty());
    }

    private static String probableDirective(@NotNull String line) {
        String[] arr = line.split(" ", 2);
        String firstWord = arr[0];

        if (firstWord.length() > 1 && StringUtil.toUpperCase(firstWord).equals(firstWord)) {
            return firstWord;
        }

        return null;
    }

    private static List<String> cleanDirectivesFromComments(@NotNull Collection<String> prefixes) {
        List<String> resultPrefixes = Lists.newArrayList();

        for (String prefix : prefixes) {
            if (prefix.startsWith("//") || prefix.startsWith("##")) {
                resultPrefixes.add(StringUtil.trimLeading(prefix.substring(2)));
            }
            else {
                resultPrefixes.add(prefix);
            }
        }

        return resultPrefixes;
    }


    @NotNull
    private static List<String> fileNonEmptyCommentedLines(@NotNull String fileText) {
        List<String> result = new ArrayList<>();

        try {
            try (BufferedReader reader = new BufferedReader(new StringReader(fileText))) {
                String line;

                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (line.startsWith("//") || line.startsWith("##")) {
                        String uncommentedLine = line.substring(2).trim();
                        if (!uncommentedLine.isEmpty()) {
                            result.add(uncommentedLine);
                        }
                    }
                }
            }
        }
        catch (IOException e) {
            throw ExceptionUtilsKt.rethrow(e);
        }

        return result;
    }

    public static String textWithDirectives(@NotNull File file) {
        try {
            String fileText;
            if (file.isDirectory()) {
                File directivesFile = new File(file, DIRECTIVES_FILE_NAME);
                if (!directivesFile.exists()) return "";

                fileText = FileUtil.loadFile(directivesFile);
            }
            else {
                fileText = FileUtil.loadFile(file);
            }
            return fileText;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean isCompatibleTarget(@NotNull TargetBackend targetBackend, @NotNull File file) {
        return isCompatibleTarget(targetBackend, file, /*separatedDirectiveValues=*/false);
    }

    public static boolean isCompatibleTarget(@NotNull TargetBackend targetBackend, @NotNull File file, boolean separatedDirectiveValues) {
        if (targetBackend == TargetBackend.ANY) return true;
        String textWithDirectives = textWithDirectives(file);
        Map<String, List<String>> byPrefixRemoved =
                findLinesByPrefixRemoved(
                        textWithDirectives,
                        /*trim=*/true,
                        /*strcit=*/true,
                        separatedDirectiveValues,
                        DORT_TARGET_EXACT_BACKEND_DIRECTIVE_PREFIX,
                        TARGET_BACKEND_DIRECTIVE_PREFIX
                );
        return isCompatibleTarget(targetBackend, byPrefixRemoved);
    }

    public static boolean isCompatibleTarget(@NotNull TargetBackend targetBackend, Map<String, List<String>> directives) {
        if (targetBackend == TargetBackend.ANY) return true;

        List<String> doNotTarget = directives.getOrDefault("DONT_TARGET_EXACT_BACKEND: ", Collections.emptyList());
        doNotTarget = doNotTarget.stream().flatMap((s) -> Arrays.stream(s.split(" "))).collect(Collectors.toList());
        if (doNotTarget.contains(targetBackend.name()))
            return false;

        List<String> backends = directives.getOrDefault("TARGET_BACKEND: ", Collections.emptyList());
        return isCompatibleTargetExceptAny(targetBackend, backends);
    }

    private static boolean isCompatibleTargetExceptAny(@NotNull TargetBackend targetBackend, @NotNull List<String> backends) {
        if (targetBackend == TargetBackend.ANY) return false;
        return backends.isEmpty() || backends.contains(targetBackend.name()) || isCompatibleTargetExceptAny(targetBackend.getCompatibleWith(), backends);
    }

    public static boolean isIgnoredTarget(@NotNull TargetBackend targetBackend, @NotNull File file, String... ignoreBackendDirectivePrefixes) {
        return isIgnoredTarget(targetBackend, file, false, ignoreBackendDirectivePrefixes);
    }

    public static boolean isIgnoredTarget(@NotNull TargetBackend targetBackend, @NotNull File file, boolean includeAny, String... ignoreBackendDirectivePrefixes) {
        List<String> ignoredBackends = findListWithPrefixes(textWithDirectives(file), ignoreBackendDirectivePrefixes);
        return isIgnoredTarget(targetBackend, includeAny, ignoredBackends);
    }

    public static boolean isIgnoredTarget(
            @NotNull TargetBackend targetBackend,
            @NotNull Map<String, List<String>> directives,
            boolean includeAny,
            String... ignoreBackendDirectivePrefixes
    ) {
        List<String> result = new ArrayList<>();
        directives
                .entrySet()
                .stream()
                .filter(entry -> entry.getKey().startsWith("IGNORE_BACKEND"))
                .forEach(entry ->
                         {
                             for (String s : entry.getValue()) {
                                 splitValues(result, s);
                             }
                         }
                );
        return isIgnoredTarget(targetBackend, includeAny, result);
    }

    private static boolean isIgnoredTarget(@NotNull TargetBackend targetBackend, boolean includeAny, List<String> ignoredBackends) {
        if (ignoredBackends.contains(targetBackend.name())) return true;
        if (includeAny && ignoredBackends.contains("ANY")) return true;
        return false;
    }

    public static boolean isIgnoredTarget(@NotNull TargetBackend targetBackend, @NotNull File file) {
        return isIgnoredTarget(targetBackend, file, IGNORE_BACKEND_DIRECTIVE_PREFIXES);
    }

    public static boolean dontRunGeneratedCode(@NotNull TargetBackend targetBackend, @NotNull File file) {
        List<String> backends = findListWithPrefixes(textWithDirectives(file), "// DONT_RUN_GENERATED_CODE: ");
        return backends.contains(targetBackend.name());
    }

    // Whether the target test is supposed to pass successfully on targetBackend
    public static boolean isPassingTarget(@NotNull TargetBackend targetBackend, @NotNull File file) {
        return isCompatibleTarget(targetBackend, file) && !isIgnoredTarget(targetBackend, file);
    }
}
