package GCPlugins::GCfilms::GCFilmWeb;

###################################################
#
#  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::GCPluginFilmWeb;

    use base qw(GCPlugins::GCfilms::GCfilmsPluginsBase);

    sub start
    {
        my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
	
        $self->{inside}->{$tagname}++;
		
        if ($self->{parsingList})
        {
            if ($self->{parsingEnded})
            {
                if (($tagname eq 'input')
                 && ($attr->{name} eq 'id'))
                {
                    $self->{itemIdx} = 0;
                    $self->{itemsList}[0]->{title} = '';
                    $self->{itemsList}[0]->{url} = 'http://www.filmweb.pl/Film?id='. $attr->{value};
                }
            }
            
            if (($tagname eq 'a')
             && ($attr->{class} eq 'searchResultTitle'))
            {
                $self->{isMovie} = 1;
                $self->{itemIdx}++;
                $self->{itemsList}[$self->{itemIdx}]->{url} = $attr->{href};
            }
        }
        else
        {
            if ($tagname eq 'div')
            { 
                $self->{isTitle} = 1
                    if ($attr->{class} eq 'film-title');
                $self->{isImage} = 1
                    if ($attr->{class} eq 'film-poster');
            }
            elsif ($tagname eq 'span')
            {
                $self->{isOrig} = 1
                    if (($attr->{class} eq 'aka')); 
            }
            elsif ($tagname eq 'a')
            {
                $self->{isCountry} = 1
                    if (! ($self->{curInfo}->{date} || $self->{curInfo}->{type}))
                    && ($attr->{href} =~ m|/film\?countryIds=|);
                $self->{isGenre} = 1
                    if ($attr->{href} =~ m|/film\?genreIds=|);
                if ($attr->{href} =~ m|,FilmDescriptions,|)
                {
                    my $page = $self->loadPage($attr->{href});
                    $page =~ m|<li><div align="justify">(.*?)</div></li>|ms;
                    $self->{curInfo}->{synopsis} = $1;
                    $self->{curInfo}->{synopsis} =~ s/<br\/>/<br>/gi;
                    $self->{curInfo}->{synopsis} =~ s/\n//gm;
                }
            }
            elsif ($tagname eq 'td')
            {
                $self->{isActors} = 1
                    if ($attr->{class} eq 'film-actor');
                $self->{isRoles} = 1
                    if ($attr->{class} eq 'film-protagonist');
            }
            elsif ($tagname eq 'strong')
            {
		if ($self->{isPreDate}) { 
                    $self->{isDate} = 1;
 		    $self->{isPreDate} = 0;
 		}
            }
            elsif ($tagname eq 'img')
            {
                if ($self->{isImage})
                {
                    $self->{curInfo}->{image} = $attr->{src};
                    $self->{isImage} = 0;
                }
            }
	    elsif ($tagname eq 'p')
	    {
                $self->{isPreDate} = 1
                    if ($attr->{class} eq 'film-premieres');
            }
        }
    }

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

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

        if ($self->{parsingList})
        {
            if ($self->{inside}->{title})
            {
                $self->{parsingEnded} = 1
                    if $origtext !~ /Filmweb\.pl - /;
            }
            if ($self->{parsingEnded})
            {
                return;
            }
            if ($self->{isMovie})
            {
                $self->{isMovie} = 0;
                if ($self->{inside}->{a})
                {
                    $origtext =~ s/^\s*//m;
                    $origtext =~ s/\s*$//m;
                    $origtext =~ s|\s*\([[:alpha:].]*\)$||;
                    return if !$origtext;
                    #$origtext =~ m|(.*?)\(([0-9]{4})\)$|;
                
                    $self->{itemsList}[$self->{itemIdx}]->{title} = $origtext;
                    #$self->{itemsList}[$self->{itemIdx}]->{date} = $2;
                    $self->{isMovie} = 1;
                }
                else
                {
                    $self->{itemsList}[$self->{itemIdx}]->{date} = $1
                        if $origtext =~ /\s*\(([0-9]{4})\)\s*/;
                }
            }

        }
        else
        {
            $origtext =~ s/^\s*//m;
            $origtext =~ s/\s*$//m;
                 
            return if !$origtext;       
            if ($self->{isTitle})
            {
                $self->{curInfo}->{title} = $origtext;
                $self->{isTitle} = 0;
            }
            elsif ($self->{isOrig})
            {
                $self->{curInfo}->{original} = $origtext if (($origtext !~ /\(/) && ($origtext !~ /rednia ocena:/) );
                $self->{curInfo}->{original} =~ s/(.*), (.*)/$2 $1/;
                $self->{isOrig} = 0;   
            }
            elsif ($self->{isCountry})
            {
                $self->{curInfo}->{country} .= $self->{curInfo}->{country} ? ", " . $origtext : $origtext;
                $self->{isCountry} = 0;
            }
            elsif ($self->{isGenre})
            {
                $self->{curInfo}->{genre} .= $self->{curInfo}->{genre} ? ", " . $origtext : $origtext;
                $self->{isGenre} = 0;
            }
            elsif ($self->{isDate})
            {
                $self->{curInfo}->{date} = $origtext;
                $self->{isDate} = 0;   
            }
            elsif ($self->{inside}->{details})
            {
                $origtext =~ m/re.yseria\s*(.+?)\s*scenariusz/gms;
                $self->{curInfo}->{director} = $1;
                $self->{curInfo}->{director} =~ s/\s+/ /g;
                $origtext =~ m/czas trwania: ([0-9]*)/;
                $self->{curInfo}->{time} = $1;
            }
            elsif ($self->{isActors})
            {
                if ($self->{actorsCounter} < $GCPlugins::GCfilms::GCfilmsCommon::MAX_ACTORS) {
                  push @{$self->{curInfo}->{actors}}, [$origtext];
                  $self->{actorsCounter}++;
                }
                $self->{isActors} = 0;
            }            
            elsif ($self->{isRoles})
            {
               push @{$self->{curInfo}->{actors}->[$self->{actorsCounter} - 1]}, $origtext
                   if ($self->{actorsCounter} <= $GCPlugins::GCfilms::GCfilmsCommon::MAX_ACTORS);
                $self->{isRoles} = 0;
            }

        }
    } 

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

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

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

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

        $self->{parsingEnded} = 0;
        $self->{insideResults} = 0;

        if ($self->{parsingList})
        {
            $html =~ s|<b>(.*?)</b>|$1|gms;
        }
        else
        {
            $html =~ s|<p class="film-info">(.*?)</p>|<details>$1</details></p>|gms;
            my $details = $1;
            $details =~ s|<a[^>]*>([^<]*)</a>|$1|gms;
            $details =~ s|<abbr[^>]*>([^<]*)</abbr>|$1|gms;
            $html =~ s|<details>(.*?)</details>|<details>$details</details>|gms;
            
        }

        return $html;
    }
    
    sub getSearchUrl
    {
		my ($self, $word) = @_;
        return "http://www.filmweb.pl/szukaj?q=$word&c=film"
    }
    
    sub getItemUrl
    {
		my ($self, $url) = @_;
		
        return $url unless $url eq '';
        return "filmweb.pl";
    }

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

    sub getName
    {
        return "FilmWeb";
    }
    
    sub getCharset
    {
        my $self = shift;
    
        return "ISO-8859-2";
    }

    sub getAuthor
    {
        return 'Tian';
    }
    
    sub getLang
    {
        return 'PL';
    }

    sub getDefaultPictureSuffix
    {
        return '.jpg';
    }
}

1;
