<?php

namespace iDServer\Internal;

class Redirector
{
    /**
     * @var string|null
     */
    private $proxyRoot;

    /**
     * @param string|null $proxyRoot
     */
    public function __construct(string $proxyRoot = null)
    {
        $this->proxyRoot = $proxyRoot;
    }

    /**
     * Do the redirection
     *
     * @param string $destination
     * @param string[] $payload Data to post to the destination
     */
    public function post(string $destination, array $payload)
    {
        ?>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
    <title>Redirecting</title>
    <link rel='stylesheet' href='<?= $this->proxyRoot ?>/static/css/boilerplate.css'/>
    <link rel='stylesheet' href='<?= $this->proxyRoot ?>/static/css/IDServerSSO.css'/>
    <meta name='viewport' content='initial-scale = 1.0,maximum-scale = 1.0'/>
    <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,200italic,300italic,400italic,600italic&subset=latin,latin-ext'
          rel='stylesheet' type='text/css'>
    <!--[if lt IE 9]>
    <script src='<?= $this->proxyRoot ?>/static/js/html5shiv-printshiv.js'></script>
    <![endif]-->
    <script src='<?= $this->proxyRoot ?>/static/js/jquery.min.js'></script>
</head>
<body onload='document.getElementById("redirectForm").hidden = true;document.getElementById("redirectForm").submit();'>
<div id='header'>
    <h2 id='oldalCim'><img id='oldalCimImg' src='<?= $this->proxyRoot ?>/static/images/idx-logo-small.png'
                           alt=' title='/></h2>
    <img id='CompanyImg' src='<?= $this->proxyRoot ?>/static/images/ikon_egroup.png' alt=' title='/>
</div>
<p class='accessibility'><a id='pageContent'>Content</a></p>

<div id='contentBox'>
    <div id='contentBoxBG2'>
        <h3>Please wait</h3>
        <p>Redirect in progress...</p>
    </div>

    <div>
        <form id='redirectForm' method='post' action='<?= $destination ?>'>
            <fieldset>
                <?php foreach ($payload as $k => $v) { ?>
                    <input type='hidden' name='<?= $k ?>' value='<?= $v ?>' />
                <?php } ?>
                <input type='submit' id='frmRegButton' value='Click this button to continue'/>
            </fieldset>
        </form>
    </div>
    <p id='footerText'>Copyright &copy; 1993 - 2016, E-Group</p>
    <ul id='footer'>
        <li><a href='http://www.egroup.hu' title='www.egroup.hu'>www.egroup.hu</a></li>
    </ul>
</div>
<script src='<?= $this->proxyRoot ?>/static/js/jquery-control.js'></script>
</body>
</html>
        <?php
    }
}
