/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * 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.kotcrab.vis.editor.module.scene.entitymanipulator;

import com.artemis.Entity;
import com.badlogic.gdx.utils.Array;
import com.kotcrab.vis.editor.module.scene.system.EntitiesCollector;
import com.kotcrab.vis.editor.module.scene.system.EntitiesCollector.Result;
import com.kotcrab.vis.editor.proxy.EntityProxy;
import com.kotcrab.vis.runtime.util.ImmutableArray;

import java.util.function.Consumer;

/** @author Kotcrab */
public class EntitiesSelection {
	private final EntitiesCollector collector;
	private final int groupId;
	private final int layerId;

	private final Array<EntityProxy> selectedProxies;
	private final Array<SelectionFragment> selectionFragments;
	private final ImmutableArray<SelectionFragment> immutableSelectionFragments;
	private final ImmutableArray<EntityProxy> immutableSelectedProxies;

	public EntitiesSelection (EntitiesCollector collector, int layerId) {
		this(collector, layerId, -1);
	}

	public EntitiesSelection (EntitiesCollector collector, int layerId, int groupId) {
		this.collector = collector;
		this.groupId = groupId;
		this.layerId = layerId;

		this.selectedProxies = new Array<>();
		this.selectionFragments = new Array<>();
		this.immutableSelectionFragments = new ImmutableArray<>(selectionFragments);
		this.immutableSelectedProxies = new ImmutableArray<>(selectedProxies);
	}

	public ImmutableArray<EntityProxy> getSelection () {
		return immutableSelectedProxies;
	}

	public ImmutableArray<SelectionFragment> getFragmentedSelection () {
		return immutableSelectionFragments;
	}

	public int getGroupId () {
		return groupId;
	}

	public int getLayerId () {
		return layerId;
	}

	public int size () {
		return selectedProxies.size;
	}

	public EntityProxy peek () {
		return selectedProxies.peek();
	}

	public void forEach (Consumer<EntityProxy> consumer) {
		selectedProxies.forEach(consumer);
	}

	public void forEachEntity (Consumer<Entity> consumer) {
		selectedProxies.forEach(proxy -> consumer.accept(proxy.getEntity()));
	}

	public boolean isSelected (EntityProxy proxy) {
		return selectedProxies.contains(proxy, true);
	}

	void clearSelection () {
		selectedProxies.clear();
		selectionFragments.clear();
	}

	void append (EntityProxy proxy) {
		int lastProxyGid = (groupId == -1) ? proxy.getLastGroupId() : proxy.getGroupIdBefore(groupId);
		if (lastProxyGid != -1) {
			if (isGroupIdAlreadySelected(lastProxyGid) == false) {
				Result result = collector.createSelectionFragment(layerId, lastProxyGid);
				selectionFragments.add(result.fragment);
				selectedProxies.addAll(result.proxies.toArray());
			}
		} else {
			SelectionFragment fragment = new SingleSelectionFragment(proxy);
			selectedProxies.add(proxy);
			selectionFragments.add(fragment);
		}
	}

	public boolean isGroupIdAlreadySelected (int gid) {
		for (SelectionFragment fragment : selectionFragments) {
			if (fragment instanceof GroupSelectionFragment) {
				GroupSelectionFragment groupFragment = (GroupSelectionFragment) fragment;
				if (groupFragment.getGroupId() == gid) return true;
			}
		}

		return false;
	}

	void deselect (EntityProxy proxy) {
		Result result = collector.createSelectionFragment(layerId, (groupId == -1) ? proxy.getLastGroupId() : proxy.getGroupIdBefore(groupId));

		selectionFragments.removeValue(result.fragment, false); //must use equals compare
		selectedProxies.removeAll(result.proxies, true);
	}

	boolean isEnterIntoGroupValid () {
		if (selectionFragments.size != 1) return false;
		return selectionFragments.first() instanceof GroupSelectionFragment;
	}
}

@@ -3094,32 +3094,32 @@
 lect
-ionFragment
+edProxie
 s.add
+All
 (result.
 frag
@@ -3114,24 +3114,33 @@
 (result.
-fragment
+proxies.toArray()
 );%0A%09%09%09%09s
@@ -3148,32 +3148,32 @@
 lect
-edProxie
+ionFragment
 s.add
-All
 (result.
 prox
@@ -3168,33 +3168,24 @@
 (result.
-proxies.toArray()
+fragment
 );%0A%09%09%09%7D%0A
@@ -3335,24 +3335,599 @@
 t);%0A%09%09%7D%0A%09%7D%0A%0A
+%09void deselect (EntityProxy proxy) %7B%0A%09%09int lastProxyGid = (groupId == -1) ? proxy.getLastGroupId() : proxy.getGroupIdBefore(groupId);%0A%0A%09%09if (lastProxyGid != -1) %7B%0A%09%09%09Result result = collector.createSelectionFragment(layerId, lastProxyGid);%0A%0A%09%09%09selectedProxies.removeAll(result.proxies, true);%0A%09%09%09selectionFragments.removeValue(result.fragment, false); //must use equals compare%0A%09%09%7D else %7B%0A%09%09%09SelectionFragment fragment = new SingleSelectionFragment(proxy);%0A%09%09%09selectedProxies.removeValue(proxy, true);%0A%09%09%09selectionFragments.removeValue(fragment, false); //use equals%0A%09%09%7D%0A%09%7D%0A%0A
 %09public bool
@@ -4246,323 +4246,8 @@
 %09%7D%0A%0A
-%09void deselect (EntityProxy proxy) %7B%0A%09%09Result result = collector.createSelectionFragment(layerId, (groupId == -1) ? proxy.getLastGroupId() : proxy.getGroupIdBefore(groupId));%0A%0A%09%09selectionFragments.removeValue(result.fragment, false); //must use equals compare%0A%09%09selectedProxies.removeAll(result.proxies, true);%0A%09%7D%0A%0A
 %09boo

Fix EntitiesSelect deselect, #104