package jsettlers.logic.map;

import jsettlers.logic.map.save.loader.MapLoader;

import java.io.*;
import java.util.Vector;

/**
 * @author codingberlin
 */
public class OriginalMapLoader {
	public MapLoader loadOriginalMap(File originalMapFile) {
		try {
			OriginalMapFileContent mapContent = new OriginalMapFileContent(originalMapFile);

			if (!mapContent.isChecksumValid()) {
				System.out.println("Checksum of original map was not valid!");
				return null;
			}
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public class OriginalMapFileContent {

		long[] mapContent;

		public OriginalMapFileContent(File originalMapFile) throws IOException {
			DataInputStream dis = new DataInputStream(new FileInputStream(originalMapFile));
			Vector<Long> mapContent = new Vector<Long>();

			while (dis.available() > 0) {
				long currentByte = dis.readUnsignedByte();
				mapContent.add(currentByte);
			}
			dis.close();

			this.mapContent = new long[mapContent.size()];
			for (int i = 0; i < mapContent.size(); i++) {
				this.mapContent[i] = mapContent.get(i);
			}
		}

		public int readBytesFrom(int numberOfBytes, int offset) {
			int result = 0;
			for (int i = offset; i < offset+numberOfBytes; i++) {
				result += mapContent[i] << (i - offset) * 8;
			}
			return result;
		}

		boolean isChecksumValid() {
			long fileChecksum = readBytesFrom(4, 0);
			System.out.println(fileChecksum);
			int count = mapContent.length - 8;

			long currentChecksum = 0;
			for (int i = 8; i < count; i+=4) {
				long currentInt = readBytesFrom(4, i);
				currentChecksum = ((currentChecksum >> 31) | ((currentChecksum << 1) ^ currentInt));
				if (i < 40) {
					System.out.println(i + " : " + currentInt + " : " + currentChecksum);
				}
				if (currentChecksum == fileChecksum) {
					System.out.println("Treffer " + i);
				}
			}

			if (currentChecksum != fileChecksum) {
				return false;
			}

			return true;
		}

	}

	public static void main(String[] args) {
		(new OriginalMapLoader()).loadOriginalMap(new File("/Users/stephanbauer/Documents/siedler3/Map/MULTI/a.map"));
	}

}

@@ -1480,20 +1480,19 @@
  8;%0A%0A%09%09%09
-long
+int
  current
@@ -1613,33 +1613,12 @@
  = (
-(currentChecksum %3E%3E 31) %7C
+int)
  ((c
@@ -1636,12 +1636,11 @@
 sum 
-%3C%3C 1
+* 2
 ) %5E 
@@ -1650,17 +1650,16 @@
 rentInt)
-)
 ;%0A%09%09%09%09if
@@ -2039,55 +2039,34 @@
 e(%22/
-Users/stephanbauer/Documents/siedler3/Map/MULTI
+data/home/sbauer/Downloads
 /a.m

change long into int