package GCPlugins::GCCinematografo;

###################################################
#
#  Copyright 2005 Tian
#
#  This file is part of GCfilms.
#
#  GCfilms 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.
#
#  GCfilms 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 GCfilms; 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::GCPluginsBase;

{
    package GCPlugins::GCPluginCinematografo;

    use base qw(GCPluginParser);

    sub start
    {
        my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
	
        $self->{inside}->{$tagname}++;
		
        if ($self->{parsingList})
        {
            if (($tagname eq 'a') && ($attr->{href} =~ /bancadati_scheda.asp\?sch/))
            {
                $self->{isMovie} = 1;
                $self->{movieIdx}++;
                $self->{movieList}[$self->{movieIdx}]->{url} = $attr->{href};
            }
        }
        else
        {
            if ($tagname eq 'img')
            {
                $self->{curInfo}->{image} = 'http://www.cinematografo.it/bdcm/'.$attr->{src}
                    if $attr->{src} =~ m|^img/|;
            }
            elsif ($tagname eq 'font')
            {
                $self->{insideTitle} = 1
                    if ($attr->{color} eq '#ffffff') && ($attr->{size} eq '2');
            }
            elsif ($tagname eq 'td')
            {
                $self->{insideInfoName} = 1 if $attr->{width} eq '96';
                $self->{insideInfoContent} = 1 if $attr->{width} eq '304';
            }
        }
    }

    sub end
    {
		my ($self, $tagname) = @_;
		
        $self->{inside}->{$tagname}--;
        
        if ($tagname eq 'td')
        {
            $self->{insideInfoName} = 0;
            $self->{insideInfoContent} = 0;
        }
    }

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

        $origtext =~ s/^\s*//m;
        $origtext =~ s/\s*$//m;

        if ($self->{parsingList})
        {            
            if ($self->{isMovie})
            {
                if ($self->{inside}->{a})
                {
                    $self->{movieList}[$self->{movieIdx}]->{title} = $origtext
                        if ! $self->{movieList}[$self->{movieIdx}]->{title};
                }
                else
                {
                    $origtext =~ /([0-9]{4})/;
                    $self->{movieList}[$self->{movieIdx}]->{year} = $1;
                    $self->{isMovie} = 0;
                }
            }
        }
        else
        {
            if ($self->{insideTitle})
            {
                $self->{curInfo}->{title} = $origtext
                    if ! $self->{curInfo}->{title};
                $self->{insideTitle} = 0;
            }
            if ($self->{inside}->{font})
            {
                if ($self->{insideInfoName})
                {
                    undef $self->{currentContent};
                    $self->{currentContent} = 'date' if $origtext eq 'Anno';
                    $self->{currentContent} = 'orig' if $origtext eq 'Altri titoli';
                    $self->{currentContent} = 'time' if $origtext eq 'Durata';
                    $self->{currentContent} = 'nat' if $origtext eq 'Origine';
                    $self->{currentContent} = 'age' if $origtext eq 'Vietato';
                    $self->{currentContent} = 'type' if $origtext eq 'Genere';
                    $self->{currentContent} = 'director' if $origtext eq 'Regia';
                    $self->{currentContent} = 'actors' if $origtext eq 'Attori';
                    $self->{currentContent} = 'synopsis' if $origtext eq 'Trama';
                }
                elsif ($self->{insideInfoContent})
                {
                    if ($self->{currentContent} eq 'actors')
                    {
                        if ($self->{inside}->{a})
                        {
                            print "Got $origtext\n";
                            $self->{curInfo}->{actors} .= ', '
                                if $self->{curInfo}->{actors};
                            $origtext =~ s/ +/ /g;
                            $self->{curInfo}->{actors} .= $origtext;
                        }
                    }
                    else
                    {
                        $self->{curInfo}->{$self->{currentContent}} = $origtext
                            if $self->{currentContent};
                    }
                }
            }
        }
    } 

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

        $self->{isMovie} = 0;
        $self->{insideTitle} = 0;

        return $self;
    }

    sub preProcess
    {
        my ($self, $html) = @_;

        # Remove bold tags as they are not well imbricated.
        $html =~ s|<b>(.*?)</b>|$1|gm;
        $html =~ s|&nbsp;| |gm;

        return $html;
    }
    
    sub getSearchUrl
    {
		my ($self, $word) = @_;
	
        return "http://www.cinematografo.it/bdcm/bancadati_query.asp?ty=CONTIENEPAROLE&fi=$word&R1=TI&I2.x=0&I2.y=0";
    }
    
    sub getMovieUrl
    {
		my ($self, $url) = @_;
		
		return 'http://www.cinematografo.it/bdcm/'.$url;
    }

    sub changeUrl
    {
        my ($self, $url) = @_;
        
        return $url;
    }

    sub getName
    {
        return "Cinematografo";
    }
    
    sub getImageFile
    {
        return "";
    }

    sub getCharset
    {
        my $self = shift;
    
        return "ISO-8859-1";
    }

    sub getAuthor
    {
        return 'Tian';
    }
    
    sub getLang
    {
        return 'IT';
    }
    sub hasSearchActors
    {
        return 0;
    }

    sub hasSearchDirector
    {
        return 0;
    }
}

1;
