#!/usr/bin/perl -I/var/www/macrophile.com/lib

use Data::Dumper;
use Macro::Auth;
use Macro::Forum;
use Macro::Template;
use Macro::Util qw/safe_username/;
use strict;

my $auth = new Macro::Auth;
my $for  = new Macro::Forum;
my $tmpl = new Macro::Template;
my $cgi  = $auth->{cgi};
my $dbh  = $for->_dbh();

my @images = qw/13379 13412 13393 13419 13420 13421 13435 13471 13473 13409 13445 13465 13498
13430 13359 13510 13516 13361 13524 13546 13558 13494/;

my @stories = qw/13493 13528/;

my $message = "
<p>In celebration of two big industry conventions: Electronic Entertainment Expo and San 
Diego Comicon. In other words, you can take your pic: For next week's E3, you've got 
\"Macros in Videogames.\" For the week after's Comicon, you've got \"Superhero/Supervillain 
Macros.\"</p>

<p>Below are the entries based on the theme of the contest. Items are preferred to be thematic. (IE: 
Rogue into Grand Theft Auto, or Viggen in the Riddler's costume, etc.) Vote for your favorite!</p>

<p>The prizes:</p>

<ul><li>First prize, picked by the forum: \$50 credit at Gamestop.com
<li>Second prize, picked at random: \$25 credit at Gamestop.com
<li>Third prize, picked by Bennie and Duncan: \$25 credit at Gamestop.com</ul>

<p>(If you're a winner outside of the US, these prizes will change...we'll stomp 
that bridge if/when we get to it). </p>

<hr noshade />

<p><b><font size='+1'>We're tallying the votes and will post results tomorrow. (Sept 31)</font></b></p>

<!--.<p>Votes are anonymous and results will be announced one-week from now on Sept 29 at midnight GMT.</p>

<p><i>We're trying a new voting system here. If you run into trouble, please PM Bennie
with info on the errors you are getting.</i></p> -->
";

$message = "<p>And we have winners!</p>

<p>The theme was in celebration of two big industry conventions: Electronic Entertainment Expo and San 
Diego Comicon. In other words, you can take your pic: For next week's E3, you've got 
\"Macros in Videogames.\" For the week after's Comicon, you've got \"Superhero/Supervillain 
Macros.\"</p>

<p><b>1st prize</b>, with 16% of the total votes and winning a \$50 credit at Gamestop.com:</p>

<blockquote><table><tr valign=\"top\"><td>
<a href=\"http://forums.macrophile.com/viewtopic.php?t=13510\"><img border=0 width=109 height=150 src=\"http://www.macrophile.com/images/preview/demoyote.jpg\" /></a>
</td><td>
&quot;<a href=\"http://forums.macrophile.com/viewtopic.php?t=13510\">Demo-man is Overpowered.</a>&quot;<br />
By: <a href=\"http://users.macrophile.com/Tyrnn_Eaveranth\">Tyrnn Eaveranth</a>
</td></tr></table></blockquote>

<p><b>2nd prize</b>, selected randomly and winning a \$25 credit at Gamestop.com.</p>

<blockquote><table><tr valign=\"top\"><td>
<a href=\"http://forums.macrophile.com/viewtopic.php?t=13359\"><img border=0 width=108 height=148 src=\"http://www.macrophile.com/images/preview/box.jpg\" /></a>
</td><td>
&quot;<a href=\"http://forums.macrophile.com/viewtopic.php?t=13359\">I'm in a box</a>&quot;<br />
By: <a href=\"http://users.macrophile.com/Kit\">Kit</a>
</td></tr></table></blockquote>

<p><b>3rd prize</b>, selected by Bennie and Duncan and winning a \$25 credit at Gamestop.com.</p>

<blockquote><table><tr valign=\"top\"><td>
<a href=\"http://forums.macrophile.com/viewtopic.php?t=13493\"><img border=0 width=32 height=32 src=\"http://www.macrophile.com/images/icons/txt.gif\" /></a>
</td><td>
&quot;<a href=\"http://forums.macrophile.com/viewtopic.php?t=13493\">S.T.A.L.K.E.D.</a>&quot;<br />
By: <a href=\"http://users.macrophile.com/Hetzer\">Hetzer</a>
</td></tr></table>



</blockquote>

<p>Thanks to all participants! I know it's been a bumpy ride with this voting system being
put together and thanks for being patient with that. Look for a new contest soon!</p>

<p><b><a href=\"http://forums.macrophile.com/viewtopic.php?t=13934\">Discussion thread / Talk about it here</a></b></p>
";

my %contest;
my $voted = 0;


# Not registered, no vote:

if ( $auth->{userid} < 2 ) {
  $voted = 2;
}

# vote if not voted

if ( $cgi->param('vote') and $auth->{userid} > 1) {
  my $ret = &vote($cgi->param('vote'));
  $message .= $cgi->p($cgi->b("Your vote has been tallied!"));
}

# Build up the contest info

my $sth = $dbh->prepare('select user_id, vote from contest');
my $ret = $sth->execute();

my $votecount = 0;

while ( my $ref = $sth->fetchrow_arrayref ) {
  $contest{$ref->[1]}++;
  $voted = 1 if $ref->[0] == $auth->{userid};
  $votecount++;
}

$voted = -1; # Turn off voting

$message .= $cgi->p($cgi->b("Thank you for voting. Results will be posted at midnight GMT on Sept 29.")) if $voted == 1;
$message .= $cgi->p($cgi->b("Log into the forum section to vote.")) if $voted == 2;

#$message .= $cgi->p("$votecount votes have been cast.");

#$message .= "($auth->{userid})";

print $cgi->header,
      $cgi->start_html('Macro Admin - Main');

my @rows = ('row1', 'row2'); # Alternated row styles
my $class;

my @table;
my @row1;
my @row2;
my @row3;
my $count = 1;

my $body = "<table class=\"innertable\" cellspacing=\"0\" cellpadding=\"3\">\n\n";

push @table, '<tr><td class="'.$rows[0].'" colspan="4">'. $message .'</td></tr>';

# stories

push @table, '<tr><td class="innertablesubhead" colspan="4">Stories</td></tr>';

for my $topic ( sort @stories ) {
  my $tref = $for->topic($topic);
  my $uref = $for->user($tref->{topic_poster});

  my $vote;
  $vote .= $cgi->start_form
        . $cgi->hidden({-name=>'vote',-value=>$topic})
        . $cgi->submit({-label=>'Vote'})
        . $cgi->end_form unless $voted;

  my $image = $cgi->a({-href=>'http://forums.macrophile.com/viewtopic.php?t='.$topic},
                $cgi->img({-border=>0,-src=>'http://www.macrophile.com/images/icons/txt.gif'})
              );

  my $desc = 'Title: ' . $cgi->b($cgi->a({-href=>'http://forums.macrophile.com/viewtopic.php?t='.$topic},$tref->{topic_title})) . $cgi->br
           . 'By: ' . $cgi->a({-href=>'http://users.macrophile.com/'.safe_username($uref->{username})},$uref->{username});

  $desc =~ s/Topic:/Title:/;
  $desc =~ s/Contest\s*Entry:?\s*//i;

  $class = $rows[0];
  $class .= ' br' if $count < 4;
  push @row1, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$desc);
  push @row2, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$image);
  push @row3, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$vote);

  if ( $count++ == 4 ) {
    push @table, $cgi->Tr(@row1);
    push @table, $cgi->Tr(@row2);
    push @table, $cgi->Tr(@row3);
    $count = 1;
    @row1 = ();
    @row2 = ();
    @row3 = ();
    push @rows, shift @rows;
  }
}

if  ( $count < 4 ) {
  until ( $count++ == 5 ) {
    push @row1, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
    push @row2, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
    push @row3, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
  }
  push @table, $cgi->Tr(@row1);
  push @table, $cgi->Tr(@row2);
  push @table, $cgi->Tr(@row3);
}

# reset

@row1 = ();
@row2 = ();
@row3 = ();
$count = 1;

# images

push @table, '<tr><td class="innertablesubhead" colspan="4">Images</td></tr>';

for my $topic ( sort @images ) {
  my $tref   = $for->topic($topic);
  my $uref   = $for->user($tref->{topic_poster});
  my $posts  = $for->topic_posts($topic);
  my $images = $for->post_attachments($posts->[0]);
  my $aref   = $for->attachment($images->[0]);
  
  #$body .= $cgi->pre("TOPIC:\n",Dumper($tref));
  #$body .= $cgi->pre("POSTS:\n",Dumper($posts));
  #$body .= $cgi->pre("IMAGES:\n",Dumper($images));
  #$body .= $cgi->pre("ATTACHMENT:\n",Dumper($aref));

  my $vote;
  $vote .= $cgi->start_form
        . $cgi->hidden({-name=>'vote',-value=>$topic})
        . $cgi->submit({-label=>'Vote'})
        . $cgi->end_form unless $voted;

  my ($image,$desc) = $for->attachment_html($aref->{1});  

  $desc = 'Title: ' . $cgi->b($cgi->a({-href=>'http://forums.macrophile.com/viewtopic.php?t='.$topic},$tref->{topic_title})) . $cgi->br
        . 'By: ' . $cgi->a({-href=>'http://users.macrophile.com/'.safe_username($uref->{username})},$uref->{username});

  $desc =~ s/Topic:/Title:/;
  $desc =~ s/Contest\s*Entry:?\s*//i;

  $class = $rows[0];
  $class .= ' br' if $count < 4;
  push @row1, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$desc);
  push @row2, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$image);
  push @row3, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},$vote);

  if ( $count++ == 4 ) {
    push @table, $cgi->Tr(@row1);
    push @table, $cgi->Tr(@row2);
    push @table, $cgi->Tr(@row3);
    $count = 1;
    @row1 = ();
    @row2 = ();
    @row3 = ();
    push @rows, shift @rows;
  }
}

if  ( $count < 4 ) {
  until ( $count++ == 5 ) {
    push @row1, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
    push @row2, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
    push @row3, $cgi->td({-class=>$class,-align=>'center',-valign=>'top',-width=>155},'&nbsp;');
  }
  push @table, $cgi->Tr(@row1);
  push @table, $cgi->Tr(@row2);
  push @table, $cgi->Tr(@row3);
}

$body .= "<tr><td class=\"innertablesubhead\" colspan=\"4\">Summer Contest</td></tr>\n"
        .  join('',@table)
        .  "\n\n";

$body .= "</td></tr></table>\n";

print $tmpl->do({ body => $body });


### Sub

sub vote {
  my $vote = shift @_;
  my $user = $auth->{userid};
  my $sql = 'insert into contest (user_id,vote) values (?,?)';
  my $sth = $dbh->prepare($sql);
  return $sth->execute($user,$vote);
}