package GCPlugins::GCcomics::GCltb;

###################################################
#
#  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::GCcomics::GCcomicsCommon;

{
    package GCPlugins::GCcomics::GCPluginltb;

    use LWP::Simple qw($ua);
    use HTTP::Cookies;

    use base qw(GCPlugins::GCcomics::GCcomicsPluginsBase);
 
    sub start
    {
        my ($self, $tagname, $attr, $attrseq, $origtext) = @_;

#
#       Behandlung von Zweiteditionen einbauen!!
#       + Erkennung ob es eine Zweitversion gibt
#       + Ebenfalls erfassen und Auswahl bieten
#       + Bild, Datum und Link anpassen, Titel besser auch
#       - verschiedene Titel für alt und neu in der Übersicht anpassen
#       - Hinzufügen: Verlag, Editor, Preis (anpassen), Seiten
#	+ Umlaute testen
#

        if ( $self->{parsingList} )
	{
		if (($tagname eq "a") && ($attr->{'onmouseover'} =~ /^.*bk_neuausgabe1.*$/i))
		{
			$self->{neweditionexists} = 1;
		}

		if ($tagname eq "b")
		{
		    $self->{isBold} = 1;
		}
		elsif (($tagname eq "td") && ($self->{nextisTitle}))
		{
		    $self->{insideTitle} = 1;
		    $self->{insideVolume} = 0;
		    $self->{insidePublishdate} = 0;
		    $self->{nextisTitle} = 0;
		}
		elsif (($tagname eq "td") && ($self->{nextisVolume}))
		{
		    $self->{insideTitle} = 0;
		    $self->{insideVolume} = 1;
		    $self->{insidePublishdate} = 0;
		    $self->{nextisVolume} = 0;
		}
		elsif (($tagname eq "td") && ($self->{nextisPublishdate}))
		{
		    $self->{insideTitle} = 0;
		    $self->{insideVolume} = 0;
		    $self->{insidePublishdate} = 1;
		    $self->{nextisPublishdate} = 0;
		}

		if ((($tagname eq "a") && ($attr->{'onmouseover'} =~ /^.*bk_neuausgabe1.*$/i)) && ($self->{once1}))
		{
			$self->{once1} = 0;
	                $self->{itemIdx}++;
			$self->{newurl} = "http://www.lustige-taschenbuecher.de/".$attr->{'href'};
	                $self->{itemsList}[$self->{itemIdx}]->{url} = $self->{newurl};
			$self->{itemsList}[$self->{itemIdx}]->{extra} = "2. Neuauflage";
		}
		elsif ($self->{once2})
		{
			$self->{once2} = 0;
	                $self->{itemIdx}++;
	                $self->{itemsList}[$self->{itemIdx}]->{url} = $self->{url};
			$self->{itemsList}[$self->{itemIdx}]->{extra} = "Erstauflage";
		}
	}
	else # end parsingList
	{
		if (($tagname eq "a") && ($attr->{'onmouseover'} =~ /^.*bk_neuausgabe1.*$/i))
		{
			$self->{newedition} = 0;
		}
		elsif (($tagname eq "a") && ($attr->{'onmouseover'} =~ /^.*kb_testoriginal.*$/i))
		{
			$self->{newedition} = 1;
		}

		if ($tagname eq "b")
		{
		    $self->{isBold} = 1;
		}
		elsif (($tagname eq "td") && ($self->{nextisTitle}))
		{
		    $self->{insideTitle} = 1;
		    $self->{insideVolume} = 0;
		    $self->{insidePublishdate} = 0;
		    $self->{insideCost} = 0;
		    $self->{nextisTitle} = 0;
		}
		elsif (($tagname eq "td") && ($self->{nextisVolume}))
		{
		    $self->{insideTitle} = 0;
		    $self->{insideVolume} = 1;
		    $self->{insidePublishdate} = 0;
		    $self->{insideCost} = 0;
		    $self->{nextisVolume} = 0;
		}
		elsif (($tagname eq "td") && ($self->{nextisPublishdate}))
		{
		    $self->{insideTitle} = 0;
		    $self->{insideVolume} = 0;
		    $self->{insidePublishdate} = 1;
		    $self->{insideCost} = 0;
		    $self->{nextisPublishdate} = 0;
		}
		elsif (($tagname eq "td") && ($self->{nextisCost}))
		{
		    $self->{insideTitle} = 0;
		    $self->{insideVolume} = 0;
		    $self->{insidePublishdate} = 0;
		    $self->{insideCost} = 1;
		    $self->{nextisCost} = 0;
		}
	}
    }

    sub end
    {
        # my ($self, $tagname) = @_;
    }

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

        return if ($origtext eq " ");

        return if ($self->{parsingEnded});
        
        if ( $self->{parsingList} )
	{
		if ($self->{isBold})
		{
		    $self->{isBold} = 0;

		    if ($origtext eq "Buchtitel:")
		    {
		        $self->{nextisTitle} = 1;
		        $self->{nextisVolume} = 0;
		        $self->{nextisPublishdate} = 0;

		    }
		    elsif ($origtext eq "Ausgabe:")
		    {
		        $self->{nextisTitle} = 0;
		        $self->{nextisVolume} = 1;
		        $self->{nextisPublishdate} = 0;
		    }
		}
		elsif ($self->{insideTitle})
		{
		        $self->{itemsList}[$self->{itemIdx}-1]->{title} = $origtext;
			# No suitable way to find the new editions title
			#if ($self->{neweditionexists})
			#{
		        #	$self->{itemsList}[$self->{itemIdx}]->{title} = $origtext;
			#}
			$self->{insideTitle} = 0;
		}
		elsif ($self->{insideVolume})
		{
			$origtext =~ s/Nr\.\ //; # Vorangestelltes "Nr. " entfernen
			$origtext =~ s/No\.\ //; # Vorangestelltes "No. " entfernen
			$self->{itemsList}[$self->{itemIdx}]->{volume} = $origtext;
			$self->{insideVolume} = 0;

			$self->{itemsList}[$self->{itemIdx}]->{series} = "Lustiges Taschenbuch";
			$self->{itemsList}[$self->{itemIdx}]->{volume} = $self->{volume};
			if ($self->{neweditionexists})
			{
				$self->{itemsList}[$self->{itemIdx}-1]->{series} = "Lustiges Taschenbuch";
				$self->{itemsList}[$self->{itemIdx}-1]->{volume} = $self->{volume};
			}
		}
	}
	else # end parsingList
	{
		if ($self->{isBold})
		{
		    $self->{isBold} = 0;

		    if ($origtext eq "Buchtitel:")
		    {
		        $self->{nextisTitle} = 1;
		        $self->{nextisVolume} = 0;
		        $self->{nextisPublishdate} = 0;
			$self->{nextisCost} = 0;
		    }
		    elsif ($origtext eq "Ausgabe:")
		    {
		        $self->{nextisTitle} = 0;
		        $self->{nextisVolume} = 1;
		        $self->{nextisPublishdate} = 0;
			$self->{nextisCost} = 0;
		    }
		    elsif ($origtext eq "Erscheinungsdatum:")
		    {
		        $self->{nextisTitle} = 0;
		        $self->{nextisVolume} = 0;
		        $self->{nextisPublishdate} = 1;
			$self->{nextisCost} = 0;
		    }
		    elsif ($origtext eq "Originalpreis:")
		    {
		        $self->{nextisTitle} = 0;
		        $self->{nextisVolume} = 0;
		        $self->{nextisPublishdate} = 0;
			$self->{nextisCost} = 1;
		    }
		}
		elsif ($self->{insideTitle})
		{
		        $self->{curInfo}->{title} = $origtext;
			$self->{insideTitle} = 0;
		}
		elsif ($self->{insideVolume})
		{
			$origtext =~ s/Nr\.\ //; # Vorangestelltes "Nr. " entfernen
			$origtext =~ s/No\.\ //; # Vorangestelltes "No. " entfernen
			$self->{curInfo}->{volume} = $origtext;
			$self->{insideVolume} = 0;
		}
		elsif ($self->{insidePublishdate})
		{
			$self->{curInfo}->{publishdate} = GCUtils::strToTime($origtext, "%d.%m.%Y");
			$self->{insidePublishdate} = 0;
		}
		elsif ($self->{insideCost})
		{
		        $self->{curInfo}->{cost} = $origtext;

			$self->{curInfo}->{series} = "Lustiges Taschenbuch";

			if ($self->{newedition})
			{
				$self->{curInfo}->{image} = "http://www.lustige-taschenbuecher.de/images/cover/ltb2na/gr/ltb_neu_".sprintf( "%03d", $self->{volume} ).".jpg";
				$self->{curInfo}->{type} = "2. Neuauflage";
			}
			else
			{
				$self->{curInfo}->{image} = "http://www.lustige-taschenbuecher.de/images/cover/ltb/gr/ltb_".sprintf( "%03d", $self->{volume} ).".jpg";
				$self->{curInfo}->{type} = "Erstauflage";
			}

			$self->{parsingEnded} = 1;
			$self->{insideCost} = 0;
		}
	}
    }

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

        $self->{ua}->cookie_jar(HTTP::Cookies->new);

        bless ($self, $class);

        $self->{hasField} = {
		series => 1,
		title  => 1,
		volume => 1,
	};   

        $self->{itemIdx} = 0;
        $self->{series} = undef;
        $self->{url} = undef;
        $self->{nextisTitle} = 0;
        $self->{nextisVolume} = 0;
        $self->{nextisPublishdate} = 0;
	$self->{once1} = 1;
	$self->{once2} = 1;
	$self->{newedition} = 0;
	$self->{neweditionexists} = 0;

        return $self;
    }

    sub preProcess
    {
        my ($self, $html) = @_;
        
        $self->{parsingEnded} = 0;
        return $html;
    }

    sub getSearchUrl
    {
        my ($self, $word) = @_;

	if ($self->{searchField} eq 'volume')
        {
            $self->{volume} = $word;
            $self->{url} = "http://www.lustige-taschenbuecher.de/ltb-katalog-".$word.".html";
            return $self->{url};
        }
        else
        {
            return "http://www.lustige-taschenbuecher.de/";
        }
    }

    sub getReturnedFields
    {
        my $self = shift;

        $self->{hasField} = {
            series => 1,
            url    => 1,
            title  => 1,
        };
    }

    sub getExtra
    {
        return "Auflage";
    }

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

    sub getNumberPasses
    {
        return 1;
    }

    sub getName
    {
        return "Lustige Taschenbuecher DB";
    }
    
    sub getAuthor
    {
        return 'vollkorn';
    }
    
    sub getLang
    {
        return 'DE';
    }

    sub getSearchFieldsArray
    {
        return ['volume'];
    }

}

1;
