/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

( function()
{
	// Run tests.
	var passCount = 0, failCount = 0;
	CKLANGTOOL.metaFile = '_assets/gettext/meta.txt';

	function deleteDirectory( path )
	{
		var dir = new File( path );

		if ( dir.isDirectory() )
		{
			var children = dir.list();
			for ( var i = 0 ; i < children.length ; i++ )
			{
				if ( !deleteDirectory( new File( dir, children[i] ) ) )
				{
					return false;
				}
			}
		}

		return dir["delete"]();
	}
	
	function copyFile( sourceFile, targetFile )
	{
		try
		{
			var inStream = new FileInputStream( sourceFile );
			var outStream = new FileOutputStream( targetFile );

			var len, buf = new Packages.java.lang.reflect.Array.newInstance( java.lang.Byte.TYPE, 1024 );

			while ( ( len = inStream.read( buf ) ) != -1 )
			{
				outStream.write( buf, 0, len );
			}
			inStream.close();
			outStream.close();
		}
		catch ( e )
		{
			throw "Cannot copy file:\n Source: " + sourceFile.getCanonicalPath() + "\n Destination : "
					+ targetFile.getCanonicalPath() + "\n" + e.message;
		}
	}	

	function normalizeDate( content )
	{
		var fullYear = new Date().getFullYear();
		return content.replace( /__CURRENT_YEAR__/g, fullYear )
			.replace(/"PO-Revision-Date:.*?"/g,'' )
			.replace(/"POT-Creation-Date:.*?"/g,'' );
	}

	function assertEquals( expected, actual, title )
	{
		if ( expected != actual )
		{
			var error = {
				expected :expected,
				actual :actual
			};

			print( 'FAILED: ' + (title ? title : "") );

			if ( !error.expected )
				throw error;

			print( '  Expected: ' + error.expected );
			print( '  Actual  : ' + error.actual );

			failCount++;
		}
		else
			passCount++;
	}

	function error( msg )
	{
		print( msg );
		quit();
	}

	function prepareTempDirs()
	{
		var tempDir = new File( 'tmp' );

		if ( tempDir.exists() && !deleteDirectory( 'tmp' ) )
			error( "Can't delete temp directory" );

		if ( !tempDir.mkdir() )
			error( "Can't create temp directory: " + tempDir );

		var tests = [ 'gettext', 'gettext/out', 'jprops', 'jprops/out', 'translator', 'translator2' ];

		for ( var i = 0 ; i < tests.length ; i++ )
		{
			tempDir = new File( 'tmp/' + tests[i] );
			if ( !tempDir.mkdir() )
				error( "Can't create temp directory: " + tempDir );
		}
	}
	
	function testTranslator( folder )
	{
		CKLANGTOOL.languageDir = new File( 'tmp/' + folder );
		CKLANGTOOL.templateFile = new File( 'tmp/' + folder, 'en.js' );
		
		var dir = new File( '_assets/' + folder );
		var dirList = dir.list();
		
		for ( var i = 0 ; i < dirList.length ; i++ )
			if ( dirList[i] != '.svn' )
				copyFile( new File( dir, dirList[i] ), new File( CKLANGTOOL.languageDir, dirList[i] ) );

		var translator = new CKLANGTOOL.translator();
		translator.run( CKLANGTOOL.languageDir );

		dirList = CKLANGTOOL.languageDir.list();
		for ( var i = 0 ; i < dirList.length ; i++ )
		{
			if ( dirList[i].indexOf( ".correct." ) == -1 || dirList[i] == '.svn' )
				continue;
			
			var f = new File( CKLANGTOOL.languageDir, dirList[i] );

			testName = dirList[i].replace( ".correct.txt", "" );

			tempFile = new File( 'tmp/' + folder + '/' + testName );
			correctFile = new File( 'tmp/' + folder + '/' + testName + '.correct.txt' );

			var date = new Date(),
				fullYear = date.getFullYear();
			assertEquals( CKLANGTOOL.io.readFile( correctFile ).replace( /__CURRENT_YEAR__/g, fullYear ), CKLANGTOOL.io.readFile( tempFile ));
		}
	}
	
	function testFormat( format )
	{
		var tmpDir = 'tmp/' + format;
		CKLANGTOOL.languageDir = new File( tmpDir );
		CKLANGTOOL.destinationDir = new File( tmpDir + '/out' );
		CKLANGTOOL.templateFile = new File( tmpDir, 'en.js' );
		
		var dir = new File( '_assets/' + format );
		var dirList = dir.list();
		
		for ( var i = 0 ; i < dirList.length ; i++ )
		{
			if ( dirList[i] != '.svn' )
				copyFile( new File( dir, dirList[i] ), new File( CKLANGTOOL.languageDir, dirList[i] ) );
		}

		var formatter = new CKLANGTOOL[format]();
		formatter.run();

		dirList = CKLANGTOOL.languageDir.list();
		for ( var i = 0 ; i < dirList.length ; i++ )
		{
			if ( dirList[i].indexOf( ".correct." ) == -1 || dirList[i] == '.svn' )
				continue;
			
			var f = new File( CKLANGTOOL.languageDir, dirList[i] );

			testName = dirList[i].replace( ".correct.txt", "" );

			tempFile = new File( tmpDir + '/out/', testName );
			correctFile = new File( tmpDir, testName + '.correct.txt' );
			
			assertEquals( normalizeDate( CKLANGTOOL.io.readFile( correctFile ) ), normalizeDate( CKLANGTOOL.io.readFile( tempFile ) ) );
		}
	}

	prepareTempDirs();
	testTranslator('translator');
	testTranslator('translator2');
	testFormat('gettext');
	testFormat('jprops');

	print( '' );
	print( 'Finished: ' + passCount + ' passed / ' + failCount + ' failed' );

})();
