<?php
require('../setClassPath.php');
require_once('object/User.php');
require_once('object/Picture.php');
require_once('db/UserDB.php');
require_once('db/TransManager.php');
require_once('ui/Component.php');
require_once('util/Constants.php');

session_start();
$user = $_SESSION['user'];
if (!isset($user))
    exit();
$page = intval($_GET['page']);
if ($page < 1)
    $page = 1;

$convId = intval($_GET['conv_id']);

$trans = TransManager::getInstance();
$trans->beginTrans();
$userdb = new UserDB();

$comments = $userdb->getConversation($user->getUserId(), $convId, $trans->getPDO(), $page, $commentsPerPage);
$otherUser = $userdb->lookupUserInformationById($convId, $trans->getPDO());

$trans->commit();
$trans->disconnect();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>View Your Conversation With <?= $otherUser->getName() ?></title>
<link type="text/css" rel="stylesheet" href="/css/main.css">
<link type="text/css" rel="stylesheet" href="/css/comment.css">
</head>
<body>
<div id="body">
<h1>View Your Conversation With <?= $otherUser->getName() ?></h1>

<?php
if (!$comments) {
    echo 'You have no messages together.';
}
else {
?>
  <div style="text-align: center;">
      (Beware of possible scam-artists)
  </div>
  <div id="top">
    <h2>View Your Conversation</h2>
  </div>
  <table id="comments" cellspacing="0">
<?php
    for ($i = 0, $n = count($comments); $i < $n; ++$i) {
        $comm = $comments[$i];
        $selfComment = ($comm->getFromUserId() == $user->getUserId());
        echo '<tr><td class="c1" rowspan="'. ($selfComment ? '2' : '3') .'">';
        if (!$selfComment) {
            if ($comm->hasPicture()) {
                $picture = $comm->getPicture();
                echo '<a href="/pic/'. $picture->getDirName() . $picture->getFileName() .'"><img src="/pic/'. $picture->getDirName() . $picture->getThumbName() .'" border="0"></a><br>';
            }
            else
                echo '<img src="/i/nopicsm.gif" alt=""><br>';
        }
        else {
            if ($user->hasPicture()) {
                $picture = $user->getPicture();
                echo '<a href="/pic/'. $picture->getDirName() . $picture->getFileName() .'"><img src="/pic/'. $picture->getDirName() . $picture->getThumbName() .'" border="0"></a><br>';
            }
            else
                echo '<img src="/i/nopicsm.gif" alt=""><br>';
        }
        if ($selfComment)
            echo $user->getName();
        else
            echo '<a href="/profile/'. $comm->getFromFName() .'">'. $comm->getFromName() .'</a>';
        echo '</td><td class="date">'. $comm->getSentOn() .'</td></tr>';
        echo '<tr><td class="c2">'. $comm->getMessage();
        if ($selfComment && $comm->getFlag() == 'N')
            echo '<div class="unread">(Not yet read)</div>';
        echo '</td></tr>';
        if (!$selfComment)
            echo '<tr><td class="linx"><a href="send.html?n='. $comm->getFromFName() .'&amp;c='. $comm->getCommentId() .'" class="reply">reply to '. $comm->getFromName() .'</a></td></tr>';
    }
}

echo '</table><div id="pages">';
if ($page > 1)
    echo '<a href="view.html?page='. ($page - 1) .'">Page '. ($page - 1) .'</a>';
if (count($comments) == $commentsPerPage)
    echo ($page > 1 ? ' | ' : '') .'<a href="view.html?page='. ($page + 1) .'">Page '. ($page + 1) .'</a>';
echo '</div>';

$ui = Component::getInstance();
$ui->drawHeader($user);
$ui->drawFooter();
?>
</div>
</body>
</html>
