# +-----------------------------------------------------------------------+
# | pLoader - a Perl photo uploader for Piwigo                            |
# +-----------------------------------------------------------------------+
# | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
# +-----------------------------------------------------------------------+
# | This program 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                                          |
# |                                                                       |
# | This program 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 this program; if not, write to the Free Software           |
# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
# | USA.                                                                  |
# +-----------------------------------------------------------------------+
package Uploader::GUI::wxChoiceGridPanel;
use strict;
use Wx qw/
             wxDefaultSize
             wxDefaultPosition
         /;

use base qw/Wx::Grid Uploader::Object2/;

use Wx::Event qw/
   EVT_GRID_CMD_CELL_CHANGE
   EVT_GRID_CELL_LEFT_CLICK
/;
use Data::Dumper;

__PACKAGE__->mk_accessors(     
    qw/
        frame_callbacks
        renderer
        editor
        choices
        selection
        id_lookup
        row_lookup
      /
);



sub new {
    my ($this, $params) = @_;
    #on recupere le nom de la classe en fonction du type d'appel de la méthode.
    my $class = ref($this) || $this;


    my $self = $class->SUPER::new( $params->{parentwnd}, -1, wxDefaultPosition, wxDefaultSize );

    $self->id_lookup(
        {}
    );

    $self->row_lookup(
        {}
    );

    $self->choices(
        $params->{choices}
    );

    $self->selection(
        $params->{selection}
    );

    
    # a frame callback for each property
    $self->frame_callbacks(
        $params->{frame_callbacks}
    );
    
    $self->Initialize;
      
    EVT_GRID_CMD_CELL_CHANGE( $self, $self, \&OnCellChanged );
    EVT_GRID_CELL_LEFT_CLICK( $self, \&OnCellLeftClick );
    $self;    
}

sub Initialize {
    my ( $self ) = @_;	


  $self->CreateGrid( scalar @{$self->choices}, 2 );
  

  $self->SetRowLabelSize(0);
  $self->SetColLabelSize(0);
  $self->SetColSize( 0, 200 );
  $self->SetColSize( 1, 20 );
  
  $self->_init_choices;

}

sub Refresh {
    my ( $self ) = @_;
    
    $self->ClearGrid;
    $self->id_lookup({});
    $self->row_lookup({});

    my $row = 0;
    map {
    	$self->SetCellValue(
    	    $row,
    	    0,
    	    $_->{name},
    	) if defined $_;	

        $self->id_lookup->{$row} = $_->{id};
        $self->row_lookup->{$_->{id}} = $row;
    	$row++;
    }@{$self->choices};

    $self->SetSelection;
	
}

sub _init_choices {
    my ( $self ) = @_;

    my $row = 0;
    #print Dumper $self->properties;
    map {
    	eval {
    	    $self->SetReadOnly(
    	        $row,
    	        0,
            );

    	    $self->SetCellValue(
    	        $row,
    	        0,
    	        $_->{name},
    	    ) if defined $_;	

    	    $self->SetCellEditor(
    	        $row,
    	        1,
                &{
                    sub { Wx::GridCellBoolEditor->new },
                },    	    
    	    );

    	    $self->SetCellRenderer(
    	        $row,
    	        1,
                &{
                    sub { Wx::GridCellBoolRenderer->new }
                },    	    
    	    );

    	};
        $self->id_lookup->{$row} = $_->{id};
        $self->row_lookup->{$_->{id}} = $row;
    	$row++;
    }@{$self->choices};


}

sub OnCellChanged {
    my ( $self, $event ) = @_;

    $self->selection->(
        $self->GetSelection
    );    
}

sub OnCellLeftClick {
    my ( $self, $event ) = @_;

    my ( $row, $col ) = ( $event->GetRow, $event->GetCol );
    
    # only col 0
    if(!$event->GetCol){
        $self->SetCellValue(
            $event->GetRow,
            1,
            !$self->GetCellValue(
                $event->GetRow,
                1,
            ),
        );
    }

    $self->selection->(
        $self->GetSelection
    );    
    $event->Skip;
}

sub SetSelection {
    my ( $self ) = @_;

    my $selection = $self->selection->()||[];

    map {
    	$self->SetCellValue(
    	    $self->row_lookup->{$_},
    	    1,
    	    1,
    	) if exists $self->row_lookup->{$_} ;	
    }
    @{$selection};	
}



sub GetSelection {
    my ( $self ) = @_;

    my $row = 0;
    my $selection = [];
    map {
        my $selected = $self->GetCellValue(
            $row,
            1,
        );
            
        if($selected){
            push @$selection, $self->id_lookup->{$row};	
        }
    	$row++;
    }@{$self->choices};
    
    $selection;	
}
1;