/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.groovy.griffon;

import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.resolve.GrImportContributorBase;
import org.jetbrains.plugins.groovy.mvc.MvcFramework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author peter
 */
public class GriffonDefaultImportContributor extends GrImportContributorBase {

  private static Couple<List<String>> getDefaultImports(@NotNull final Module module) {
    return CachedValuesManager.getManager(module.getProject()).getCachedValue(module, new CachedValueProvider<Couple<List<String>>>() {
      @Override
      public Result<Couple<List<String>>> compute() {
        PsiPackage aPackage = JavaPsiFacade.getInstance(module.getProject()).findPackage("META-INF");
        if (aPackage != null) {
          for (PsiDirectory directory : aPackage.getDirectories(GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module))) {
            PsiFile file = directory.findFile("griffon-default-imports.properties");
            if (file instanceof PropertiesFile) {
              List<String> modelImports = tokenize(((PropertiesFile)file).findPropertyByKey("models"));
              List<String> viewImports = tokenize(((PropertiesFile)file).findPropertyByKey("views"));
              return Result.create(Couple.of(modelImports, viewImports), PsiModificationTracker.MODIFICATION_COUNT);
            }
          }
        }

        return Result.create(Couple.<List<String>>of(new ArrayList<>(), new ArrayList<>()),
                             PsiModificationTracker.MODIFICATION_COUNT);
      }

      private List<String> tokenize(IProperty models) {
        List<String> modelImports = new ArrayList<>();
        if (models != null) {
          String value = models.getValue();
          if (value != null) {
            String[] split = value.split(", ");
            for (String s : split) {
              modelImports.add(StringUtil.trimEnd(s, "."));
            }
          }
        }
        return modelImports;
      }
    });
  }

  @NotNull
  @Override
  public List<String> appendImplicitlyImportedPackages(@NotNull GroovyFile file) {
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    MvcFramework framework = MvcFramework.getInstance(module);
    if (framework instanceof GriffonFramework) {
      ArrayList<String> result = new ArrayList<>();
      result.add("griffon.core");
      result.add("griffon.util");

      VirtualFile griffonApp = framework.findAppDirectory(file);
      if (griffonApp != null) {
        VirtualFile models = griffonApp.findChild("models");
        VirtualFile views = griffonApp.findChild("views");
        VirtualFile vFile = file.getOriginalFile().getVirtualFile();

        assert vFile != null;
        assert module != null;
        if (models != null && VfsUtilCore.isAncestor(models, vFile, true)) {
          result.addAll(getDefaultImports(module).first);
        }
        else if (views != null && VfsUtilCore.isAncestor(views, vFile, true)) {
          result.addAll(getDefaultImports(module).second);
        }
      }

      return result;
    }

    return Collections.emptyList();
  }
}

@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -18,9 +18,9 @@
 -201
-6
+7
  Jet
@@ -36,566 +36,109 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 %0Apac
@@ -1056,16 +1056,24 @@
 resolve.
+imports.
 GrImport
@@ -1083,20 +1083,156 @@
 tributor
-Base
+;%0Aimport org.jetbrains.plugins.groovy.lang.resolve.imports.GroovyImport;%0Aimport org.jetbrains.plugins.groovy.lang.resolve.imports.StarImport
 ;%0Aimport
@@ -1360,16 +1360,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1472,14 +1472,17 @@
 tor 
-extend
+implement
 s Gr
@@ -1502,12 +1502,8 @@
 utor
-Base
  %7B%0A%0A
@@ -3197,24 +3197,20 @@
 l%0A  
-@Override%0A  publ
+private stat
 ic L
@@ -3225,14 +3225,11 @@
 ng%3E 
-append
+get
 Impl
@@ -4296,11 +4296,217 @@
 t();%0A  %7D
+%0A%0A  @NotNull%0A  @Override%0A  public List%3CGroovyImport%3E getFileImports(@NotNull GroovyFile file) %7B%0A    return getImplicitlyImportedPackages(file).stream().map(StarImport::new).collect(Collectors.toList());%0A  %7D
 %0A%7D%0A

use new imports API in griffon