package lis_to_html;

/**
 * Created by raub_ni on 12/12/14.
 */


import java.io.*;

import static java.lang.System.getProperty;
import static java.lang.System.out;

public class Main {

  private String  orbitNumber, html1, html2, lisFilePath,
      htmlOutputDirectoryPath, finalOrbitNumber;
  private boolean beforeSequence  = true;
  private int     sequenceCounter = 0;

  public static void main(String[] args) {
    new Main(args);
  }

  public Main(String[] args) {

    /*
     * char a = 'a'; out.println("char= " + a); char b = 'd'; //wenn das zeichen
     * hinter dem o ein buchstabe ist, muss man die differenz zwischen char b
     * und char a berechnen und mit 10 addieren out.println("char2= " + b);
     * 
     * out.println("char - char2 = " + (b - a + 10));
     */

    File file = null;

    for (String path : args) { // get program argument

      if (path.endsWith(".lis")) { // if the file is the .lis-file
        lisFilePath = path; // setLisFilePath to the argument
        file = new File(lisFilePath); // set filepath to path
      } else {
        htmlOutputDirectoryPath = path; // set directory for html files
      }

      // out.println("lis-filepath = " + file.getPath() + "\nlis-filename = " +
      // file.getName());
      out.println("htmlOutputDirectoryPath = " + htmlOutputDirectoryPath);
    }
    makeStrings(file); // define the strings
    writeHTML(file); // write and create html file
  }

  private void writeHTML(File file) {
    try {

      htmlOutputDirectoryPath = htmlOutputDirectoryPath + finalOrbitNumber
          + ".html"; // set the path for the html file

      // make writer
      OutputStreamWriter pw = new OutputStreamWriter(new FileOutputStream(
          htmlOutputDirectoryPath), "UTF-8");

      // write html1
      pw.write(html1);

      // make reader
      BufferedReader br = new BufferedReader(new InputStreamReader(
          new FileInputStream(file), "UTF8"));

      String contentLisFile;

      while ((contentLisFile = br.readLine()) != null) { 
        // set contentLisFile to readLine()
        // write contenLisFile

        String av = "�"; // � == °
        if (contentLisFile.contains(av)) { // if � is in the line
          String deg = contentLisFile.replace(av, " deg"); 
          // replace � with deg
          contentLisFile = "";
          pw.write(deg);
        } else if (contentLisFile.contains("Sequence#")) {
          beforeSequence = false;
          sequenceCounter++; // count how often Sequence# is written in the file
          contentLisFile = "Sequence #" + sequenceCounter; 
          // replace content of this line with Sequence
          // #howOftenIsSequenceWritten
        }

        if (!beforeSequence) { // if we are not before the first line containing
                               // sequence# (everything before the first
                               // sequence has to be deleted)
          pw.write(contentLisFile);

          // write new line
          pw.write("\n");
        }

        // out.println(contentLisFile);

      }

      // write html2
      pw.write(html2);

      // close reader
      pw.close();
      br.close();

    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

  private void makeStrings(File file) {
    orbitNumber = file.getName().substring(1); // delete 'o'
    orbitNumber = orbitNumber.substring(0, 4); // get first 4 chars

    String letter = orbitNumber.substring(0, 1); // get letter
    char letter2 = letter.charAt(0); // convert into char
    out.println("letter: " + letter);
    orbitNumber = orbitNumber.substring(1);
    finalOrbitNumber = "" + getNumber(letter2) + orbitNumber; 
    // define real orbitnumber
    out.println("fon: " + finalOrbitNumber);

    html1 = "<html>\n<head>\n<meta http-equiv=\"Content-Type\""
        + " content=\"text/html; charset=UTF-8\">"
        + "\n<title>"
        + finalOrbitNumber
        + "</title>\n</head>\n<body>\n<h2 style = margin-left:85px >Orbit "
        + finalOrbitNumber + "</h2>\n<pre>";

    html2 = "</pre>\n</body>\n</html>";

    out.println("fileencoding: " + getProperty("file.encoding"));

  }

  private int getNumber(char t) {
    char b = 'a';
    return t - b + 10;
  }

}
