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

$helper     = InputHelper::getInstance();
$fname      = $helper->fixInput($_GET['name'], FALSE);

if (!isset($fname))
    exit();

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

$user = $userdb->lookupUserInformation($fname, $trans->getPDO());
if ($user) {
    $profile    = $userdb->getProfile($user->getUserId(), $trans->getPDO());
    $details    = $userdb->getProfileDetails($user->getUserId(), $trans->getPDO());
    $pictures   = $userdb->getPictures($user->getUserId(), $trans->getPDO(), $tinyPicsOnProfile);
    $tags       = $userdb->getUserInterestTags($user->getUserId(), $trans->getPDO());
    $frienddb   = new FriendDB();
    $friends    = $frienddb->getFriends($user->getUserId(), $trans->getPDO());
}
else {
    $trans->commit();
    $trans->disconnect();
    exit();
}


session_start();
$myprofile = (isset($_SESSION['user']) && $_SESSION['user']->getUserId() == $user->getUserId());

if ($myprofile) {
    $commcount = $userdb->getCommentCount($user->getUserId(), $trans->getPDO());
    $tookcount = $quizdb->getTookQuizCount($user->getUserId(), $trans->getPDO());
}
$quizKey = $quizdb->hasActiveQuiz($user->getUserId(), $trans->getPDO());

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


if (!$user) {
    echo 'No such user.';
    exit();
}

$picture = $user->getPicture();
$ui = Component::getInstance();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Profile For <?= $user->getName() ?></title>
<link type="text/css" rel="stylesheet" href="/css/main.css">
<link type="text/css" rel="stylesheet" href="/css/profile.css">
</head>
<body>
<div id="body">

<h1>Profile For <?= $user->getName() ?></h1>

  <div id="rcol">
<?php
if ($profile) {
?>
    <div id="top1">
      <h2><?= $user->getName() ?>'s Profile Details</h2>
    </div>
    <table id="profile" colspan="2">
      <tr>
        <td class="lab">Gender:</td>
        <td class="ans"><?= ($user->getGender() == 'M' ? 'Man' : 'Woman') ?></td>
        <td class="lab">Age:</td>
        <td class="ans"><?= $user->getAge() ?></td>
      </tr>
      <tr>
        <td class="lab">Drinker:</td>
        <td class="ans"><?= $profile->getDrinker() ?></td>
        <td class="lab">Education:</td>
        <td class="ans"><?= $profile->getEducation() ?></td>
      </tr>
      <tr>
        <td class="lab">Smoker:</td>
        <td class="ans"><?= $profile->getSmoker() ?></td>
        <td class="lab">Income:</td>
        <td class="ans"><?= $profile->getIncome() ?></td>
      </tr>
      <tr>
        <td class="lab">Status:</td>
        <td class="ans"><?= $profile->getMaritalStatus() ?></td>
        <td class="lab">Religion:</td>
        <td class="ans"><?= $profile->getReligion() ?></td>
      </tr>
      <tr>
        <td class="lab">Orientation:</td>
        <td class="ans"><?= $profile->getOrientation() ?></td>
        <td class="lab">Ethnicity:</td>
        <td class="ans"><?= $profile->getRace() ?></td>
      </tr>
      <tr>
        <td class="lab">Kids?</td>
        <td class="ans" colspan="3">
<?php
if ($profile->getHasKids() == 'T') {
    if ($profile->getWantsKids() == 'T')
        echo 'Has kids and is interested in more';
    else if ($profile->getWantsKids() == 'F')
        echo 'Has kids and does not want more';
    else
        echo 'Has kids, declines to specify about more';
}
else if ($profile->getWantsKids() == 'F') {
    if ($profile->getWantsKids() == 'T')
        echo 'No kids but wants them some day';
    else if ($profile->getWantsKids() == 'F')
        echo 'No kids, does not want';
    else
        echo 'No kids, declines to specify about more';
}
else {
    if ($profile->getWantsKids() == 'T')
        echo 'Declines to specify, but wants kids';
    else if ($profile->getWantsKids() == 'F')
        echo 'Declines ot specify, but does not want';
    else
        echo 'Decline to specify';
}
?>
        </td>
      </tr>
<?php
$loc = $user->getLocation();
if ($loc) {
?>
      <tr>
        <td class="lab">Location:</td>
        <td class="ans" colspan="3"><?= $loc->getCity() ?>, <?= $loc->getState() ?>, <?= $loc->getCountry() ?></td>
      </tr>
<?php
}
?>    
      <tr>
        <td class="lab">Why here?</td>
        <td class="ans" colspan="3"><?= $profile->getRelationship() ?></td>
      </tr>
    </table>
<?php
}
?>
  <div id="linx">
    <div id="linx2">
      <div id="linx3">
<?php
if ($myprofile) {
    if ($quizKey)
        echo '<a href="/quiz/viewQuizResults.html">'. $tookcount .' '. ($tookcount == 1 ? 'person has' : 'people have') .' taken your quiz</a><br>';
    else
        echo '<a href="/quiz/create1.html">Create Your Compatability Quiz</a><br>';
}
else if ($quizKey)
    echo '<a href="/quiz/'. $quizKey .'.html">Take '. $user->getName() .'\'s Compatability Quiz</a><br>';

if (!$myprofile)
    echo '<a href="/comment/send.html?n='. $fname ."\">Send ". $user->getName() ." a Private Message</a><br>\r\n";
else {
    if (!$commcount)
        echo 'You Have No Private Messages';
    else {
        echo '<a href="/comment/view.html">Check Your ';
        echo $commcount[0] .' message'. ($commcount[0] == 1 ? '' : 's') .'</a>';
        if ($commcount[1] > 0)
            echo ' ('. $commcount[1] .' New)';
    }
    echo '<br><a href="/account/uprofile1.html">Modify Your Profile</a>';
    echo '<br><a href="/account/usettings.html">Adjust Account Settings</a>';
}
?>
      </div>
    </div>
  </div>

  </div>

  <div id="lcol">
<?php

if (isset($picture))
    echo '<img src="/pic/'. $picture->getDirName() . $picture->getFileName() .'" id="selfpic"><br>';
else
    echo '<img src="/i/nopic.gif" id="selfpic"><br>';

if ($pictures && (count($pictures) > 1 || !isset($picture))) {
    echo '<table><tr>';
    for ($i = 0, $n = count($pictures); $i < $n; ++$i) {
        if (!isset($picture) || $pictures[$i]->getPictureId() != $picture->getPictureId())
            echo '<td><a href="/pic/'. $pictures[$i]->getDirName() . $pictures[$i]->getFilename() .'"><img src="/pic/'. $pictures[$i]->getDirName() . $pictures[$i]->getTinyName() .'" border="0"></a></td>';
    }
    echo '</tr></table>';
}
if ($myprofile)
    echo '<div id="piclink"><a href="/pictures/'. $user->getFName() .'">Manage Pictures</a></div>';
else if (count($pictures) == $tinyPicsOnProfile)
    echo '<div id="piclink"><a href="/pictures/'. $user->getFName() .'">View All Pictures</a></div>';

if ($myprofile) {
?>
<form action="/account/uploadpic.html" method="post" enctype="multipart/form-data">
  Add a pic: <input type="file" name="mypic" size="30">
  <input type="submit" value="Upload">
</form>
<?php
}
?>
  </div>

<?php
if ($details) {
?>
  <div id="top2">
    <h2><?= $user->getName() ?>'s Description</h2>
  </div>
  <table cellspacing="2" id="details">
    <tr>
      <td><p><?= $details->getDescription() ?></p></td>
    </tr>
    <tr>
      <th><?= $user->getName() ?>'s Interest Tags:</th>
    </tr>
    <tr>
      <td>
<?php
    if (count($tags) == 0)
        echo '<i>'. $user->getName() .' has not listed any interest tags</i>';
    else {
        for ($i = 0, $n = count($tags); $i < $n; ++$i)
            echo ($i != 0 ? ', ' : '') . $tags[$i];
    }
?>        
      </td>
    </tr>
    <tr>
      <th><?= $user->getName() ?>'s Friends:</th>
    </tr>
    <tr>
      <td>
<?php
    if (count($friends) == 0)
        echo '<i>'. $user->getName() .' has not yet added any friends</i>';
    else {
        echo '<ul id="friends">';
        for ($i = 0, $n = count($friends); $i < $n; ++$i) {
            if ($friends[$i]->hasPicture()) {
                $fpic = $friends[$i]->getPicture();
                echo '<li><a href="/profile/'. $friends[$i]->getFName() .'"><img src="/pic/'. $fpic->getDirName() . $fpic->getThumbName() .'" alt=""><br>'. $friends[$i]->getName() .'</a></li>';
            }
            else
                echo '<li><a href="/profile/'. $friends[$i]->getFName() .'"><img src="/i/nopicsm.gif" alt=""><br>'. $friends[$i]->getName() .'</a></li>';
        }
        echo '</ul>';
    }
    if (count($friends))
        echo '<div id="frlink"><a href="/friends/'. $fname .'">'. ($myprofile ? 'Manage Friends' : 'View All Friends') .'</a></div>';
?>
      </td>
    </tr>
  </table>
<?php
}
$ui->drawHeader($_SESSION['user']);
$ui->drawFooter();
?>

</div>

</body>
</html>
