/*
 * 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.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import labyrinth.refmaterial.decoders.DecodedDungeon;
import labyrinth.refmaterial.decoders.DndDecoder;
import labyrinth.refmaterial.decoders.pcdnd.PcDndDecoder;
import labyrinth.refmaterial.decoders.unixdnd.UnixDndDecoder;

public class ExtractMapsToSMF
{
	private static File baseDir = new File( "Labyrinth/data/tmp-smf-extracted" );
	private static DndDecoder pcDecoder = new PcDndDecoder();
	private static DndDecoder unixDecoder = new UnixDndDecoder();
		
	public static void main( String[] args ) throws FileNotFoundException
	{
		dumpMaps( pcDecoder, "PC" );
		dumpMaps( unixDecoder, "UNIX" );
		
		System.out.println( "Done!" );
	}
	
	private static String getWallString( int wall )
	{
		if( wall == 0 )
			return "WALL_NONE";
		else if( wall == 1 )
			return "WALL_BLOCKED";
		else if( wall == 2 )
			return "WALL_DOOR";
		else if( wall == 3 )
			return "WALL_SECRET_DOOR";
		throw new RuntimeException( "Invalid wall type "+wall );
	}
	
	private static void dumpMaps( DndDecoder decoder, String type ) throws FileNotFoundException
	{
		PrintStream out;
		File file;
		for( DecodedDungeon dungeon : decoder.getDungeons() )
		{
			if( dungeon.getName().equals("Squire's Hole") )
				continue;
			
			System.out.print( type+"."+dungeon.getName()+": " );
			
			for( int level = 1; level <= 20; level++ )
			{
				if( dungeon.isValidLevel( level ) )
				{
					System.out.print( " "+level );
					
					String dname = dungeon.getName();
					String lname = "Level "+level;
					file = new File( baseDir, dname+"/"+lname+".smf" );
					file.getParentFile().mkdirs();
					out = new PrintStream( new FileOutputStream(file) );
					
					out.printf( "Dungeon|%s\n", dname );
					out.printf( "Level|%s\n", lname );

					for( int y=0; y<20; y++ )
					{
						for( int x=0; x<20; x++ )
						{
							if( dungeon.isValidRoom(x,y,level) )
							{
								int w;
								String s;
								w = dungeon.getRawNorthWall( x, y, level );
								s = getWallString( w );
								out.printf( "NorthWall|%d|%d|%s\n", x+1, y+1, s );
								w = dungeon.getRawWestWall( x, y, level );
								s = getWallString( w );
								out.printf( "WestWall|%d|%d|%s\n", x+1, y+1, s );
							}
						}
					}

					for( int y=0; y<20; y++ )
					{
						for( int x=0; x<20; x++ )
						{
							if( dungeon.isValidRoom(x,y,level) )
							{
								int spc = dungeon.getRawSpc( x, y, level );
								if( spc == 1 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_STAIRWAY_DOWN" );
								else if( spc == 2 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_STAIRWAY_UP" );
								else if( spc == 3 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_STAIRWAY_SPIRAL" );
								else if( spc == 4 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_EXCELSIOR" );
								else if( spc == 5 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_PIT" );
								else if( spc == 6 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_TELEPORTER" );
								else if( spc == 7 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_FOUNTAIN" );
								else if( spc == 8 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_ALTAR" );
								else if( spc == 9 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_DRAGON_LAIR" );
								else if( spc == 10 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_ORB" );
								else if( spc == 11 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_MAGIC_MIRROR" );
								else if( spc == 12 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_ELEVATOR" );
								else if( spc == 13 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_THRONE" );
								else if( spc == 14 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_TREASURE_VAULT" );
								else if( spc == 15 )
									out.printf( "Spc|%d|%d|%s\n", x+1, y+1, "SPC_GENIE" );
							}
						}
					}

					out.printf( "EndDungeon\n", dname );
					
					out.close();
				}
			}
			System.out.println();
			
		}
	}
	
	private ExtractMapsToSMF()
	{
	}
}
