package com.lea.dao.script.mission;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;

import com.lea.dao.script.AbstractScriptDAO;
import com.lea.domain.Mission;

public class InitMissionDAO extends AbstractScriptDAO {

	/**
	 * Write the init.sqf file into mission folder
	 */

	@Override
	public void writeScript(Object object, String path, String scriptName)
			throws IOException {

		String missionFolderPath = ((Mission) object).getMissionFolderPath();
		String relativePath = path.subSequence(missionFolderPath.length(),
				path.length()).toString();

		if (relativePath.length() != 0) {
			relativePath = relativePath.substring(1) + "\\";
		}

		File initFile = new File(missionFolderPath + "/" + scriptName);

		String command = "leaFunction =  compile preprocessFileLineNumbers "
				+ "\"" + relativePath + "lea\\loadout-init.sqf" + "\""
				+ "; call leaFunction;" + "//line generated by LEA.";

		if (!initFile.exists()) {
			PrintWriter fWo = new PrintWriter(new FileWriter(initFile));
			fWo.println(command);
			fWo.close();
		} else {
			DataInputStream fRo = new DataInputStream(new FileInputStream(
					missionFolderPath + "/" + "init.sqf"));
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					fRo));

			DataOutputStream fWo = new DataOutputStream(new FileOutputStream(
					missionFolderPath + "/" + "init.sqf.tmp"));
			BufferedWriter writter = new BufferedWriter(new OutputStreamWriter(
					fWo));

			// String text = "_dummy = [] execVM " + "\"" + relativePath
			// + "lea\\loadout-init.sqf" + "\"" + ";"
			// + "//call for lea loadout scripts, line generated by LEA.";

			String ligne = "";
			ligne = reader.readLine();
			if (ligne == null) {
				ligne = command;
				writter.write(ligne);
				writter.newLine();
			} else if (ligne.contains("loadout-init.sqf")) {
				ligne = command;
				writter.write(ligne);
				writter.newLine();
			} else {
				writter.write(command);
				writter.newLine();
				writter.write(ligne);
				writter.newLine();
			}

			while (true) {
				ligne = reader.readLine();

				if (ligne == null) {
					break;
				}

				writter.write(ligne);
				writter.newLine();
			}

			writter.flush();
			reader.close();
			writter.close();
			fRo.close();

			deleteFile(initFile);
			File sourceLocation = new File(missionFolderPath + "/"
					+ "init.sqf.tmp");
			File targetLocation = new File(missionFolderPath + "/" + "init.sqf");
			copyFile(sourceLocation, targetLocation);
			deleteFile(sourceLocation);

			fWo.close();
		}
	}

	private void copyFile(File sourceLocation, File targetLocation)
			throws IOException {

		FileChannel source = null;
		FileChannel destination = null;
		try {
			source = new FileInputStream(sourceLocation).getChannel();
			destination = new FileOutputStream(targetLocation).getChannel();
			destination.transferFrom(source, 0, source.size());
		} finally {
			if (source != null) {
				source.close();
			}
			if (destination != null) {
				destination.close();
			}
		}
	}

	private boolean deleteFile(File file) {
		boolean response = false;
		if (file.exists()) {
			response = file.delete();
		}
		return response;
	}
}
