<?php
include("serverConnect.php");
include("functions.php");

session_start();

if (count(getTerrainsList()) - 1 < $_SESSION['mapSvg']) {
    header("Location:endMapSvg.php");
    exit;
}
else {
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Map svg</title>
    <script src="clipper/clipper.js"></script>
  </head>
  <body>
    <div id="svgcontainer"></div>
    <script>
    
var cpr = new ClipperLib.Clipper();
var scale = 10000;

<?
$width = getHexaWidth();
$height = getHexaHeight();

if ($_SESSION['mapSvg'] == 0) { //region contour
    for ($i = 0; $i < getRegionWidth(); $i++) {
        for ($j = 0; $j < getRegionHeight(); $j++) {
            
            $baseX = $i * 3*$width/4;
            $baseY = $j * $height;
            
            if ($i % 2 == 1) {
                $baseY += $height / 2;
            }
                        
            ?>
            var hexaPath = [[{X:<? echo $baseX; ?>,Y:<? echo $baseY + $height / 2; ?>},
                               {X:<? echo $baseX + $width/4; ?>,Y:<? echo $baseY; ?>},
                               {X:<? echo $baseX + 3*$width/4; ?>,Y:<? echo $baseY; ?>},
                               {X:<? echo $baseX + $width; ?>,Y:<? echo $baseY + $height / 2; ?>},
                               {X:<? echo $baseX + 3*$width/4; ?>,Y:<? echo $baseY + $height; ?>},
                               {X:<? echo $baseX + $width/4; ?>,Y:<? echo $baseY + $height; ?>}]];
                               
            ClipperLib.JS.ScaleUpPaths(hexaPath, scale);
            cpr.AddPaths(hexaPath, ClipperLib.PolyType.ptSubject, true);
            <?
        }
    }
}
else {
    $req = $bdd->prepare('SELECT * FROM cTerritories WHERE type = ?');
    $req->execute(array($_SESSION['mapSvg']));
    while ($data = $req->fetch()) {
        $baseX = getTerritoryX($data['id']) * 3*$width/4;
        $baseY = getTerritoryY($data['id']) * $height;
        
        if (getTerritoryX($data['id']) % 2 == 1) {
            $baseY += $height / 2;
        }
                    
        ?>
        var hexaPath = [[{X:<? echo $baseX; ?>,Y:<? echo $baseY + $height / 2; ?>},
                           {X:<? echo $baseX + $width/4; ?>,Y:<? echo $baseY; ?>},
                           {X:<? echo $baseX + 3*$width/4; ?>,Y:<? echo $baseY; ?>},
                           {X:<? echo $baseX + $width; ?>,Y:<? echo $baseY + $height / 2; ?>},
                           {X:<? echo $baseX + 3*$width/4; ?>,Y:<? echo $baseY + $height; ?>},
                           {X:<? echo $baseX + $width/4; ?>,Y:<? echo $baseY + $height; ?>}]];
                           
        ClipperLib.JS.ScaleUpPaths(hexaPath, scale);
        cpr.AddPaths(hexaPath, ClipperLib.PolyType.ptSubject, true);
        <?
    }
}
?>

var solution_paths = new ClipperLib.Paths();
var succeeded = cpr.Execute(ClipperLib.ClipType.ctUnion, solution_paths, ClipperLib.PolyFillType.pftNonZero, ClipperLib.PolyFillType.pftNonZero);

<?
if ($_SESSION['mapSvg'] == 0) { //region contour
    ?>
    var strSvg = '<path id="regionContour" stroke="red" opacity="0" stroke-width="0" d="' + paths2string(solution_paths, scale) + '"/>';
    <?
}
else {
    ?>
    var strSvg = '<path id="path<? echo $_SESSION['mapSvg']; ?>" stroke="white" fill=<? echo getTerrainsList()[$_SESSION['mapSvg']]['color']; ?> stroke-width="1" d="' + paths2string(solution_paths, scale) + '"/>';
    <?
}
?>

// Scale down coordinates and draw ...
var svg = '<svg style="background-color:#dddddd" width="1600" height="1600">';
svg += strSvg;
svg += '</svg>';
document.getElementById("svgcontainer").innerHTML = svg;

post("addMapSvg.php", {txt: strSvg});

// Converts Paths to SVG path string
// and scales down the coordinates
function paths2string (paths, scale) {
  var svgpath = "", i, j;
  if (!scale) scale = 1;
  for(i = 0; i < paths.length; i++) {
    for(j = 0; j < paths[i].length; j++){
      if (!j) svgpath += "M";
      else svgpath += "L";
      svgpath += (paths[i][j].X / scale) + ", " + (paths[i][j].Y / scale);
    }
    svgpath += "Z";
  }
  if (svgpath=="") svgpath = "M0,0";
  return svgpath;
}

function post(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}
    </script>
  </body>
</html>
<?
}
?>