/*
 * Copyright (c) 2021, L. Adamson
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies,
 * either expressed or implied, of L. Adamson or DizzyDragon.net.
 * 
 */

package labyrinth.refmaterial.tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import labyrinth.refmaterial.decoders.DecodedDungeon;
import labyrinth.refmaterial.decoders.DecodedDungeon.DecodedSpc;
import labyrinth.refmaterial.decoders.DndDecoder;
import labyrinth.refmaterial.decoders.pcdnd.PcDndDecoder;
import labyrinth.refmaterial.decoders.pcdnd.PcDotndDecoder;
import labyrinth.refmaterial.decoders.unixdnd.UnixDndDecoder;
import labyrinth.refmaterial.decoders.vmsdnd.VmsDndDecoder;

public class ExtractMaps
{
	private static File baseDir = new File( "Historical Data/Maps" );
	private static DndDecoder pcDecoder = new PcDndDecoder();
	private static DndDecoder vmsDecoder = new VmsDndDecoder();
	private static DndDecoder unixDecoder = new UnixDndDecoder();
	private static DndDecoder dotndDecoder = new PcDotndDecoder();
		
	public static void main( String[] args ) throws FileNotFoundException
	{
		dumpMaps( pcDecoder, "PC" );
		dumpMaps( vmsDecoder, "VMS" );
		dumpMaps( unixDecoder, "UNIX" );
		dumpMaps( dotndDecoder, "DotND" );
		
		dumpDiffs();
		
		System.out.println( "Done!" );
	}
	
	private static void dumpMaps( DndDecoder decoder, String type ) throws FileNotFoundException
	{
		PrintStream out;
		File file;
		for( DecodedDungeon dungeon : decoder.getDungeons() )
		{
			System.out.print( type+"."+dungeon.getName()+": " );
			
			File infoFile = new File( baseDir, type+"."+dungeon.getName()+"/Info.txt" );
			infoFile.getParentFile().mkdirs();
			out = new PrintStream( new FileOutputStream(infoFile) );
			out.println( "Start Location: "+dungeon.getStartX()+","+dungeon.getStartY() );
			for( int level=1; level<=20; level++ )
			{
				for( int y=0; y<20; y++ )
				{
					for( int x=0; x<20; x++ )
					{
						if( dungeon.getSpc( x, y, level ) == DecodedSpc.DGO )
							out.println( "DGO/VMS or ORB/PC on level "+level+" at "+x+","+y );
						if( dungeon.getSpc( x, y, level ) == DecodedSpc.ORB )
							out.println( "ORB/VMS or MIR/PC on level "+level+" at "+x+","+y );
						if( dungeon.getSpc( x, y, level ) == DecodedSpc.RCK )
							out.println( "RCK/VMS or GNI/PC on level "+level+" at "+x+","+y );
					}
				}
			}
			out.close();
			
			file = new File( baseDir, type+"."+dungeon.getName()+"/Block/00 Map Key.txt" );
			file.getParentFile().mkdirs();
			out = new PrintStream( new FileOutputStream(file) );
			out.println( "+ - Door" );
			out.println( "S - Secret Door" );
			out.println();
			out.println( "1 - Downward Staircase" );
			out.println( "2 - Upward Staircase" );
			out.println( "3 - Spiral Staircase (up/down)" );
			out.println( "4 - Excelsior Transporter" );
			out.println( "5 - Pit" );
			out.println( "6 - Teleporter" );
			out.println( "7 - Fountain" );
			out.println( "8 - Altar" );
			out.println( "9 - Dragon's Lair" );
			out.println( "a - Dragon, Guarding the Orb (The Orb with no Dragon in PC-DND)" );
			out.println( "b - The Orb (Magic Mirror in PC-DND)" );
			out.println( "c - Elevator" );
			out.println( "d - Throne" );
			out.println( "e - Treasure Vault" );
			out.println( "f - Solid Rock (Genie in PC-DND)" );
			out.close();					
			
			for( int level = 1; level <= 20; level++ )
			{
				if( dungeon.isValidLevel( level ) )
				{
					System.out.print( " "+level );
					String levelStr = String.format( "%02d", level );
					
					file = new File( baseDir, type+"."+dungeon.getName()+"/Legacy/Level"+levelStr+".txt" );
					file.getParentFile().mkdirs();
					out = new PrintStream( new FileOutputStream(file) );
					dungeon.printMap( level, out );
					out.close();
					
					file = new File( baseDir, type+"."+dungeon.getName()+"/Block/Level"+levelStr+".txt" );
					file.getParentFile().mkdirs();
					out = new PrintStream( new FileOutputStream(file) );
					dungeon.printBlockMap( level, out );
					out.close();
				}
			}
			System.out.println();
			
		}
	}
	
	private static void dumpDiffs() throws FileNotFoundException
	{
		System.out.println( "Diffing Dungeons..." );
		
		PrintStream out;
		String s1, s2;
		
		s1 = diffDungeons( vmsDecoder.getDungeon("TELENGARD"), pcDecoder.getDungeon("Telengard") );
		s2 = diffDungeons( vmsDecoder.getDungeon("TELENGARD"), unixDecoder.getDungeon("Squire's Hole") );
		if( s1 != null )
		{
			s1 = "PC Telengard differs from VMS Telengard:\n"+s1;
			if( s2 != null )
				s1 += "\n\nUnix Squire's Hole differs from VMS Telengard:\n"+s2;
		}
		else if( s2 != null )
		{
			s1 = "Unix Squire's Hole differs from VMS Telengard:\n"+s2;
		}
		if( s1 != null )
		{
			out = new PrintStream( new FileOutputStream(new File(baseDir,"Diff.Telengard.txt")) );
			out.print( s1 );
			out.close();
		}
		
		s1 = diffDungeons( vmsDecoder.getDungeon("SVHENK'S LAIR"), pcDecoder.getDungeon("Shvenk's Lair") );
		if( s1 != null )
		{
			out = new PrintStream( new FileOutputStream(new File(baseDir,"Diff.Svhenk's Lair.txt")) );
			out.println( "PC Shvenk's Lair differs from VMS Svhenk's Lair:" );
			out.print( s1 );
			out.close();
		}
		
		s1 = diffDungeons( vmsDecoder.getDungeon("LAMORTE"), pcDecoder.getDungeon("Lamorte") );
		if( s1 != null )
		{
			out = new PrintStream( new FileOutputStream(new File(baseDir,"Diff.LaMorte.txt")) );
			out.println( "PC Lamorte differs from VMS Lamorte:" );
			out.print( s1 );
			out.close();
		}
		
		s1 = diffDungeons( vmsDecoder.getDungeon("WARREN"), pcDecoder.getDungeon("Warren") );
		if( s1 != null )
		{
			out = new PrintStream( new FileOutputStream(new File(baseDir,"Diff.Warren.txt")) );
			out.println( "PC Warren differs from VMS Warren:" );
			out.print( s1 );
			out.close();
		}
		
		s1 = diffDungeons( vmsDecoder.getDungeon("SJKII"), pcDecoder.getDungeon("The Cavern") );
		if( s1 != null )
		{
			out = new PrintStream( new FileOutputStream(new File(baseDir,"Diff.The Cavern.txt")) );
			out.println( "PC The Cavern differs from VMS SJKII:" );
			out.print( s1 );
			out.close();
		}
	}
	
	private static String diffDungeons( DecodedDungeon d1, DecodedDungeon d2 )
	{
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		PrintStream out = new PrintStream( ba );
		
		int x, y, level;
		for( level=1; level<=20; level++ )
		{
			boolean diff=false;
			for( y=0; y<20; y++ )
			{
				for( x=0; x<20; x++ )
				{
					if( (d1.getRawByte(x,y,level) != d2.getRawByte(x,y,level)) )
					{
						if( d1.isValidRoom(x,y,level) || d2.isValidRoom(x,y,level) )
						{
							if( !diff )
							{
								diff = true;
								out.print( " - Level "+level+":" );
							}
							out.print( " "+x+","+y );
						}
					}
				}
			}
			if( diff )
				out.println();
		}
		
		String rv = ba.toString();
		if( rv.length() <= 0 )
			return null;
		return rv;
	}
	
	private ExtractMaps()
	{
	}
}
