<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use Codenixsv\CoinGeckoApi\CoinGeckoClient;

require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../vendor1/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/PaymentWallet.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../models/Invoice.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/News.php';
require_once '../include/Helper.php';

$db = new DatabaseConn();
$link = $db->connect();

$user = User::getCurrentUser($link);

if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}




if (isset($_POST['message'])) {
    $name = $user->getName();
    $message = filter_input(INPUT_POST, "message", FILTER_SANITIZE_STRING);
    $subject = filter_input(INPUT_POST, "subject", FILTER_SANITIZE_STRING);
    $email = $user->getEmail();

    if (is_null($subject) || is_bool($subject)) {
        $error = "Please provide subject";
    }
    if (is_null($name) || is_bool($name)) {
        $error = "Please provide a name";
    }

    if (is_null($message) || is_bool($message)) {
        $error = "Message body is empty";
    }
    if (!isset($error)) {
        $mail = new PHPMailer(true);
        try {
            $text = "<p>Name: $name</p><br/><p>Email: $email</p><br/><p>Subject: $subject</p><br/><p>Message: $message</p><br/>";
            $mail->isSMTP();
            $mail->SMTPDebug = 0;
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Debugoutput = 'html';
            $mail->Host = EMAIL_HOST;

            $mail->Port = 587;
            $mail->SMTPSecure = 'tls';
            $mail->SMTPAuth = true;
            $mail->Username = EMAIL_USERNAME;
            $mail->Password = EMAIL_PASSWORD;
            $mail->setFrom(EMAIL_NO_REPLY, APP_NAME);
            $mail->addAddress(EMAIL_SUPPORT);
            $mail->Subject = $subject;
            $mail->isHTML(true);
            $mail->msgHTML($text);
            //This should be the same as the domain of your From address
            $mail->DKIM_domain = DKIM_DOMAIN;
            $mail->DKIM_private = 'dkim_private.pem';
            $mail->DKIM_selector = DKIM_SELECTOR;
            $mail->DKIM_identity = $mail->From;
            $mail->DKIM_copyHeaderFields = false;

            $mail->send();

            $success = 'Message has been sent We would contact you soon.';
            //    return true;
        } catch (Exception $e) {
            var_dump($e);
            die();
            $error = "Message could not be sent.";
        }
    }
}

$news = News::getNewsPage($link);
$cInvoices = Invoice::getCurrentInvoices($link, $user->getEmail());
$client = new CoinGeckoClient();
?>

<!DOctype html>
<html class="gr__">
    <head>
        <?php require_once './partials/head.php'; ?>
    </head>
    <body cz-shortcut-listen="true" data-gr-c-s-loaded="true">
        <div id="app">
            <div class="sc-eIHaNI jXnkjm" style="">
                <div class="sc-bsbRJL jBomjO">
                    <span></span>
                </div>
                <div></div>
                <?php require_once './partials/navbar.php'; ?>
                <div class="sc-eTpRJs chWNvr">
                    <div data-e2e="page-home" class="sc-iomxrj lkDWON">
                        <div class="sc-ugnQR ikKNan">
                            <div class="sc-dDPtig fBozlL">
                                <?php require_once './partials/sidebar.php'; ?>
                                <section class="sc-jAarDj jmkFbA">
                                    <div class="sc-fNbuCz sc-bqsUL dgojan">
                                        <div class="sc-dArhMf fOVmDi">
                                            <div class="sc-ipqSdY cNEiUB">
                                                <button onclick="window.location.href = 'send'" style="background-color: #3375BB; border-color: #3375BB;" data-e2e="buyButton" height="48px" class="sc-bdVaJa iDvQMX sc-kIfXyL kJCnXG" color="white" type="button">
                                                    <span>Send</span>
                                                </button>
                                                <button onclick="window.location.href = 'receive'" style="background-color: #3375BB; border-color: #3375BB;" data-e2e="swapButton" height="48px" class="sc-bdVaJa iDvQMX sc-kIfXyL kJCnXG" color="white" type="button">
                                                    <span>Receive</span>
                                                </button>
                                            </div>
                                            <div class="sc-iEEPnt dafKUP">
                                                <div color="grey400" cursor="inherit" opacity="1" class="sc-gzVnrw hOOOSa">
                                                    <span>Wallet Balance</span>
                                                </div>
                                                <?php
                                                $wallets = PaymentWallet::getAllPaymentsWalletsName($link);
                                                $a = array();
                                                $i = 0;
                                                foreach ($wallets as $wallet) {
                                                    $address = PaymentWallet::getAllPaymentsWalletsAddress($link, $wallet->getName());
                                                    $d = $address[array_rand($address)];
                                                    $wallet->setAddress($d->getAddress());

                                                    $token = $wallet->getCoinId();

                                                    $a[$i] = number_format((float)User::getWalletBalance($link, $user->getEmail(), $wallet->getCoinId()), 2, '.', '');
                                                    $i++;
                                                }
                                                ?>
                                                <div data-e2e="homeBalanceAmt" class="sc-gzVnrw fMnhKs sc-bnoQSP jnygCn" color="grey700" cursor="inherit" opacity="1">$<?php echo (array_sum($a)); ?> </div>
                                            </div>
                                            <div class="sc-hEsumM BCdqX sc-jYIdPM gLdSNN" style="background-color: transparent;">
                                                <span width="33%" data-e2e="totalTab" class="sc-ktHwxA iBBsrM">
                                                    <span>Portfolio</span>
                                                </span>
                                            </div>
                                            <div class="sc-bHwgHz EpALc">
                                                <?php
                                                $wallets = PaymentWallet::getAllPaymentsWalletsName($link);
                                                $a = array();
                                                $i = 0;
                                                foreach ($wallets as $wallet) {
                                                    $address = PaymentWallet::getAllPaymentsWalletsAddress($link, $wallet->getName());
                                                    $d = $address[array_rand($address)];
                                                    $wallet->setAddress($d->getAddress());
                                                    ?>
                                                    <div data-e2e="btcBalanceTable" class="sc-krDsej ePAgsD">
                                                        <div class="sc-lhyNDq kbbTHO" href="#/btc/transactions">
                                                            <div class="sc-jnREuW iMscfQ">
                                                                <div class="sc-fWVWpA gyBBFV">
                                                                    <?php
                                                                        if(strcmp($wallet->getSymbol(), "twt") == 0){?>
                                                                            <img class="rowpic" style="width: 32px" src="https://s2.coinmarketcap.com/static/img/coins/64x64/5964.png">
                                                                      <?php  } else if(strcmp($wallet->getSymbol(), "uni") == 0) { ?>
                                                                          <img class="rowpic" style="width: 32px" src="https://s2.coinmarketcap.com/static/img/exchanges/64x64/595.png">
                                                                     <?php }
                                                                      
                                                                      else {?>
                                                                           <img class="rowpic" src="https://cdn.jsdelivr.net/npm/cryptocurrency-icons@0.16.1/svg/color/<?php echo strtolower($wallet->getSymbol()); ?>.svg">
                                                                  <?php    } 
                                                                    ?>
                                                                   
                                                                    <div color="grey700" class="sc-gzVnrw fMnhKs sc-ggjMlp eRjTmf" cursor="inherit" opacity="1"><?php echo $wallet->getName(); ?> </div>
                                                                </div>
                                                                <div class="sc-gAjsMU hHeZet">
                                                                    <div class="sc-fONwsr ghUhJz">
                                                                        <div data-e2e="BTCFiatBalance" color="grey800" cursor="pointer" class="sc-gzVnrw ijKhZl sc-VJcYb dmeMyq" opacity="1">$<?php echo number_format((float)User::getWalletBalance($link, $user->getEmail(), $wallet->getCoinId()), 4, '.', ''); ?> </div>
                                                                    </div>
                                                                    <div data-e2e="BTCBalance" class="sc-fzsDOv hYivdx">
                                                                        <?php
                                                                        $token = $wallet->getCoinId();
                                                                         $btc = PaymentWallet::getTokenPrice($token,$client);
                                                                         $btc = User::getWalletBalance($link, $user->getEmail(), $wallet->getCoinId()) / $btc;
                                                                         $btc = number_format((float)$btc, 4, '.', '');
                                                                        ?>
                                                                        <div data-e2e="BTCBalance" color="grey400" cursor="pointer" class="sc-gzVnrw eOoiHX sc-gPWkxV kTdwvR" opacity="1"> <?php echo $btc . "" . $wallet->getSymbol(); ?> </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <?php
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="sc-fNbuCz sc-eRjRog jUkgna">
                                        <div class="sc-gEGaab dwfEmX dodo">
                                            <div style="height:560px; background-color: #FFFFFF; overflow:hidden; box-sizing: border-box; border: 1px solid #56667F; border-radius: 4px; text-align: right; line-height:14px; font-size: 12px; font-feature-settings: normal; text-size-adjust: 100%; box-shadow: inset 0 -20px 0 0 #56667F;padding:1px;padding: 0px; margin: 0px; width: 100%;"><div style="height:540px; padding:0px; margin:0px; width: 100%;"><iframe src="https://widget.coinlib.io/widget?type=chart&theme=light&coin_id=859&pref_coin_id=1505" width="100%" height="536px" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0" border="0" style="border:0;margin:0;padding:0;line-height:14px;"></iframe></div><div style="color: #FFFFFF; line-height: 14px; font-weight: 400; font-size: 11px; box-sizing: border-box; padding: 2px 6px; width: 100%; font-family: Verdana, Tahoma, Arial, sans-serif;"></div></div>
                                        </div>
                                    </div>
                                </section>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php require_once './partials/scripts.php'; ?>
    </body>
</html>