/*
 * Copyright 2000-2013 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 com.intellij.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;

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

public class FinderRecursivePanelTest extends LightPlatformTestCase {

  public void testListModelMerge1() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 0, 0, "a");
  }

  public void testListModelMerge2() {
    assertMerge(new String[0], "a", "b", "c", "d");
  }

  public void testListModelMerge3() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 0, -1, new String[0]);
  }

  public void testListModelMerge4() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 2, 2, new String[]{"a", "b", "c", "d"});
  }

  public void _testListModelMerge5() {
    assertMerge(new String[]{"a", "b", "c", "d"}, new String[]{"d", "c", "b", "a"});
  }

  public void testListModelMerge6() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 2, 1, "b", "c");
  }

  public void testListModelMerge7() {
    assertMerge(new String[]{"a", "b", "c", "d"}, 1, 0, "b", "c", "d", "e");
  }

  public void _testListModelMerge8() {
    assertMerge(new String[]{"a", "a", "b", "b"}, "b", "a", "b", "a");
  }

  public void testListModelMerge9() {
    assertMerge(new String[]{"a", "b"}, 1, 3, "e", "d", "a", "b");
  }

  public void testListModelMerge10() {
    assertMerge(new String[]{"a", "b"}, 0, -1, "c", "d");
  }

  private static void assertMerge(String[] items, int startSelection, int expectedSelection, String... newItems) {

    CollectionListModel<String> model = new CollectionListModel<String>();
    model.add(Arrays.asList(items));
    JBList list = new JBList(model);
    list.setSelectedIndex(startSelection);

    FinderRecursivePanel.mergeListItems(model, Arrays.asList(newItems));
    assertEquals(model.getSize(), newItems.length);
    for (int i = 0; i < newItems.length; i++) {
      assertEquals(newItems[i], model.getElementAt(i));
    }
    assertEquals(list.getSelectedIndex(), expectedSelection);
  }

  private static void assertMerge(String[] items, String... newItems) {
    assertMerge(items, -1, -1, newItems);
  }

  public void testUpdate() {
    StringFinderRecursivePanel panel_0 = new StringFinderRecursivePanel(getProject()) {
      @NotNull
      @Override
      protected JComponent createRightComponent(String s) {
        return new StringFinderRecursivePanel(this) {
          @Override
          @NotNull
          protected JComponent createRightComponent(String s) {
            return new StringFinderRecursivePanel(this) {
              @Override
              @NotNull
              protected JComponent createRightComponent(String s) {
                return new StringFinderRecursivePanel(this);
              }
            };
          }
        };
      }
    };
    Disposer.register(myTestRootDisposable, panel_0);
    panel_0.getList().setSelectedIndex(0);
    //panel_0.updateRightComponent(true);

    StringFinderRecursivePanel panel_1 = (StringFinderRecursivePanel)panel_0.getChildPanel();
    panel_1.getList().setSelectedIndex(1);

    StringFinderRecursivePanel panel_2 = (StringFinderRecursivePanel)panel_1.getChildPanel();
    panel_2.getList().setSelectedIndex(2);

    StringFinderRecursivePanel panel_3 = (StringFinderRecursivePanel)panel_2.getChildPanel();
    panel_3.getList().setSelectedIndex(3);

    panel_0.updatePanel();

    assertEquals(panel_0.getSelectedValue(), "a");
    assertEquals(panel_1.getSelectedValue(), "b");
    assertEquals(panel_2.getSelectedValue(), "c");
    assertEquals(panel_3.getSelectedValue(), "d");
  }


  private class StringFinderRecursivePanel extends FinderRecursivePanel<String> {

    private StringFinderRecursivePanel(Project project) {
      super(project, "fooPanel");
      init();
    }

    public StringFinderRecursivePanel(StringFinderRecursivePanel panel) {
      super(panel);
      init();
    }

    @NotNull
    @Override
    protected List<String> getListItems() {
      return Arrays.asList("a", "b", "c", "d");
    }

    @Override
    protected JBList createList() {
      JBList list = super.createList();
      ((CollectionListModel)list.getModel()).replaceAll(getListItems());
      return list;
    }
  }
}

move to platform-tests