//******************************************************************************
// transparent true/false
// ugc - garmin active captain
// layer - 0 Seachart, 1 Sonarchart
// du - depth unit  1 for depth in meters, 2 for depth in feet, 3 for fathoms
// sd - safe depth (shading)
// sa ?
const
  cReqParams = 'transparent=false&ugc=true&layer=0&du=1&sd=10&sa=false';

//******************************************************************************
const
  //cTTL = 24*60*60; // 24 hour
  cTTL = 10*60; // 10 Minutes
function CheckTTL(const ATimeStamp: Integer): Boolean;
begin
  Result := (cTTL div 2) > GetUnixTime - ATimeStamp;
end;

function GetNavToken: AnsiString;
var
  VNavTokensJSON: AnsiString;
  VTimeStamp: Integer; 
  VResponseCode: Cardinal;
  VResponseHeader, VResponseData: AnsiString;
  VRequestUrl, VRequestHeader: AnsiString;
begin
  Result := '';
  
  Global.Lock;
  try
    if Global.Exists(0) and Global.Exists(1) then begin
      VNavTokensJSON := Global.GetVar(0);
      VTimeStamp := Global.GetVar(1);
      if (VNavTokensJSON <> '') and (VTimeStamp <> 0) and CheckTTL(VTimeStamp) then begin
        Result := VNavTokensJSON;
      end; 
    end;
    
    if (Result = '') and Assigned(Downloader) then begin          
      VRequestUrl := 'https://maps.garmin.com/marine/api/getNavionicsTokens';
      VRequestHeader := 'Origin: https://maps.garmin.com' + #13#10 + 'Referer: https://maps.garmin.com/' + #13#10 + 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
      VResponseHeader := '';
      VResponseData := ''; 
      VResponseCode := Downloader.DoHttpRequest(VRequestUrl, VRequestHeader, '', VResponseHeader, VResponseData);
   
      if VResponseCode = 200 then begin
        Result := VResponseData;
        Global.SetVar(0, Result);
        Global.SetVar(1, GetUnixTime);
      end; 
    end;
  finally
    Global.Unlock;
  end;
end;

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

var
  VlNavToken: AnsiString;
  VlNavBearer: AnsiString;
  VBuf : AnsiString;
begin
  VBuf := GetNavToken;
  writeln('Got VBuf: ' + VBuf);
  VlNavToken := RegExprGetMatchSubStr(VBuf,'"configuration_token":"(.*)"',1);
  VlNavBearer := RegExprGetMatchSubStr(VBuf,'"access_token":"(.*)",',1);
  writeln('VlNavToken: '+VlNavToken);
  writeln('VlNavBearer: '+VlNavBearer);
  if VlNavToken <> '' then begin
    ResultURL := TemplateToUrl(GetURLBase) + '?' + 'config=' + VlNavToken + '&' + cReqParams;
    RequestHead:= 'Referer: https://maps.garmin.com/' + #13#10 +
                  'Origin: https://maps.garmin.com' + #13#10 +
                  'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36' + #13#10 +
                  'Accept: */*' + + #13#10 +
                  'Authorization: Bearer '+VlNavBearer + #13#10 +
                  'Connection: keep-alive' + #13#10 +
                  'Sec-Fetch-Dest: empty' + #13#10 +
                  'Sec-Fetch-Mode: cors' + #13#10 +
                  'Sec-Fetch-Site: cross-site' + #13#10 +
                  'Priority: u=4' + #13#10 +
                  'TE: trailers';

  end else begin
    ResultURL := '';
  end;
end.
