// -----------------------------------------------------------------------------
// ---     AnyGIS
// -----------------------------------------------------------------------------

//         SasPlanet. 
//         Pascal,
//      GetUrlScript.txt     .
//      URL    {x}, {y}, {z}
//   DefURLBase   params.txt.  , :
//
// DefURLBase=http://{s:a,b,c}.tile.openstreetmap.org/{z}/{x}/{y}.png


//   Nnngrach  Erelen

 


// ---     :

// {x}              X. (   OpenStreetMaps)
// {y}              Y. (   OpenStreetMaps)
// {z}           /. (   OpenStreetMaps)
// {q}              QuadKey. (   Bing)
// {-y}              Y. (   Nakarte)
// {bbox}         . ( WMS )
// {timeStamp}      UnixTime. (   )
// {z+1}         . ( ,    SasPlanet)
// {x/1024}        ( ,   SasPlanet)
// {y/1024}        ( ,   SasPlanet)
// {s:a,b,c}	      . 
//                 -    (A,B,C),  .



//   :      , 
//       .

//   :     , 
//     ( ,   Debug Output )  :
//
// writeLn('Hello Sas.Planet!');








// -----------------------------------------------------------------------------
// --- 4.  
// -----------------------------------------------------------------------------

// --- ,     ?
function isContains(findingText: string; inSourceText: string) : boolean;
begin
	result := pos(findingText, inSourceText) <> 0;
end;


// ---       
// --- (     -  )
function roundFor(sourceNumber: Double  ; digitAfterComma: integer) : string;
var
	intPart, floatPart : integer;
begin    
	intPart := floor(sourceNumber);
	floatPart := floor( (sourceNumber - intPart) * round(intPower(10, digitAfterComma)) )
	result := intToStr(intPart) + '.' + intToStr(floatPart)
end;


// ---      Quadkey (   Bing)
function getQuadkeyText(x: integer; y: integer; z: integer) : string;
var
	i, q : byte;
begin
	result:='';
	for i:=1 to z do begin
		q:=0;
		if x mod 2 = 1 then q := q + 1;
		if y mod 2 = 1 then q := q + 2;
		x := x div 2;
		y := y div 2;
		result := intToStr(q) + result;
	end;
end;




// -----------------------------------------------------------------------------
// --- 3.  ,       
// ---     {s: a,b,c}
// -----------------------------------------------------------------------------
type
	TSubst = record
		mask, val : string;
	end;
	TSubsts = record
		count : integer;
		s : array [0..15] of TSubst;
	end;


function replaceServerName(url: string) : string;
var
	s, ss : string;
	sarr : array [0..9] of string;
	sarr_l, p : integer;

begin
	s := RegExprGetMatchSubStr(url, '\{[sS]:([^}]+)\}', 0);
	if s <> '' then begin
		ss := s;
		ss := StringReplace(ss, '{s:', '', [rfIgnoreCase]);
		ss := StringReplace(ss, '}', '', [rfIgnoreCase]);
		ss := ss + ',';
		sarr_l := 0;
		while ss <> '' do begin
			p := pos(',', ss);
			if p = 0 then p := length(ss);
			sarr[sarr_l] := copy(ss, 1, p-1);
			sarr_l := sarr_l + 1;
			delete(ss, 1, p);
		end;
		url := StringReplace(url, s, sarr[random(sarr_l)], []);
	end;
	Result := url;
end;




// -----------------------------------------------------------------------------
// --- 2.  ,       URL  
// ---       {x}, {y}, {z}.
// -----------------------------------------------------------------------------
function replaceLeafletPlaceholders(urlTemplate: string; x: integer; y: integer; z: integer) : string;
var
	options: tReplaceFlags;
	calculatedValue: string;

begin
	options := [rfReplaceAll, rfIgnoreCase];
	result := urlTemplate;

	if isContains('{x}', result) then begin
		calculatedValue := intToStr(x);
		result := stringReplace( result, '{x}', calculatedValue, options)
	end;

	if isContains('{y}', result) then begin
		calculatedValue := intToStr(y);
		result := stringReplace( result, '{y}', calculatedValue, options)
	end;

	if isContains('{z}', result) then begin
		calculatedValue := intToStr(z-1);
		result := stringReplace( result, '{z}', calculatedValue, options)
	end;

	if isContains('{z+1}', result) then begin
		calculatedValue := intToStr(z);
		result := stringReplace( result, '{z+1}', calculatedValue, options)
	end;

	if isContains('{x/1024}', result) then begin
		calculatedValue := intToStr(x div 1024);
		result := stringReplace( result, '{x/1024}', calculatedValue, options)
	end;

	if isContains('{y/1024}', result) then begin
		calculatedValue := intToStr(y div 1024);
		result := stringReplace( result, '{y/1024}', calculatedValue, options)
	end;

	if isContains('{-y}', result) then begin
		calculatedValue := intToStr( round(intPower(2, z-1)) - 1 - y);
		result := stringReplace( result, '{-y}', calculatedValue, options)
	end;

	if isContains('{q}', result) then begin
		calculatedValue := getQuadkeyText(x, y, z);
		result := stringReplace( result, '{q}', calculatedValue, options)
	end;

	if isContains('{bbox}', result) then begin
        result := stringReplace( result, '{bbox}', '{Left},{Bottom},{Right},{Top}', options)
        result := stringReplace( result, '{Left}', roundFor(GetLMetr,8), options)
        result := stringReplace( result, '{Bottom}', roundFor(GetBMetr,8), options)
        result := stringReplace( result, '{Right}', roundFor(GetRMetr,8), options)
        result := stringReplace( result, '{Top}', roundFor(GetTMetr,8), options)
        //      ,    
        //      . 
        //  -   .
	end;

	if isContains('{timeStamp}', result) then begin
		calculatedValue := IntToStr(GetUnixTime);
		result := stringReplace( result, '{timeStamp}', calculatedValue, options)
	end;
end;




// -----------------------------------------------------------------------------
// --- 1.  .   URL- .
// ---     URL.
// -----------------------------------------------------------------------------
begin
	resultURL := replaceLeafletPlaceholders(getURLBase, getX, getY, getZ);
	resultURL := replaceServerName(resultURL);	
end.
