#KanDeLa.
#Copyright (C) 2011 Alain Barbason
#
#This file is part of KanDeLa.
#
#KanDeLa 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 3 of the License, or
#(at your option) any later version.
#
#KanDeLa 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 KanDeLa.  If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use warnings;
use vars qw($sf_curve $view);
my $sf_curve;
package Curve;
use Commun;
our @ISA=qw( Commun );
use Data::Dumper;
# rewrite : to select an image, use openfile, and change x,y position with button
our %liste;
sub getListe {
    my $self=shift;    # rewrite $class ??
    return %liste;
}
sub new {
    my $classe=shift;
    my $self=$classe->SUPER::new();
    my %param=@_;
    if ($param{"-id"}) {
        $self->{ID}=$param{"-id"};
        Commun::set_id($self->{ID});
    }
    $self->{COORD}=$param{"-coord"};
    $self->{WIDTH}=3;
    $self->{COULEUR}=
      defined($param{"-couleur"}) ? $param{"-couleur"} : "purple";
    # rewrite change COULEUR by COLOR in all files
    $self->{SMOOTH}="True";
    $self->{SELECT}=0;
    bless($self, $classe);
    if ($self->{COORD}) {$self->coord(split(/,/, $self->{COORD}));}
    $liste{$self->id}=$self;
    $self->undoDelete;
    #print Dumper $self;
    return $self;
}
sub coord {
    my $self=shift;
    my @coord=@_;
    $self->{COORD}=\@coord;
    $self->{LINE}=$::drawing->createLine(
        @coord,
        -width  => $self->{WIDTH},
        -fill   => $self->{COULEUR},
        -smooth => $self->{SMOOTH}
    );
    $::liste{$self->{LINE}}=$self;
}
sub getCoords {    # rewrite fusion with "sub coord"
    my $self=shift;
    return @{$self->{COORD}};
}
sub color {
    my $self=shift;
    if (@_) {
        $self->{COULEUR}=shift;
        $::drawing->itemconfigure($self->{LINE}, -fill => $self->{COULEUR});
        $self->redraw;
    }
    return $self->{COULEUR};
}
sub configure {
    my $self=shift;
    my $key=uc(substr(shift, 1));
    $self->{$key}=shift;
}
sub select {
    my $self=shift;
    return $self->{SELECT};
}
sub pique {
    my $self=shift;
    return $self->{PIQUE};
}
sub inLozenge {
    my $self=shift;
    my @coord=$self->getCoords();
    my $ok=1;
    for (my $k=0; $k < @coord; $k += 2) {
        $ok=Commun->inLozenge($coord[$k], $coord[$k+1], @_) && $ok;
    }
    return $ok;
}
sub draw {
    my $self=shift;
    $self->redraw(@_);
}
sub redraw {
    my $self=shift;
    if ($Commun::view_technique) {
        $::drawing->itemconfigure($self->{LINE}, -state => "normal");
    }
    else {
        $::drawing->itemconfigure($self->{LINE}, -state => "hidden");
    }
}
sub selectionne {
    my $self=shift;
    $self->{SELECT}=1;
    $::drawing->itemconfigure($self->{LINE}, -width => 5);
    $self->redraw();
    $::sf_curve->pack();
}
sub changeCoord {
    my $self=shift;
    my @coord=@_;
    $self->{COORD}=\@coord;
    $::drawing->coords($self->{LINE}, @coord);
}
sub deselectionne {
    my $self=shift;
    if ($self->{SELECT}==1) {
        $self->{SELECT}=0;
        $::drawing->itemconfigure($self->{LINE}, -width => 3);
        $self->redraw();
    }
}
sub copyTranslate {
    my $self=shift;
    my ($move_i, $move_j)=@_;
    my @coord=$self->getCoords();
    for (my $k=0; $k < @coord; $k += 2) {
        $coord[$k]=$coord[$k]+$move_j;
        $coord[$k+1]=$coord[$k+1]+$move_i;
    }
    my $curve=Curve->new(
        -couleur => $self->color,    # fixme-m change couleur by color
    );
    $curve->coord(@coord);
}
sub print {
    my $self=shift;
    #print "print de ".$self;
    return
        "-type Curve -coord "
      . join(",", @{$self->{COORD}})
      . " -id $self->{ID} -width $self->{WIDTH} -couleur $self->{COULEUR} -smooth $self->{SMOOTH}\n";
}
sub supprime {
    my $self=shift;
    delete($liste{$self->id});
    delete($::liste{$self->{LINE}});
}
sub DESTROY {
    #print "destroy";
    my $self=shift;
    $self->undoCreate;
    $self->SUPER::DESTROY();
    $self->deselectionne;
    $::drawing->delete($self->{LINE});
    #print "destroy of " . $self;
}
1;
