<?php

header('Content-type: text/html; charset=ISO-8859-1');

$params = array();
if(count($_GET) > 0) {
    $params = $_GET;
} else {
    $params = $_POST;
}

// We're doing a simple hash so that the user doesn't know how to change the points easily
if($params['p'] != "") {

  $xorConstant = 0x53;
  $data = array();
  $params['p'] = base64_decode($params['p']);
  $data = unpack("C*", $params['p']);

  for ($i = 0; $i < count($data); $i++)
  {
      $data[$i] = $data[$i] ^ $xorConstant;
  }
  $data = mb_convert_encoding($data, "UTF-8", "ASCII");
  $plainText = call_user_func_array("pack", array_merge(array("C*"), $data));

  // Conversion makes an extra S (with xor constant = 0x53) appear in the end of the string
  // The following code removes the extra character
  if (strrpos($plainText,"S") == (strlen($plainText) - 1)) {
    $plainText = trim($plainText, "S");
  }

  // The same thing that is used to populate $_GET and $_POST
  parse_str($plainText, $params);

  // if ($pts)
  //   $params['pts'] = $pts;

  // if ($pl)
  //   $params['pl'] = $pl;

  // if ($ct)
  //   $params['ct'] = $ct;
}

// Set default values for params
if($params['pts'] == "") $params['pts'] = "mais de 0.88 mil";
if($params['pl'] == "") $params['pl'] = "lay";
if($params['ct'] == "") $params['ct'] = "Brasilia";

$gameTitle = "Fiz " . $params['pts'] . " pontos no Só Para Constar!";
$teamPlayerHashtag = "#TimeLayla88";
if ($params['pl'] == "mig") $teamPlayerHashtag = "#TimeMiguel88";
$description = $gameTitle . " Bata seu recorde e acesse: brasil.gov.br/30anosdeconstituicao #SoParaConstar " . $teamPlayerHashtag . " #30anosCF88";

$gameLinkText = 'Jogar o Só Para Constar';

// TODO: Change Link
$imageLink = "http://www.brasil.gov.br/30anosdeconstituicao/imagens/";

$bsbImgs = array("bsb_hea","bsb","bsb_lib","bsb_wrk");
$mauImgs = array("mau_dem","mau_edu","mau");
$rioImgs = array("rio_hea","rio_lib");
$salImgs = array("sal_edu","sal","sal_wrk");
$spImgs = array("sp_dem","sp_edu","sp");

$imgs = array();

switch ($params['ct']) {
    case "Brasilia":
        $imgs = $bsbImgs;
        break;
    case "Manaus":
        $imgs = $mauImgs;
        break;
    case "Rio":
        $imgs = $rioImgs;
        break;
    case "Salvador":
        $imgs = $salImgs;
        break;
    case "SaoPaulo":
        $imgs = $spImgs;
        break;
    default:
        $imgs = $bsbImgs;
}

$randomImage = $imgs[rand(0, count($imgs)-1 )];

$imageLink = $imageLink . $randomImage . "_" . $params['pl'] .  ".png";

$gameLink = "http://www.brasil.gov.br/30anosdeconstituicao";

?>


<html lang="pt-BR">

<head>

<meta property="og:type"               content="game" />
<meta property="og:title"              content="<?php echo $gameTitle; ?>" />
<meta property="og:description"        content="<?php echo $description; ?>" />
<meta property="og:image"              content="<?php echo $imageLink; ?>" />
<meta property="fb:app_id"             content="473099186530662" />

<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />

  <!-- Compiled and minified CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">

  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>

</head>

<body>
    <div class="container">
      <div class="row">
            <div class="col s0 m0 l2"></div>
            <div class="col s12 m12 l8">
              <div class="card">
                <div class="card-image">
                  <a href="<?php echo $gameLink; ?>"><img src="<?php echo $imageLink; ?>"></a> 
                </div>
                <div class="card-content">
                  <p><?php echo $description; ?></p>
                </div>
                <div class="card-action">
                  <a href="<?php echo $gameLink; ?>"><?php echo $gameLinkText; ?></a>
                </div>
              </div>
            </div>
        </div>
    </div>

</body>

</html>