/*
 * Copyright 2012 s_wolff.
 *
 * 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.aegee.runanddine.pathfinder.optimizers;

import java.util.ArrayList;
import java.util.Collection;

import org.aegee.runanddine.pathfinder.Course;

/**
 * creates an abstract setting
 * 
 * @author s_wolff
 */
public class AbstractGroupBuilder
{

	public static Collection<AbstractGroup> makeAbstractGroups(int numberOfGroups)
	{
		ArrayList<AbstractGroup> result = new ArrayList<AbstractGroup>(numberOfGroups);

		if (numberOfGroups % 3 == 0)
		{
			int[][] hors = new int[numberOfGroups / 3][3];
			int[][] main = new int[numberOfGroups / 3][3];
			int[][] dessert = new int[numberOfGroups / 3][3];

			// fill arrays with 1..n
			for (int i = 0; i < numberOfGroups / 3; i++)
			{
				hors[i][0] = i;
				main[i][0] = i + (numberOfGroups / 3);
				dessert[i][0] = i + (2 * numberOfGroups / 3);
			}
			// assign the groups (basically it has different offsets and cycles through the group ids of other
			// categories)
			for (int i = 0; i < numberOfGroups / 3; i++)
			{
				hors[i][1] = main[(main.length + i - 1) % main.length][0];
				hors[i][2] = dessert[(dessert.length + i - 2) % dessert.length][0];

				main[i][1] = hors[i % hors.length][0];
				main[i][2] = dessert[(dessert.length + i - 3) % dessert.length][0];

				dessert[i][1] = main[(main.length + i - 2) % main.length][0];
				dessert[i][2] = hors[i % hors.length][0];
			}
			for (int i = 0; i < numberOfGroups / 3; i++)
			{
				result.add(new AbstractGroup(hors[i][0], hors[i][1], hors[i][2], Course.HORS_DOEUVRE));
				result.add(new AbstractGroup(main[i][0], main[i][1], main[i][2], Course.MAIN));
				result.add(new AbstractGroup(dessert[i][0], dessert[i][1], dessert[i][2], Course.DESSERT));
			}

			// RunAndDineApplication.debug(result);
			return result;
		}
		else
		{
			throw new IllegalArgumentException("Number of Groups mod 3 != 0");
		}
	}
}
