package AttenuationGraph;

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

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

use Wx qw(:everything);
use Wx::Event qw(EVT_CLOSE EVT_PAINT);

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

use File::Spec;

# Parameters: title, position, size
sub new {
    my( $class ) = shift;
    # now we are calling the base class constructor
    my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2], $_[3] );
    Wx::InitAllImageHandlers();
    # This frame only displays the graph. Define a Paint event that performs
    # the drawing of the bitmap
    $this->{ALPHA_CALC} = pdl [0];

    EVT_PAINT($this, sub {
      my $png_file = File::Spec->catfile( File::Spec->curdir(), 'attenuation.png');
      # since here you are handling a paint event, you *MUST*
      # use a PaintDC
      $this->{dc} = Wx::PaintDC->new( $this );
      $this->{dc}->BeginDrawing();
      my $bmp = Wx::Bitmap->new( $png_file,wxBITMAP_TYPE_PNG);
      $this->{dc}->DrawBitmap( $bmp, 0, 0, 1 );
      $this->{dc}->EndDrawing();
    });
    $this->attenuation::Initialize; # call initialize of parent
    $this->Fit();
    return $this;
}

sub alpha_calc {
  my $this = shift;
  if (@_) { $this->{ALPHA_CALC} = pdl @_; }
  return $this->{ALPHA_CALC};
}

sub alpha_calc_elem {
  my ($this,$index,$value) = @_;
  if ($value) { $this->{ALPHA_CALC}->slice("$index") .= $value }
  return $this->{ALPHA_CALC}->slice("$index")->sclr;
}


sub Create_Graph {
  my ($this) = @_;
  # Test OS and determine path to gnuplot executable
  my $path_to_gnuplot;
  my $png_file = File::Spec->catfile( File::Spec->curdir(), 'attenuation.png');
  if ($^O =~ 'Win32') {
    $path_to_gnuplot = File::Spec->catfile( 'gnuplot','pgnuplot.exe');
  }
  else {
    $path_to_gnuplot = File::Spec->catfile(File::Spec->rootdir,'usr','bin','gnuplot');
  }
  my $tmpfile = "attenuation.txt";
  open(TMP, ">$tmpfile") || die ("Cannot create temporary file $tmpfile\n");
  # Write results to temporary file
  #printf $this->alpha_calc;
  for(my $i=0; $i<$this->f->getdim(0);$i++) {
    my $t1=$this->f_elem("$i"); # Units: MHz
    my $t2=$this->alpha_elem("$i");
    my $t3=$this->alpha_calc_elem("$i");
    print TMP ("$t1\t$t2\t$t3\n");
  }
  close TMP;
  # Plot data from temporary file
  my $PLOTCOMMAND="plot ";
  $PLOTCOMMAND= "$PLOTCOMMAND" . "\'$tmpfile\' using 1:2 title \'measurement\'  with lines linestyle  1, \'$tmpfile\' using 1:3 title \'calculation\' with lines linestyle  2";

  # pipe data to gnuplot
  open ( GNUPLOT, ">attenuation_commands");
  #| sed 's/0 1 0/0 0.752941 0/;s/0 0 1/0 0 0.752941/;s/1 0 1/0.752941 0 0.752941/;s/0 1 1/0.752941 0.752941 0/;s/1 1 0/0 0.752941 0.752941/'");

  print GNUPLOT << "gnuplot_Commands";
  set output '$png_file'
  set terminal png
  #eps enhance color solid "Times-Roman" 11
  set size 0.857, 0.680
  set key top left
  set key spacing 1.3
  set missing "-"
  set ytics nomirror
  set xtics nomirror
  #set logscale x
  set xlabel "f [MHz]"
  set ylabel "alpha [dB\/cm\/MHz]"
  set style line 1 linetype 1 pointtype 9 pointsize 1
  set style line 2 linetype 2 lw 1.5 pointtype 7 pointsize 1
  set style line 3 linetype 3 lw 1.5 pointtype 9 pointsize 1
  $PLOTCOMMAND
gnuplot_Commands

  close (GNUPLOT);
  my $a = system("$path_to_gnuplot attenuation_commands");
  # Set Window size to graph size
  my $Image = Wx::Image->new($png_file,wxBITMAP_TYPE_PNG);
  $this->SetSize(-1,-1,$Image->GetWidth,$Image->GetHeight);
  $this->Show(1);
}

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


1;
