# +-----------------------------------------------------------------------+
# | 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::wxPropertyGridPanel;
use strict;
use Wx qw/
             wxDefaultSize
             wxDefaultPosition
         /;
use Wx::Locale qw/:default/;
use base qw/Wx::Dialog Uploader::Object2/;

use Wx::Event qw/
   EVT_GRID_CMD_CELL_CHANGE
/;
use Data::Dumper;
__PACKAGE__->mk_accessors(     
    qw/
        renderer
        editor
        properties
        grid
      /
);



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( undef, -1, "PropertyGrid", wxDefaultPosition, wxDefaultSize );
    $self->grid(
        Wx::Grid->new($self, -1, wxDefaultPosition, wxDefaultSize)
    );

    $self->renderer(
        {
            Bool     => sub { Wx::GridCellBoolRenderer->new },
            Number   => sub { Wx::GridCellNumberRenderer->new },
            Float    => sub { Wx::GridCellFloatRenderer->new(-1, 2) },
            LongText => sub { Wx::GridCellAutoWrapStringRenderer->new },
        }
    );

    $self->editor(
        {
            Bool     => sub { Wx::GridCellBoolEditor->new },
            Number   => sub { Wx::GridCellNumberEditor->new },
            Float    => sub { Wx::GridCellFloatEditor->new(-1, 2) },
            Choice   => sub { Wx::GridCellChoiceEditor->new(@_) },
            LongText => sub { Wx::GridCellAutoWrapStringEditor->new },
        }
    );


    $self->properties(
        $params->{properties}
    );
    
    $self->Initialize;
      
    EVT_GRID_CMD_CELL_CHANGE( $self, $self->grid, \&OnCellChanged );
    $self;    
}

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


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

  $self->grid->SetRowLabelSize(0);
  $self->grid->SetColLabelSize(0);
  $self->grid->SetColSize( 0, 200 );
  $self->grid->SetColSize( 1, 200 );
  
  $self->_init_property_grid();

}

sub Refresh {
    my ( $self ) = @_;
    
    $self->grid->ClearGrid;

    my $row = 0;
    map {
    	eval {

    	    $self->grid->SetCellValue(
    	        $row,
    	        0,
    	        $_->{label},
    	    );	


    	    $self->grid->SetCellValue(
    	        $row,
    	        1,
    	        exists $_->{choice} ? gettext($_->{value}->()) : $_->{value}->(),
    	    );	



    	};
    	$row++;
    }@{$self->properties};
	
}

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

    my $row = 0;
    #print Dumper $self->properties;
    map {
    	eval {
    	    $self->grid->SetCellEditor(
    	        $row,
    	        1,
                &{
                    sub { $self->editor->{
                        $_->{type}
                    }->( $_->{choice} ) },
                },    	    
    	    ) if $self->editor->{$_->{type}};

    	    $self->grid->SetCellRenderer(
    	        $row,
    	        1,
                &{
                    $self->renderer->{
                        $_->{type}
                    }
                },    	    
    	    ) if $self->renderer->{$_->{type}};

    	    $self->grid->SetReadOnly(
    	        $row,
    	        0,
            );

    	    $self->grid->SetCellValue(
    	        $row,
    	        0,
    	        $_->{label},
    	    );	

    	    $self->grid->SetCellValue(
    	        $row,
    	        1,
    	        exists $_->{choice} ? gettext($_->{value}->()) : $_->{value}->(),
    	    );	

    	    $self->grid->SetReadOnly(
    	        $row,
    	        1,
    	    ) if $_->{readonly};	


    	};
    	$row++;
    }@{$self->properties};
	
}

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

    my( $row, $col ) = ( 
        $event->GetRow,
        $event->GetCol,
    );
    
    my $value = $self->grid->GetCellValue(
        $row,
        $col,
    );

    # if we need to preprocess the value
    $value = $self->properties->[$row]->{pre_process}->($value) if exists
        $self->properties->[$row]->{pre_process}; 

    # set property
    $self->properties->[$row]->{value}->($value);
printf("property value %s\n",     $self->properties->[$row]->{value}->());
    
    # execute the corresponding frame callback
    $self->properties->[$row]->{frame_callback}->() if exists
        $self->properties->[$row]->{frame_callback}; 
}


1;