use zUtil;
use POSIX qw(strftime);
use Time::Local;

sub idiomOfTheDayHandler
{
    my $html = shift;
    my $type = shift;

    if( $type eq "collins" )
    {
        return collins( $html );
    }
    if( $type eq "goenglish" )
    {
        return goenglish( $html );
    }
    if( $type eq "grammarstation" )
    {
        return grammarstation( $html );
    }
    else
    {
        return wordEnglish( $html );
    }   
}

sub goenglish
{
   my $html = shift;
   my ( @rValue ) = ();
   my ($table, $col, $p);
   my ( $idiom );

   print "=------------ idiomOfTheDayHandler (goenglish) -------------=\n";
   #print "Html: $html\n";
   #exit;

   if( $html =~ /Today\'s Idiom = \"(.*?)\"/gsi )
   {
       $idiom = $1;
   }

   print "found idiom: $idiom\n";

   $count = 2;
   while( $count < 30 )
   {
       $table = zUtil::getNthItemNew( $count, $html, "<table", "</table>" );
       if( $table =~ /$idiom/gsi )
       {
           #$print "found a table with the idiom: $table\n";
           if( $table =~ /.*?<i><b>.*?<\/b><\/i>(.*?)<\/font/gsi )
           {
               print "desc is: $1\n";
               $desc = $1;
           }
           if( $table =~ /.*?Example:.*?\"(.*?)<b>/gsi )
           {
               print "phrase is: $1\n";
               $phrase = $1;
           }

       }

       $count = $count + 1;
   }

   push @rValue,
   {
       'idiom' => $idiom,
       'desc' => $desc
   };
   
   push @rValue,
   {
       "phrase1" => $desc
   };

   push @rValue,
   {
       "phrase2" => $phrase
   };

   #exit;

   return @rValue;
}

sub collins
{
   my $html = shift;
   my ( @rValue ) = ();
   my ($table, $col, $p);

   print "=------------ idiomOfTheDayHandler (collins) -------------=\n";
   #print "Html: $html\n";
   #exit;

   $table = zUtil::getNthItemNew( 7, $html, "<TABLE", "</TABLE>" );
   $col = zUtil::getNthItemNew( 3, $table, "<td", "</td>" );

   $idiom = "Not Available";
   $desc = "";
   $phrase = "";

   #print "table: $table\n";

   $p = zUtil::getNthItemNew( 3, $col, "<P", "</P>" );
   if( $p =~ /<strong>(.*?)<\/strong><br>(.*?)$/gsi )
   {
       $idiom = $1;
       $desc =  $2;
       $desc =~ s/\[$//si;
       $desc =~ s/\<.*?\>//gsi;
   }

   $limit = 30;
   $p = zUtil::getNthItemNew( 4, $col, "<P", "</P>" );
   if( $p =~ /<em>(.*?)<\/em>/gsi )                                                                  
   {                                                                                                                       
       $phrase = $1; 
       $phrase =~ s/\<.*?\>//gsi;

       $keeper = "";
       $total_count = 0;
       my @words = split( / / ,$phrase); #split into words
       foreach my $word (@words)
       {    
           $total_count++;
           if( $total_count < $limit )
           {
               $keeper .= "$word ";
           }
       }
       if( $total_count >= $limit )
       {
           $keeper .= "<b>...</b>";
       }
       $phrase = $keeper;
   }      
   
   #if( $col =~ /\<p class=\".ArticleBody\"><B><P.*?><SPAN.*?><FONT.*?>(.*?)<\?xml\:namespace.*?\>/gsi )
   #{
   #    $desc = $1;
   #}
   # if( $col =~ /\<SPAN.*?>.*?<I>(.*?)<\/I><\/SPAN>/gsi )
   #{
   #    $phrase = $1;
   #}
   #exit;

   push @rValue,
   {
       'idiom' => $idiom,
       'desc' => $desc
   };
   
   push @rValue,
   {
       "phrase1" => $desc
   };

   push @rValue,
   {
       "phrase2" => $phrase
   };

   print "idiom: $idiom\n";
   print "desc: $desc\n";
   print "phrase: $phrase\n";

   # exit;

   return @rValue; 
}

sub grammarstation
{
    my $html = shift;

    
    
}

sub wordEnglish
{
   $html = shift;
   my( @rValue ) = ();
   my($done, $after, $table, $idiom, $phrase, $desc, $key, $temp );

   print "=------------------ idiomOfTheDayHandler --------------------=\n";
   #print "Html: $html\n";
   #exit;

   $day = strftime( "%e", localtime() ); 
   $day =~ s/^\s//gsi;

   #print "day is: $day\n";
   

   if( $html =~ /msg\[$day\].*?Idiom\s$day<.*?><.*?>(.*?)<br>(.*?)<.*?><.*?>(.*?)\";/gsi )
   {
       $idiom = $1;
       $desc = $2;
       $phrase = $3;

       $idiom =~ s/(\w+)/\u\L$1/g;
       print "found an idiom: $idiom\n";
       print "found an desc: $desc\n";
       print "found an phrase: $phrase\n";
   }
   else
   {
       $idiom = "Not Available";
       $desc = "";
       $phrase = "";
   }

   #exit;

   push @rValue,
   {
       'idiom' => $idiom
   };
   
   push @rValue,
   {
       "phrase1" => $desc
   };

   push @rValue,
   {
       "phrase2" => $phrase
   };


#   exit;
   return @rValue;
}

return 1;
