//GeoGet 2
//
// Trekbuddy Full GPX, verze 2010-04-30
// Web:     http://www.zby.cz/geocaching/gge
// License: public domain



function ExportExtension: string;
begin
  result := 'GPX';
end;

function ExportDescription: string;
begin
  result := 'TrekBuddy Full GPX ------';
end;



// nefunguje pro UTF-8 retezce :-(
function MySubStr(mystr:string;  start:integer; len:integer): string;
begin
  result := Copy(mystr, start, len);
end;

// pouzil bych na radce 113 (jmeno kese u ADD WP)
function MyShorten(mystr:string; len:integer): string;
begin
  if Length(mystr) >= len then
    result := MySubStr(mystr, 0, len-1) + '..'
  else
    result := mystr;
end;


function mycdata(str:string): string;
begin
	result := cdata(str);
end;



function ExportHeader: string;
begin
  Result := '<?xml version="1.0" encoding="utf-8"?>'
         + '<gpx>';
         //+ '<desc>Geocaching.com - ' + Geoget_Version + '</desc>'
         //+ '<time>' + formatdatetime('yyyy"-"mm"-"dd"T"hh":"nn', now) + '</time>';
end;

function ExportFooter: string;
begin
  result := '</gpx>' + CRLF;
end;


function ExportPoint: string;
var
  s: string;
  txtlogs: string;
  n: integer;
begin
  Result := '';
  
  //Export for Geocaches
  if GC.IsListed then
  begin
    Result := Result + '<wpt lat="' + GC.Lat + '" lon="' + GC.Lon + '">';
    Result := Result + '<name>' + GC.ID +'</name>';
    Result := Result + '<cmt>' + mycdata(MySubStr(GC.CacheType,0,1) + MySubStr(GC.Size,0,1) + GC.Difficulty + '/' + GC.Terrain) +'</cmt>';


    if GC.CacheType = 'Unknown Cache' then s := '?'
    else if GC.CacheType = 'Multi-cache' then s := '×'
    else if GC.CacheType = 'Traditional Cache' then s := ''
    else s := '*';

		if GC.IsDisabled then
			s := '<dis>'+s;
		if GC.IsArchived then
			s := '<archived>'+s;


    Result := Result + '<cache>';
    Result := Result + '<name>' + mycdata(s+GC.Name) + '</name>';

    Result := Result + '<type>' + MySubStr(GC.CacheType,0,1) +'</type>'         //TrekBuddy tez zobrazi jen prvni znak
                     + '<container>' + MySubStr(GC.Size,0,1) +'</container>'
                     + '<difficulty>' + GC.Difficulty +'</difficulty>'
                     + '<terrain>' + GC.Terrain +'</terrain>';

    Result := Result + '<short_description>' + mycdata('Hint: ' + GC.Hint + CRLF+CRLF + GC.ShortDescription) +'</short_description>';
    
    // VYGENERUJEME LOGY A PRIDAME NA KONEC LONG-DESCRIPTIONu
    if GC.Logs.Count > 0 then
    begin
      txtlogs := CRLF + '-----LOGS------';
      for n := 0 to GC.Logs.Count - 1 do
      begin
        txtlogs := txtlogs + CRLF
              + formatdatetime('dd"-"mm"-"yyyy', GC.Logs[n].Date) + ' ' + MySubStr(GC.Logs[n].LogType,0,5) + CRLF
              + GC.Logs[n].Text +  ' ~'+GC.Logs[n].Finder + CRLF;
      end;
    end;
    Result := Result + '<long_description>' + mycdata(GC.LongDescription + txtlogs) + '</long_description>';
    
    
    //Result := Result + '<encoded_hints>' + mycdata(GC.Hint) +'</encoded_hints>';  //trekbuddy to ukazuje až na konci
    Result := Result + '</cache>';
    Result := Result + '</wpt>';
  end;
  
  //Export for Waypoints
  for n := 0 to GC.Waypoints.Count - 1 do
    if GC.Waypoints[n].IsListed then
    begin
      Result := Result
              + '<wpt lat="' + GC.Waypoints[n].Lat + '" lon="' + GC.Waypoints[n].Lon + '">'
              + '<name></name>'
              + '<cmt> </cmt>'
              + '<cache>'
                + '<name>'+mycdata('+ '+GC.Waypoints[n].Name+' ('+s+GC.Name+')')+'</name>'
                + '<type>' + MySubStr(GC.CacheType,0,1) +'</type>'
                + '<container>' + MySubStr(GC.Size,0,1) +'</container>'
                + '<difficulty>' + GC.Difficulty +'</difficulty>'
                + '<terrain>' + GC.Terrain +'</terrain>'
                + '<short_description>'+mycdata('['+GC.Waypoints[n].ID+'] '+GC.Waypoints[n].Description)+'</short_description>'
              + '</cache>'
              + '</wpt>';
    end;
end;




