package GCPlugins::GCfilms::GCOFDb;

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

    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} =~ m/view\.php\?page=film&fid=[0-9]*/)
                 || ($attr->{href} =~ m|^film/[0-9]*|))
                {
                    $self->{isMovie} = 1;
                    $self->{isInfo} = 1;
                    $self->{itemIdx}++;
                    $self->{itemsList}[$self->{itemIdx}]->{url} = $attr->{href};
                }
            }
            elsif ($tagname eq "div")
            {
                if ($attr->{class} eq "year")
                {
                    $self->{isYear} = 1;
                }
            }
        }
        else
        {
            if ($tagname eq "font")
            {
                if ($attr->{face} eq "Arial,Helvetica,sans-serif")
                {
                    if ($attr->{size} eq "3")
                    {
                        $self->{insideName} = 1;
                    }
                    elsif ($attr->{size} eq "2")
                    {
                        $self->{insideInfosNames} = 1 if $attr->{class} eq "Normal";
                        $self->{insideInfos} = 1 if $attr->{class} eq "Daten";
                    }
                }
            }
            elsif (($tagname eq "img") && ($attr->{src} =~ m|img\.ofdb\.de/film/[0-9]+/[0-9]*.jpg|))
            {
                $self->{curInfo}->{image} = $attr->{src}
                    if ! $self->{curInfo}->{image};
            }
            elsif ($tagname eq "a")
            {
                if ($attr->{href} =~ m/view\.php\?page=blaettern&Kat=Land&Text=(.*)/)
                {
                    my $country = GCUtils::urlDecode($1);
                    $self->{curInfo}->{country} .= ', ' if $self->{curInfo}->{country};
                    $self->{curInfo}->{country} .= $country; 
                }
                $self->{curInfo}->{date} = $1
                    if ($attr->{href} =~ m/view\.php\?page=blaettern&Kat=Jahr&Text=([0-9]{4})/);
            }
            elsif (($tagname eq "div") && ($attr->{class} eq "synopsis"))
            {
                $self->{insideSynopsis} = 1;
            }
        }
    }

    sub end
    {
        my ($self, $tagname) = @_;
        
        if ($tagname eq "tr")
        {
            $self->{insideDirector} = 0;
            $self->{insideActors} = 0;
            $self->{insideGenre} = 0;
            $self->{insideInfos} = 0;
        }

        $self->{inside}->{$tagname}--;
    }

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

        return if length($origtext) < 2;
           
        if ($self->{parsingList})
        {
            if ($self->{isMovie})
            {
                $self->{itemsList}[$self->{itemIdx}]->{"title"} = $origtext;
                $self->{isMovie} = 0;
                $self->{isInfo} = 1;
                return;
            }
            elsif ($self->{isYear})
            {
                $self->{itemsList}[$self->{itemIdx}]->{"year"} = $origtext;
                $self->{isYear} = 0;
            }
        }
        else
        {
            if ($self->{insideName})
            {
                $self->{curInfo}->{title} = $origtext if !$self->{curInfo}->{title};
                $self->{insideName} = 0;
            }
            elsif ($self->{insideInfosNames})
            {
                $self->{insideOrig} = 1 if $origtext =~ m/Originaltitel:/;
                $self->{insideDirector} = 1 if $origtext =~ m/Regie:/;
                $self->{insideActors} = 1 if $origtext =~ m/Darsteller:/;
                $self->{insideGenre} = 1 if $origtext =~ m/Genre\(s\):/;
                $self->{insideInfosNames} = 0;
            }
            elsif ($self->{insideInfos} && $self->{inside}->{font})
            {
                if ($self->{insideOrig})
                {
                    $self->{curInfo}->{original} = $origtext;
                    $self->{insideOrig} = 0;
                    $self->{insideInfos} = 0;
                }
                elsif ($self->{insideNat})
                {
                    $self->{curInfo}->{country} = $origtext;
                    $self->{insideNat} = 0;
                    $self->{insideInfos} = 0;
                }
                elsif ($self->{insideDirector})
                {
                    $self->{curInfo}->{director} .= $self->{curInfo}->{director} ? ', ' . $origtext : $origtext;
                }
                elsif ($self->{insideActors})
                {
                    $self->{curInfo}->{actors} .= $self->{curInfo}->{actors} ? ', ' . $origtext : $origtext
                        if $self->{actorsCounter} < $GCPlugins::GCfilms::GCfilmsCommon::MAX_ACTORS;
                    $self->{actorsCounter}++;
                }
                elsif ($self->{insideGenre})
                {
                    $self->{curInfo}->{genre} .= $self->{curInfo}->{genre} ? ',' . $origtext : $origtext;
                }
            }
            elsif ($self->{insideSynopsis})
            {
                $origtext =~ m/(http.*?)(\s|$)/;
                my $page = $self->loadPage($1);
                $page =~ m|<font face="Arial,Helvetica,sans-serif" size="2" class="Blocksatz">.*?</a><br>[^<]*</b>(?:</b>)?<br><br>(.*?)</font>|ms;
                $self->{curInfo}->{synopsis} = $1;
                $self->{curInfo}->{synopsis} =~ s/<br \/>/<br>/gi;
                $self->{curInfo}->{synopsis} =~ s/\n//gm;
                $self->{insideSynopsis} = 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) = @_;
       
        if ($self->{parsingList})
        {
            $html =~ s|<font\s*size="\d+">([^<]*)</font>|$1|gms;
            $html =~ s|onmouseover="[^"]*"||gms;
            #"
        }
        $html =~ s/[0-9]*\. (<a href='view\.php\?page=film&fid=[0-9]*'>[^<]*)<font size='1'> \/ [^<]*<\/font> \(([0-9]{4})\)<\/a><br>/$1<\/a><div class="year">$2<\/div>/g;
        $html =~ s/<a href="view\.php\?page=liste&Name=[^"]*">([^<]*)<\/a>/$1/g;
        $html =~ s/<a href="view\.php\?page=genre&Genre=[^"]*">([^<]*)<\/a>/$1/g;
        $html =~ s/<font face="Arial,Helvetica,sans-serif" size="2" class="Blocksatz"><p class="Blocksatz"><b>Inhalt:<\/b>\s?([^<]*)<a href="(view\.php\?page=inhalt&fid=[0-9]*&sid=[0-9]*)">\s?<b>\[mehr\]<\/b><\/a><\/p><\/font>/<div class="synopsis">$1\nhttp:\/\/www.ofdb.de\/$2<\/div>/;
        $html =~ s|<font face="Arial,Helvetica,sans-serif" size="2" class="Blocksatz"><p\s*class="Blocksatz"><b>Inhalt:</b>\s?([^<]*)<a href="(plot/[0-9]*[^"]*)">\s?<b>\[mehr\]</b></a></p></font>|<div class="synopsis">$1\nhttp://www.ofdb.de/$2</div>|gm;
        #"
        $html =~ s/%DF/ss/;

        return $html;
    }
    
    sub getSearchUrl
    {
        my ($self, $word) = @_;
	
        return "http://www.ofdb.de/view.php?page=suchergebnis&Kat=DTitel&SText=$word";
    }
    
    sub getItemUrl
    {
        my ($self, $url) = @_;
		
        return 'http://www.ofdb.de/' . $url;
    }

    sub getName
    {
        return "OFDb.de";
    }
    
    sub getAuthor
    {
        return 'MeV';
    }
    
    sub getLang
    {
        return 'DE';
    }

}

1;
