package GCPlugins::GCfilms::GCAllocine;

###################################################
#
#  Copyright 2005-2007 Tian
#
#  This file is part of GCstar.
#
#  GCstar is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  GCstar is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GCstar; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###################################################

use strict;
use utf8;

use GCPlugins::GCfilms::GCfilmsCommon;

{
    package GCPlugins::GCfilms::GCPluginAllocine;

    use base qw(GCPlugins::GCfilms::GCfilmsPluginsBase);
 
    sub start
    {
        my ($self, $tagname, $attr, $attrseq, $origtext) = @_;        
        $self->{inside}->{$tagname}++;

        if ($self->{parsingList})
        {
            if ($tagname eq "a")
            {

                if (($attr->{href} =~ /^\/film\/fichefilm_gen_cfilm=/) && ($self->{inside}->{h4}))
                {
                    my $url = $attr->{href};
                    $self->{isMovie} = 1;
                    $self->{isInfo} = 0;
                    $self->{itemIdx}++;
                    $self->{itemsList}[$self->{itemIdx}]->{url} = $url;
                }
            }
        }
        else
        {
           if ($tagname eq "h4")
           {
               $self->{insideActors} = 0;
               $self->{insideDirector} = 0;
               if ($attr->{style} =~ /color:#D20000/)
               {
                   $self->{insideAge} = 1;
               }
               else
               {
                   $self->{insideAge} = 0;
               }
           }
           elsif ($tagname eq "img")
           {
               my $src = $attr->{src};
               if (!$self->{curInfo}->{image})
               {
                   if ($src =~ /\/nmedia\//)
                   {
                       $self->{curInfo}->{image} = $src;
                   }
                   elsif ($src =~ /AffichetteAllocine\.gif/i)
                   {
                       $self->{curInfo}->{image} = "empty";
                   }
               }
           }
           elsif ($tagname eq "br")
           {
               if ($self->{insideSynopsis})
               {
                   $self->{curInfo}->{synopsis} .= "\n";
               }
           }
        }
    }

    sub end
    {
        my ($self, $tagname) = @_;
        $self->{inside}->{$tagname}--;

        if ($tagname eq "h4")
        {
            $self->{insideSynopsis} = 0;
        }
    }

    sub text
    {
        my ($self, $origtext) = @_;

        if ($self->{parsingList})
        {
            if ($self->{isMovie})
            {
                $self->{itemsList}[$self->{itemIdx}]->{title} = $origtext;
                $self->{isMovie} = 0;
                $self->{isInfo} = 1;
                return;
            }
            if ($self->{isInfo})
            {
                if ($self->{inside}->{h4})
                {
                    if ( $origtext =~ /([0-9]{4})/ )
                    {
                        $self->{itemsList}[$self->{itemIdx}]->{date} = $1;
                    }
                }
                elsif ($self->{inside}->{h5})
                {
                    if ( $origtext =~ /^de (.*)/ )
                    {
                        $self->{itemsList}[$self->{itemIdx}]->{director} = $1;
                    }
                    elsif (( $origtext =~ m/^avec (.*)/ ) && (!$self->{itemsList}[$self->{itemIdx}]->{actors}))
                    {
                        $self->{itemsList}[$self->{itemIdx}]->{actors} = $1;
                    }
                }
            }
        }
        else
        {
            if (($self->{inside}->{h1}) && !($self->{inside}->{script}))
            {
                $origtext =~ s/[\r\n]//g;
                $self->{curInfo}->{title} = $origtext if length($origtext) > 2;
            }
            if ($self->{inside}->{h3})
            {
                $self->{insideSynopsis} = 1 if $origtext =~ /Synopsis/
            }
            if ($self->{inside}->{h4})
            {
                if ($self->{insideSynopsis})
                {
                    $self->{curInfo}->{synopsis} .= $origtext;
                }
                elsif ($self->{insideActors})
                {
                    $self->{curInfo}->{actors} .= $origtext;
                    $self->{curInfo}->{actors} =~ s/^\s*//;
                }
                elsif ($self->{insideDirector})
                {
                    $self->{curInfo}->{director} .= $origtext;
                }
                elsif ($self->{insideAge})
                {
                    $origtext =~ s/.*?([0-9]+).*/$1/;
                    $self->{curInfo}->{age} = $origtext;
                }
                elsif ( $origtext =~ /^Genre/ )
                {
                    $origtext =~ s/^Genre : //;
                    $origtext =~ s/, /,/g;
                    $self->{curInfo}->{genre} = $self->capWord($origtext);
                }
                elsif (( $origtext =~ /^Film / ) && (!$self->{curInfo}->{country}))
                {
                    $origtext =~ s/^Film //;
                    $self->{curInfo}->{country} = ucfirst $origtext;
                }
                elsif ( $origtext =~ /^Durée/ )
                {
                    $origtext =~ s/^Durée : (.*)?/$1/;
                    $self->{curInfo}->{time} = $origtext;
                }
                elsif (( $origtext =~ /Date de sortie/ )
                    && ( !$self->{curInfo}->{date} ) )
                {
                    $origtext =~ s/Date de sortie : (.*)/$1/;
                    $origtext =~ s/^[\s\n]*//;
                    $origtext =~ s/\s*$//;
                    $self->{curInfo}->{date} = $origtext;
                }
                elsif ( $origtext =~ /^Titre original/ )
                {
                    $origtext =~ s/^Titre original : .*?/$1/;
                    $self->{curInfo}->{original} = $origtext;
                }
                elsif (($origtext =~ /^Avec/ ) && (! $self->{curInfo}->{actors}))
                {
                    $self->{insideActors} = 1;
                }
                elsif ( $origtext =~ /^Réalisé par/ )
                {
                    $self->{insideDirector} = 1;
                }
           }
        }
    }

    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new();

        $self->{hasField} = {
            title => 1,
            date => 1,
            director => 1,
            actors => 1,
        };

        $self->{isInfo} = 0;
        $self->{isMovie} = 0;
        $self->{curName} = undef;
        $self->{curUrl} = undef;

        bless ($self, $class);
        return $self;
    }

    sub preProcess
    {
        my ($self, $html) = @_;
        $html =~ s/&nbsp;/ /gi;
        $html =~ s/<b>|<strong>|<\/b>|<\/strong>|<i>|<\/i>//gi;
        
        #Remove links for genre
        $html =~ s|<a href="/film/alaffiche_genre_gen_genre.*?>(.*?)</a>|$1|gi;
        #"

        if (!$self->{parsingList})
        {
            # Remove link "Plus" in list of actors
            $html =~ s|<a href="/film/casting_gen_cfilm=.*?>.*?</a>||gi;
        }
        
        return $html;
    }
    
    sub getSearchUrl
    {
        my ($self, $word) = @_;
	
        return "http://www.allocine.fr/recherche/?motcle=$word&f=3&rub=1";
    }
    
    sub getItemUrl
    {
        my ($self, $url) = @_;
		
        return "http://www.allocine.fr".$url;
    }

    sub getName
    {
        return "Allocine.fr";
    }
    
    sub getAuthor
    {
        return 'Tian';
    }
    
    sub getLang
    {
        return 'FR';
    }
    
    sub getCharset
    {
        return "ISO-8859-1";
    }
}

1;
