<?php

function contains($string, $char) {
    return strpos($string, $char) != false;
}
function expandDecimals($val) {
    if (!contains($val, ".")) {
        return $val;
    }
    $parts = explode('.', $val);
    if (count($parts) == 2) {
        if (strlen($parts[1]) > 0) {
            if (strlen($parts[1]) == 1) {
                $part = $parts[0] . "." . $parts[1] . "0";
            } else {
                $part = $parts[0] . "." . $parts[1];
            }
        }
    } else {
        $part = $parts[0] . ".00";
    }
    return $part;
}

function getArrayFor($currencyID) {
    $ch = curl_init("https://api.coinbase.com/v2/prices/ETH-$currencyID/spot");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    $jsonArray = json_decode($data, true);
    if (isset($jsonArray['data'])) {
        return $jsonArray['data'];
    } else {
        return $jsonArray;
    }
}

    require_once $_SERVER['DOCUMENT_ROOT'] . "/Base.php";
    require_once $_SERVER['DOCUMENT_ROOT'] . $PREFIX_DIR . "/start.php";
?>

    </head>
    <body>
        <?php
        include_once $DOCROOT . "$PREFIX_DIR/header_start.php";
        include_once $DOCROOT . "$PREFIX_DIR/menu/userMenu.php";
        include_once $DOCROOT . "$PREFIX_DIR/header_finish.php";
        ?>
        <?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);

        GLOBAL $currency;
        if (!empty($_REQUEST)) {
            $currency = $_REQUEST['currency'];
        }
        if (empty($currency))
            $currency = "CAD";

        $array = getArrayFor($currency);
        $amount = $array['amount'];
        if ($amount > 0) $one = 1 / $amount;
        else $one = 0.00;

        $ch = curl_init("https://api.coinbase.com/v2/currencies");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        $jsonArray = json_decode($data, true);
        $currencies = $jsonArray['data'];
        ?>
        <div class="container">
            <style>
                table, thead {
                    border: 1px solid black;
                    border-collapse: collapse;
                }

                thead, th {
                    text-align: center;
                }
            </style>
            <br />
            <h4>Data acquired on page load or conversion currency change from <a href="https://www.coinbase.com/">coinbase.com</a>.</h4>
            <h6>The code for this page is <a href="ETHPriceCode.php">here</a>.</h6>
            <br />
            <table class="table table-bordered table-striped table-hover">
                <thead>
                    <tr>
                        <th colspan="2">Conversions</th>
                        <th colspan="2">
                            <label for="currency">Currency:</label>
                            <script>
                                function doReload() {
                                    window.location = "<?php echo $_SERVER['PHP_SELF']; ?>?currency=" + document.getElementById('currency').value;
                                }
                            </script>
                            <select id="currency" onchange="doReload();">
                                <?php
                                print("<option value='" . $currency . "'>" . $currency . "</option>\n");
                                for ($i = 0; $i < count($currencies); $i++) {
                                    if ($currencies[$i]['id'] != $currency) {
                                        print("<option value='" . $currencies[$i]['id'] . "'>" . $currencies[$i]['id'] . "</option>\n");
                                    }
                                }
                                ?>
                            </select>
                        </th>
                    </tr>
                    <tr>
                        <th>Amount</th>
                        <th>Currency</th>
                        <th colspan="4">Result</th>
                    </tr>
                </thead>
                <tbody>
                    <tr><td align="right">$50</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 50; ?></td></tr>
                    <tr><td align="right">$200</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 200; ?></td></tr>
                    <tr><td align="right">$3000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 3000; ?></td></tr>
                    <tr><td align="right">$1</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one; ?></td></tr>
                    <tr><td align="right">$10</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 10; ?></td></tr>
                    <tr><td align="right">$100</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 100; ?></td></tr>
                    <tr><td align="right">$1,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 1000; ?></td></tr>
                    <tr><td align="right">$10,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 10000; ?></td></tr>
                    <tr><td align="right">$100,000</td><td><?php echo $currency; ?></td><td align="right"><?php echo $one * 100000; ?></td></tr>

                </tbody>
            </table>
            <br />

            <table class="table table-bordered table-striped table-hover">
                <thead>
                    <tr style="border: 1px solid black;"><th colspan="5"><strong>1 ETH</strong></th></tr>
                    <tr>
                        <th>Currency</th>
                        <th>Name</th>
                        <th>min_size</th>
                        <th>Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    for ($i = 0; $i < count($currencies); $i++) {
                        print("<tr>\n");
                        print("  <td>" . $currencies[$i]['id'] . "</td>\n");
                        print("  <td align='left'>" . $currencies[$i]['name'] . "</td>\n");
                        print("  <td align='right'>" . $currencies[$i]['min_size'] . "</td>\n");
                        print("  <td align='right'>");
                        $array = getArrayFor($currencies[$i]['id']);
                        $j = 0;
                        if (isset($array['errors'])) {
                            print("<font style='color: red'>");
                            for(;$j < count($array) - 1;$j++) {
                                $errArray = $array['errors'][$j];
                                print($errArray['message']);
                                print("<br />");
                            }
                            $errArray = $array['errors'][$j];
                            print($errArray['message']);
                            print("</font>");
                        } else {
                            print(expandDecimals($array['amount']));
                        }
                        print("</td>\n");
                        print("</tr>\n");
                    }

                    ?>
            </tbody>
        </table>
        <br />
    </div>
</body>
</html>