package ResultWindow;

use strict;
use warnings;
use vars qw(@ISA);

@ISA=qw(Wx::Frame size_distribution);

use Wx qw(:everything);
use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
use Wx::Grid;
use Wx::DND;

use PDL;
use PDL::Core;
use PDL::Math;
use PDL::Primitive;
use PDL::Complex;

my( $Copy_to_Clipboard_Btn_ID) = ( 1);

# Parameters: title, position, size
sub new {
  #my( $this ) = shift;
  #my $class = ref($this) || $this;
  ## Call constructor of base class:
  #size_distribution->new(); # call constructor of parent
  #bless($this, $class);

  my( $class ) = shift;
  # now we are calling the base class constructor
  my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2], $_[3] );
  size_distribution->new($this); # call constructor of parent

  EVT_CLOSE( $this, \&OnClose );
  # Add a panel for better optics
  $this->{toppanel} = Wx::Panel->new($this,-1);
  # tell we want automatic layout
  $this->{toppanel}->SetAutoLayout(1);
  # Create the layout: top sizer
  $this->{toppanel}->{topsizer} = Wx::BoxSizer->new( wxVERTICAL );
  #------------------------
  #------------------------
  # Sizer for measurement values
  #my $box=Wx::StaticBox->new(
  #               $this,
  #               -1,
  #               "Result",Wx::Point->new(-1,-1),Wx::Size->new(100,100));
  #my ($table_sizer) = Wx::StaticBoxSizer->new($box,wxHORIZONTAL);
  # Create a "Copy to Clipboard"-Button
  my ($Copy_to_Clipboard_Btn) =Wx::Button->new( $this->{toppanel},
               $Copy_to_Clipboard_Btn_ID,
               "Copy to Clipboard" );
  # Assign "Click" event
  EVT_BUTTON($this,          # Object to bind to
            $Copy_to_Clipboard_Btn,         # ButtonID
            \&Copy_to_Clipboard_Btn_Clicked # Subroutine to execute             );
           );
  $this->{toppanel}->{topsizer}->Add($Copy_to_Clipboard_Btn,
                0, # make horizontally unstretchable
                wxEXPAND|wxALL, # make border all around (implicit top alignment)
                5 ); # set border width to 5

  $this->{table_sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
  #$topsizer->Add( $table_sizer,
  #              1,
  #              wxEXPAND|wxALL,
  #              10);

  # Create a grid
  #my ($grid) = Wx::Grid->new( $this,
  $this->{grid} = Wx::Grid->new( $this->{toppanel},
                      -1,
                      wxDefaultPosition,
                      Wx::Size->new(300,500));
  # Adjust number of Rows/Columns in grid
  $this->{grid}->CreateGrid( $_[4], 2 );
  # Set Column Label
  $this->{grid}->SetColLabelValue(0,"xm [µm]");
  $this->{grid}->SetColLabelValue(1,"Q");
  # Set grid sizes
  $this->{grid}->AutoSizeRows(1);
  #$this->{grid}->Enable(0);
  # Add grid to grid sizer
  $this->{table_sizer}->Add( $this->{grid},
                1,
                wxEXPAND|wxALL,
                10);
  $this->{toppanel}->{topsizer}->Add($this->{table_sizer},
                1,
                wxEXPAND|wxALL,
                10);

  $this->SetSizer( $this->{toppanel}->{topsizer} );
  $this->SetAutoLayout(1);
  $this->{toppanel}->{topsizer}->Fit($this->{toppanel});
  $this->Fit();

  return $this;
}

sub fill_x_values {
  my( $this, $event) = @_;
  for(my $i=0;$i<$this->{XO}->getdim(0);$i++){
    $this->{grid}->SetCellValue($i,0, $this->xo_elem($i)*1e6);
    $this->{grid}->SetReadOnly($i,0,1);
    #$this->{grid}->SetRowFormatFloat($i,4,2);
    #$this->{grid}->SetColLabelValue($i, $this->{inversion}->f_elem($i));
  }
  # Adjust column width
  #$this->{grid}->AutoSizeColumns(1);
}

sub fill_Q_values {
  my( $this, $event) = @_;
  for(my $i=0;$i<$this->{XO}->getdim(0);$i++){
    $this->{grid}->SetCellValue($i,1, $this->Q_elem($i));
    $this->{grid}->SetReadOnly($i,1,1);
  }

}

sub Copy_to_Clipboard_Btn_Clicked {
  my( $this, $event) = @_;
  my $ClipboardText="xo [µm]\t Q [-]\n";
  if (wxTheClipboard->Open) {
    for(my $i=0;$i<$this->{XO}->getdim(0);$i++){
      $ClipboardText=$ClipboardText . $this->{grid}->GetCellValue($i,0)
       . "\t" . $this->{grid}->GetCellValue($i,1) . "\n";
    }
    wxTheClipboard->SetData(Wx::TextDataObject->new($ClipboardText));
    wxTheClipboard->Close;
  }
}

sub OnClose {
  my( $this, $event ) = @_;
  #Wx::MessageBox( 'OnClose: Closing', 'OnClose', wxOK, $this );
  #$event->Skip();
  $this->Destroy();
}




1;
