//******************************************************************************
// LAYERS parameter: config_a_b_c
//    a = 1 for depth in meters, 2 for depth in feet, 3 for fathoms
//    b = 10.00: for 10.00 m safety depth (beginning of blue coloring) (unit equal to that set by a)
//    c = 0 for pristine Navionics charts, 1 for Sonar Charts
//
// TRANSPARENT parameter: 
//    FALSE for non-layer
//    TRUE for layer
//
// UGC parameter: 
//    FALSE for pristine Navionics charts
//    TRUE for additinal user-generated content icons
//******************************************************************************

const
  cReqParams = 'LAYERS=config_1_10.00_1&TRANSPARENT=FALSE&UGC=FALSE';

//******************************************************************************

const
  cTTL = 24*60*60; // 24 hour  
  cNavTokenKey = 'NavToken=';
  cTimeStampKey = 'TimeStamp=';
  
function CheckTTL(const ATimeStamp: AnsiString): Boolean;
begin
  Result := ( (cTTL div 2) > GetUnixTime - StrToInt64Def(ATimeStamp, 0) );   
end;

function GetNavToken: AnsiString;
var
  VNavToken: AnsiString;
  VTimeStamp: AnsiString;    
  VResponseCode: Cardinal;
  VResponseHeader, VResponseData: AnsiString;
  VRequestUrl, VRequestHeader: AnsiString;
begin
  Result := '';
  
  if ScriptBuffer <> '' then begin
    VNavToken := GetBetween(ScriptBuffer, cNavTokenKey, ';');
    VTimeStamp := GetBetween(ScriptBuffer, cTimeStampKey, ';');
    if (VNavToken <> '') and (VTimeStamp <> '') and CheckTTL(VTimeStamp) then begin
      Result := VNavToken;      
    end else begin
      ScriptBuffer := '';
    end;
  end;
  
  if (Result = '') and Assigned(Downloader) then begin    
    VTimeStamp := IntToStr(GetUnixTime);    
    VRequestUrl := 'https://backend.navionics.io/tile/get_key/Navionics_internalpurpose_00001/webapiv2.navionics.com?_=' + VTimeStamp;
    VRequestHeader := 'Origin: https://webapiv2.navionics.com' + #13#10 + 'Referer: https://webapiv2.navionics.com/examples/4000_gNavionicsOverlayExample.html';
    VResponseHeader := '';
    VResponseData := ''; 
    VResponseCode := Downloader.DoHttpRequest(VRequestUrl, VRequestHeader, '', VResponseHeader, VResponseData);
 
    if VResponseCode = 200 then begin
      Result := VResponseData;
      ScriptBuffer := cNavTokenKey + Result + ';' + cTimeStampKey + VTimeStamp + ';';
    end; 
  end;
end;

//******************************************************************************

var
  VNavToken: AnsiString;
begin
  VNavToken := GetNavToken;
  if VNavToken <> '' then begin
    ResultURL := GetURLBase + IntToStr(GetZ-1) + '/' + IntToStr(GetX) + '/' + 
      IntToStr(GetY) + '?' + cReqParams + '&' + 'navtoken=' + VNavToken;
  end else begin
    ResultURL := '';
  end;
end.
