
package com.exedio.copernica.admin;

import javax.servlet.http.HttpServletRequest;

import com.exedio.cope.lib.ReportTable;
import com.exedio.cops.Cop;

// TODO: should not be public
public final class AdminCop extends Cop
{
	static final String REPORT = "report";
	static final String SHOW = "show";
	static final String SHOW_DROP_BOXES = "drop";
	static final String SHOW_RENAME_FIELDS = "rename";

	// TODO: should not be public
	public final boolean report;
	final String reportTable;
	// TODO: should not be public
	public final boolean showDropBoxes;
	// TODO: should not be public
	public final boolean showRenameFields;

	AdminCop()
	{
		this(false, null, false, false);
	}

	private AdminCop(final boolean report, final String reportTable, final boolean showDropBoxes, final boolean showRenameFields)
	{
		super("admin.jsp");
		this.report = report;
		this.reportTable = reportTable;
		this.showDropBoxes = showDropBoxes;
		this.showRenameFields = showRenameFields;
		if(!report && reportTable!=null)
			throw new RuntimeException();
		
		if(report)
			addParameter(REPORT, reportTable==null ? "" : reportTable);
		if(showDropBoxes)
			addParameter(SHOW, SHOW_DROP_BOXES);
		if(showRenameFields)
			addParameter(SHOW, SHOW_RENAME_FIELDS);
	}
	
	// TODO: should not be public
	public final AdminCop toggleReport()
	{
		return new AdminCop(!report, null, false, false);
	}
	
	// TODO: should not be public
	public final AdminCop narrowReport(final ReportTable reportTable)
	{
		if(!report)
			throw new RuntimeException();
			
		return new AdminCop(true, reportTable.name, showDropBoxes, showRenameFields);
	}
	
	// TODO: should not be public
	public final AdminCop widenReport()
	{
		if(!report)
			throw new RuntimeException();
			
		return new AdminCop(true, null, showDropBoxes, showRenameFields);
	}
	
	// TODO: should not be public
	public final AdminCop toggleDropBoxes()
	{
		if(!report)
			throw new RuntimeException();

		return new AdminCop(true, reportTable, !showDropBoxes, showRenameFields);
	}
	
	// TODO: should not be public
	public final AdminCop toggleRenameFields()
	{
		if(!report)
			throw new RuntimeException();

		return new AdminCop(true, reportTable, showDropBoxes, !showRenameFields);
	}
	
	// TODO: should not be public
	public final boolean isNarrowReport()
	{
		return reportTable!=null;
	}
	
	// TODO: should not be public
	public final boolean skipTable(final ReportTable table)
	{
		return reportTable!=null && !reportTable.equals(table.name);
	}

	// TODO: should not be public
	public static final AdminCop getCop(final HttpServletRequest request)
	{	
		final String reportID = request.getParameter(REPORT);
		if(reportID==null)
		{
			return new AdminCop(false, null, false, false);
		}
		else
		{
			boolean showDropBoxes = false;
			boolean showRenameFields = false;

			final String[] showIDs = request.getParameterValues(SHOW);
			if(showIDs!=null)
			{
				for(int i = 0; i<showIDs.length; i++)
				{
					final String showID = showIDs[i];
					if(SHOW_DROP_BOXES.equals(showID))
						showDropBoxes = true;
					else if(SHOW_RENAME_FIELDS.equals(showID))
						showRenameFields = true;
					else
						throw new RuntimeException(showID);
				}
			}
			
			if(reportID.length()==0)
				return new AdminCop(true, null, showDropBoxes, showRenameFields);
			else
				return new AdminCop(true, reportID, showDropBoxes, showRenameFields);
		}
	}

}

@@ -151,45 +151,8 @@
 p;%0A%0A
-// TODO: should not be public%0Apublic 
 fina
@@ -363,46 +363,8 @@
 ;%0A%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -413,114 +413,38 @@
 e;%0A%09
-// TODO: should not be public%0A%09public final boolean showDropBoxes;%0A%09// TODO: should not be public%0A%09public 
+final boolean showDropBoxes;%0A%09
 fina
@@ -1101,46 +1101,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1192,46 +1192,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1391,46 +1391,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1548,46 +1548,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1714,46 +1714,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1883,46 +1883,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -1951,46 +1951,8 @@
 %0A%09%0A%09
-// TODO: should not be public%0A%09public 
 fina
@@ -2071,46 +2071,8 @@
 %7D%0A%0A%09
-// TODO: should not be public%0A%09public 
 stat

make things package-private again, that where made public because of the new apackage com.exedio.copernica.admin