#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char const* kmlHeader = 
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
  "  <Document>\n"
  "    <name>Paths</name>\n"
  "    <description>from %s</description>\n"
  "    <Style id=\"yellowLineGreenPoly\">\n"
  "      <LineStyle>\n"
  "        <color>7f00ffff</color>\n"
  "        <width>4</width>\n"
  "      </LineStyle>\n"
  "      <PolyStyle>\n"
  "        <color>7f00ff00</color>\n"
  "      </PolyStyle>\n"
  "    </Style>"
  "    <Placemark>\n"
  "      <name>from %s</name>\n"
  "      <description>Transparent green wall with yellow outlines</description>\n"
  "      <styleUrl>#yellowLineGreenPoly</styleUrl>\n"
  "      <Point>\n"
  "         <coordinates>%lf,%lf</coordinates>\n"
  "      </Point>\n"
  "      <LineString>\n"
  "        <extrude>1</extrude>\n"
  //"        <tessellate>1</tessellate>\n"
  "        <altitudeMode>relativeToGround</altitudeMode>\n"
  "        <coordinates>\n";
static char const* kmlFooter = 
  "        </coordinates>\n"
  "      </LineString>\n"
  "    </Placemark>\n"
  "  </Document>\n"
  "</kml>\n";

const int INPUT_FILED_COUNT = 13;

void Txt2Kml() {
}

int main(int argc, char* argv[]) {
	if (argc != 2) {
		printf("Usage: txt2kml txtfile\n");
		return 1;
	}
  char const* inputFilePath = argv[1];
	FILE* input = fopen(inputFilePath, "r");
  if (!input) {
    perror("Filed to open input file");
    return 2;
  }

  static char buffer[4096];
	fgets(buffer, sizeof(buffer), input);
	fgets(buffer, sizeof(buffer), input);
  fgets(buffer, sizeof(buffer), input);
  double homeLongitude = 0, homeLatitude = 0;
  fscanf(input, "%s", buffer);
  fscanf(input, "%s", buffer);
  fscanf(input, "%lf", &homeLongitude);
  printf("%f\n", homeLongitude);
  fscanf(input, "%s", buffer);
  fscanf(input, "%s", buffer);
  fscanf(input, "%lf", &homeLatitude);

  static char kmlFileName[_MAX_FNAME];
  _splitpath(inputFilePath, NULL, NULL, kmlFileName,NULL); 
  strcat(kmlFileName, ".kml");
  FILE* kml = fopen(kmlFileName, "w");
  if (!kml) {
    perror("Failed to open output file");
    fclose(input);
    return 3;
  }

  fprintf(kml, kmlHeader, inputFilePath, inputFilePath, homeLongitude, homeLatitude);

  fgets(buffer, sizeof(buffer), input);
  fgets(buffer, sizeof(buffer), input);
  fgets(buffer, sizeof(buffer), input);
  static char time[0x20];
  static char mode[0x10];
  int satellite;
  double longitude, latitude, altitude;
  double groundSpeed, airspeed;
  double power;
  double video;
  double current;
  double distance;
  double temperature;
  do {
    int numOfFields = fscanf(input, "%s %s %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
      time, mode, &satellite, &longitude, &latitude, &altitude, &groundSpeed, &airspeed,
      &power, &video, &current, &distance, &temperature);
    if (numOfFields == INPUT_FILED_COUNT) {
      fprintf(kml, "%lf,%lf,%lf\n", longitude, latitude, altitude);
    }
  } while (!feof(input));
  fputs(kmlFooter, kml);
  
  fclose(kml);
  fclose(input);
}