/*
 * Copyright 2013-2016 consulo.io
 *
 * 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 consulo.web.gwt.server;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Iconable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.DeferredIcon;
import com.intellij.ui.LayeredIcon;
import com.intellij.ui.RowIcon;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import consulo.annotations.DeprecationInfo;
import consulo.fileTypes.impl.VfsIconUtil;
import consulo.web.gwt.shared.transport.GwtVirtualFile;

import javax.swing.*;
import java.util.List;

/**
 * @author VISTALL
 * @since 18-May-16
 */
@Deprecated
@DeprecationInfo("This is part of research 'consulo as web app'. Code was written in hacky style. Must be dropped, or replaced by Consulo UI API")
public class GwtVirtualFileUtil {
  public static GwtVirtualFile createVirtualFile(final Project project, final VirtualFile virtualFile) {
    final GwtVirtualFile gwtVirtualFile = new GwtVirtualFile();
    gwtVirtualFile.url = virtualFile.getUrl();
    gwtVirtualFile.name = virtualFile.getName();
    gwtVirtualFile.isDirectory = virtualFile.isDirectory();

    ApplicationManager.getApplication().runReadAction(new Runnable() {
      @Override
      public void run() {
        Icon icon = VfsIconUtil.getIcon(virtualFile, Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY, project);
        if (icon instanceof DeferredIcon) {
          icon = ((DeferredIcon)icon).evaluate();
        }

        if (setIconLayers(icon, gwtVirtualFile.iconLayers)) {
          return;
        }

        if (icon instanceof RowIcon) {
          int iconCount = ((RowIcon)icon).getIconCount();
          for (int i = 0; i < iconCount; i++) {
            Icon nextIcon = ((RowIcon)icon).getIcon(i);

            if (i == 0) {
              setIconLayers(nextIcon, gwtVirtualFile.iconLayers);
            }
            else if (i == 1) {
              List<String> list = new SmartList<String>();
              setIconLayers(nextIcon, list);
              if (!list.isEmpty()) {
                gwtVirtualFile.rightIcon = list.get(0);
              }
            }
          }
        }
      }
    });

    if(gwtVirtualFile.iconLayers.isEmpty()) {
      gwtVirtualFile.iconLayers.add(iconUrl(AllIcons.FileTypes.Unknown));
    }
    return gwtVirtualFile;
  }

  private static boolean setIconLayers(Icon icon, List<String> list) {
    if (icon instanceof RowIcon) {
      return false;
    }
    if (icon instanceof LayeredIcon) {
      Icon[] allLayers = ((LayeredIcon)icon).getAllLayers();
      for (Icon layerIcon : allLayers) {
        ContainerUtil.addIfNotNull(list, iconUrl(layerIcon));
      }
    }
    else {
      ContainerUtil.addIfNotNull(list, iconUrl(icon));
    }
    return true;
  }

  private static String iconUrl(Icon icon) {
    String maybeUrl = icon.toString();
    int i = maybeUrl.indexOf("!/");
    if (i != -1) {
       return maybeUrl.substring(i + 1, maybeUrl.length());
    }
    return null;
  }
}

@@ -645,407 +645,76 @@
 lij.
-icons.AllIcons;%0Aimport com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.util.Iconable;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.ui.DeferredIcon;%0Aimport com.intellij.ui.LayeredIcon;%0Aimport com.intellij.ui.RowIcon;%0Aimport com.intellij.util.SmartList;%0Aimport com.intellij.util.containers.ContainerUtil
+openapi.project.Project;%0Aimport com.intellij.openapi.vfs.VirtualFile
 ;%0Aim
@@ -759,51 +759,8 @@
 fo;%0A
-import consulo.fileTypes.impl.VfsIconUtil;%0A
 impo
@@ -816,54 +816,8 @@
 e;%0A%0A
-import javax.swing.*;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -1382,1870 +1382,34 @@
 );%0A%0A
-    ApplicationManager.getApplication().runReadAction(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A        Icon icon = VfsIconUtil.getIcon(virtualFile, Iconable.ICON_FLAG_READ_STATUS %7C Iconable.ICON_FLAG_VISIBILITY, project);%0A        if (icon instanceof DeferredIcon) %7B%0A          icon = ((DeferredIcon)icon).evaluate();%0A        %7D%0A%0A        if (setIconLayers(icon, gwtVirtualFile.iconLayers)) %7B%0A          return;%0A        %7D%0A%0A        if (icon instanceof RowIcon) %7B%0A          int iconCount = ((RowIcon)icon).getIconCount();%0A          for (int i = 0; i %3C iconCount; i++) %7B%0A            Icon nextIcon = ((RowIcon)icon).getIcon(i);%0A%0A            if (i == 0) %7B%0A              setIconLayers(nextIcon, gwtVirtualFile.iconLayers);%0A            %7D%0A            else if (i == 1) %7B%0A              List%3CString%3E list = new SmartList%3CString%3E();%0A              setIconLayers(nextIcon, list);%0A              if (!list.isEmpty()) %7B%0A                gwtVirtualFile.rightIcon = list.get(0);%0A              %7D%0A            %7D%0A          %7D%0A        %7D%0A      %7D%0A    %7D);%0A%0A    if(gwtVirtualFile.iconLayers.isEmpty()) %7B%0A      gwtVirtualFile.iconLayers.add(iconUrl(AllIcons.FileTypes.Unknown));%0A    %7D%0A    return gwtVirtualFile;%0A  %7D%0A%0A  private static boolean setIconLayers(Icon icon, List%3CString%3E list) %7B%0A    if (icon instanceof RowIcon) %7B%0A      return false;%0A    %7D%0A    if (icon instanceof LayeredIcon) %7B%0A      Icon%5B%5D allLayers = ((LayeredIcon)icon).getAllLayers();%0A      for (Icon layerIcon : allLayers) %7B%0A        ContainerUtil.addIfNotNull(list, iconUrl(layerIcon));%0A      %7D%0A    %7D%0A    else %7B%0A      ContainerUtil.addIfNotNull(list, iconUrl(icon));%0A    %7D%0A    return true;%0A  %7D%0A%0A  private static String iconUrl(Icon icon) %7B%0A    String maybeUrl = icon.toString();%0A    int i = maybeUrl.indexOf(%22!/%22);%0A    if (i != -1) %7B%0A       return maybeUrl.substring(i + 1, maybeUrl.length());%0A    %7D%0A    return null
+%0A    return gwtVirtualFile
 ;%0A  

drop dead method