#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/>.
#
# fixme-m change in Pin.pm
# fixme-m possibility to not print all pin (pique)
use strict;    # rewrite regler le problème des variable global
use warnings;
use vars qw($r $epingle $width_line $drawing $sf_pin $view_parenthese );
$r=12;            # rayon des epingle
$epingle=2;       # grosseur de trou
$width_line=3;    # epaisseur des lignes
$drawing="";      # surface de dssin
$view_parenthese=0;
my $sf_pin;
package Epingle;
use Commun;
use Data::Dumper;
our @ISA=qw( Commun );
our %liste;
our %champ;
sub getListe {    # rewrite : try to put in Commun.pm if possible.
    my $self=shift;
    return %liste;
}
sub configureAll {
    foreach my $liste (values %liste) {
        if ($liste->select) {
            $liste->configure(@_);
        }
    }
    return;
}
sub configure {    # todo put this sub in Commun.pm
    my $self=shift;
    my $key=uc(substr(shift, 1));
    $self->{$key}=shift;
    print "c $key $self->{$key} ";
    #    my $self=shift;
    #    my %param=@_;    # rewrite use %param
    #    foreach my $key (keys %param) {    # todo check for multiple parameter
    #        my $newKey=uc(substr($key, 1));
    #        $self->{$newKey}=$param{$key};
    #    }
}
sub round {
    my $number=shift;
    return (int($number+.5*($number <=> 0)));
}
sub find {
    my $i=shift;
    my $j=shift;
    return ($champ{$i}{$j});
}
sub findOrCreate {
    my $i=round(shift);
    my $j=round(shift);
    my $color=shift;
    my $epingle=Epingle::find($i, $j);
    if ($epingle) {
        if ($color) {$epingle->color($color);}
    }
    else {
        if (!$color) {$color="red";}
        $epingle=Epingle->new(-i => $i, -j => $j, -color => $color);
    }
    return ($epingle);
}
sub new {
    my $classe=shift;
    my $self=$classe->SUPER::new();
    my %param=@_;
    if ($param{"-id"}) {    # rewrite move in Commun
        $self->{ID}=$param{"-id"};
        Commun::set_id($self->{ID});
    }
    $self->{I}=$param{"-i"};
    $self->{J}=$param{"-j"};
    $self->{COULEUR}=$param{"-color"};
    $self->{TDI}=defined($param{"-tdi"})     ? $param{"-tdi"}   : 2;
    $self->{TDJ}=defined($param{"-tdj"})     ? $param{"-tdj"}   : 12;
    $self->{RAYON}=defined($param{"-rayon"}) ? $param{"-rayon"} : 12;
    if ($self->{RAYON} < 1) {$self->{RAYON}=12;}
    $self->{INTERIEUR}="white";
    $self->{WIDTH}=$main::width_line;
    $self->{SELECT}=0;
    $self->{NBRE}=0;
    $self->{NBRE_FIL}=0;
    $self->{NUMBER}=defined($param{"-number"}) ? $param{"-number"} : ".";
    bless($self, $classe);
    $self->draw();
    $liste{$self->id}=$self;
    $champ{$self->i}{$self->j}=$self;
    $self->undoDelete;
    #print Dumper $self;
    return $self;
}
sub i {
    my $self=shift;
    if (@_) {$self->{I}=shift}
    return $self->{I};
}
sub tdi {
    my $self=shift;
    if (@_) {$self->{TDI}=shift}
    return $self->{TDI};
}
sub min_i {
    my $self=shift;
    return $self->{I};
}
sub max_i {
    my $self=shift;
    return $self->{I};
}
sub j {
    my $self=shift;
    if (@_) {$self->{J}=shift}
    return $self->{J};
}
sub number {
    my $self=shift;
    if (@_) {$self->{NUMBER}=shift}
    return $self->{NUMBER};
}
sub tdj {
    my $self=shift;
    if (@_) {$self->{TDJ}=shift}
    return $self->{TDJ};
}
sub min_j {
    my $self=shift;
    return $self->{J};
}
sub max_j {
    my $self=shift;
    return $self->{J};
}
sub color {
    # apply color if param (and change line attach to pin) or return color
    my $self=shift;
    if (@_) {
        $self->{COULEUR}=shift;
        $self->redraw;
        foreach my $line (values %Line::liste) {
            if ($line->o eq $self) {$line->redraw();}
            if ($line->e eq $self) {$line->redraw();}
        }
    }
    return $self->{COULEUR};
}
sub r {
    my $self=shift;
    if   (@_)                      {$self->{RAYON}=shift}
    if   ($Commun::view_technique) {return $self->{RAYON};}
    else                           {return 5;}                # pique
}
sub width {
    my $self=shift;
    if (@_) {$self->{WIDTH}=shift}
    $::drawing->itemconfigure($self->{DRAW}, -width => $self->{WIDTH});
    return $self->{WIDTH};
}
sub nbre {
    my $self=shift;
    if (@_) {$self->{NBRE}=shift}
    return $self->{NBRE};
}
sub nbre_fil {
    my $self=shift;
    if (@_) {$self->{NBRE_FIL}=shift}
    return $self->{NBRE_FIL};
}
sub id_draw {
    my $self=shift;
    return $self->{DRAW};
}
sub id_text {
    my $self=shift;
    return $self->{TEXT};
}
sub incO {
    my $self=shift;
    $self->{NBRE}++;
    $self->{NBRE_FIL}++;
    $self->redraw;
}
sub decO {
    my $self=shift;
    $self->{NBRE}--;
    $self->{NBRE_FIL}--;
    $self->redraw;
}
sub incE {
    my $self=shift;
    $self->{NBRE}--;
    $self->{NBRE_FIL}++;
    $self->redraw;
}
sub decE {
    my $self=shift;
    $self->{NBRE}++;
    $self->{NBRE_FIL}--;
    $self->redraw;
}
sub inLozenge {
    my $self=shift;
    return Commun->inLozenge($self->j, $self->i, @_);
}
sub normal {
    my $self=shift;
    $::drawing->itemconfigure($self->{DRAW}, -state => "normal");
    $::drawing->itemconfigure($self->{TEXT}, -state => "normal");
}
sub hidden {
    my $self=shift;
    $::drawing->itemconfigure($self->{DRAW}, -state => "hidden");
    $::drawing->itemconfigure($self->{TEXT}, -state => "hidden");
}
sub redraw {
    my $self=shift;
    my $i=$self->{I};
    my $j=$self->{J};
    my $color=$self->{COULEUR};
    my $r=$self->{RAYON};
    my $id=$self->{ID};
    $::drawing->coords($self->{DRAW}, $j-$r, $i-$r, $j+$r, $i+$r);
    $::drawing->itemconfigure($self->{DRAW}, -outline => $self->{COULEUR});
    $::drawing->coords(
        $self->{EPI},      $j-$main::epingle, $i-$main::epingle,
        $j+$main::epingle, $i+$main::epingle
    );
    $::drawing->coords($self->{TEXT}, $j+$self->{TDJ}, $i+$self->{TDI});
    my $text;
    if    ($self->{NBRE} < 0) {$text=$self->{NBRE};}
    elsif ($self->{NBRE} > 0) {$text="+" . $self->{NBRE};}

    if (   ($self->{NBRE_FIL} != 4)
        && ($self->{NBRE_FIL} != 0)
        && ($self->{NBRE_FIL} != 6)
        && ($::view_parenthese==1)
        && ($self->{NBRE_FIL}))
    {
        $text .= "(" . $self->{NBRE_FIL} . ")";
    }
    $::drawing->itemconfigure($self->{TEXT}, -text  => $text);
    $::drawing->itemconfigure($self->{DRAW}, -state => "hidden");
    $::drawing->itemconfigure($self->{TEXT}, -state => "hidden");
    $::drawing->itemconfigure($self->{EPI},  -state => "hidden");
    if ($Commun::view_technique) {
        $::drawing->itemconfigure($self->{DRAW}, -state => "normal");
        $::drawing->itemconfigure($self->{TEXT}, -state => "normal");
        $::drawing->itemconfigure($self->{EPI},  -state => "normal");
    }
    if ($Commun::view_pique) {
        $::drawing->itemconfigure($self->{EPI}, -state => "normal");
    }
    if ($self->{NUMBER} ne ".") {
        $::drawing->itemconfigure($self->{TEXT}, -text => $self->{NUMBER});
    }
    if ($self->{NUMBER} eq "..") {
        $::drawing->itemconfigure($self->{TEXT}, -text => "");
    }
    return;
}
sub draw {
    my $self=shift;
    my $i=$self->{I};
    my $j=$self->{J};
    my $color=$self->{COULEUR};
    my $r=$self->{RAYON};
    my $id=$self->{ID};
    $self->{DRAW}=$::drawing->createOval(
        $j-$r,
        $i-$r,
        $j+$r,
        $i+$r,
        -fill    => "white",
        -outline => $color,
        -width   => $main::width_line,
        -tags    => ["epingle", "id=$id"]
    );
    $::liste{$self->{DRAW}}=$self;
    $self->{EPI}=$main::drawing->createOval(
        $j-$main::epingle,
        $i-$main::epingle,
        $j+$main::epingle,
        $i+$main::epingle,
        -fill  => "black",
        -width => 0,
        -tags  => ["mini", "id=$id"]
    );
    $::liste{$self->{EPI}}=$self;
    my $text="";
    if    ($self->{NBRE} != 0) {$text=$self->{NBRE};}
    elsif ($self->{NBRE} > 0)  {$text="+" . $self->{NBRE};}

    if (   ($self->{NBRE_FIL} != 4)
        && ($self->{NBRE_FIL} != 0)
        && ($self->{NBRE_FIL} != 6)
        && ($self->{NBRE_FIL}))
    {
        $text .= "(" . $self->{NBRE_FIL} . ")";
    }
    $self->{TEXT}=$main::drawing->createText(
        $j+$self->{TDJ}, $i+$self->{TDI},
        -text => "$text",
        -font => "20",
        -tags => ["text", "id=$id"]
    );
    if ($self->{NUMBER} ne ".") {
        $::drawing->itemconfigure($self->{TEXT}, -text => $self->{NUMBER});
    }
    if ($self->{NUMBER} eq "..") {
        $::drawing->itemconfigure($self->{TEXT}, -text => "");
    }
    $::liste{$self->{TEXT}}=$self;
    return;
}
sub select {
    my $self=shift;
    return $self->{SELECT};
}
sub selectionne {
    my $self=shift;
    $self->{SELECT}=1;
    $self->width(5);    # rewrite
    $::sf_pin->pack();
}
sub deselectionne {
    my $self=shift;
    if ($self->{SELECT}==1) {
        $self->{SELECT}=0;
        $self->width(3);    # rewrite
    }
}
sub copyTranslate {
    my $self=shift;
    my ($move_i, $move_j)=@_;
    return findOrCreate($self->i+$move_i, $self->j+$move_j, $self->color);
}
sub fusion {
    my $class=shift;
    my ($pin1, $pin2)=@_;
    foreach my $liste (values %Line::liste) {
        if ($liste->o eq $pin2) {
            $liste->o($pin1);
            $pin1->incO;    # rewrite put in "sub o" ?
            $liste->redraw();
        }
        elsif ($liste->e eq $pin2) {
            $liste->e($pin1);
            $pin1->incE;
            $liste->redraw();
        }
    }
    $pin2->supprime;
}
sub print {
    my $self=shift;
    return
"-type Epingle -id $self->{ID} -i $self->{I} -j $self->{J} -color $self->{COULEUR} -rayon $self->{RAYON} -tdi $self->{TDI} -tdj $self->{TDJ} -number $self->{NUMBER}\n";
#    return
#"-type=>Epingle -id=>$self->{ID} -i=>$self->{I} -j=>$self->{J} -color=>$self->{COULEUR} -rayon=>$self->{RAYON} -tdi=>$self->{TDI} -tdj=>$self->{TDJ}\n";
}
sub supprime {
    my $self=shift;
    foreach my $liste (values %Line::liste) {
        if ($liste->o eq $self) {
            $liste->supprime();
        }
        elsif ($liste->e eq $self) {
            $liste->supprime();
        }
    }
    delete($::liste{$self->{DRAW}});
    delete($::liste{$self->{EPI}});
    delete($::liste{$self->{TEXT}});
    delete($liste{$self->id});
    delete($champ{$self->i}{$self->j});
}
sub DESTROY {
    my $self=shift;
    $self->undoCreate;
    $self->SUPER::DESTROY();
    $::drawing->delete($self->{DRAW});
    $::drawing->delete($self->{TEXT});
    $::drawing->delete($self->{EPI});
    #print "destroy de".$self;
}
1;
