package App::POS::Hardware::CustomerDisplays::LED8;

use strict;
use warnings;
use Time::HiRes qw( usleep );
use Data::Dumper;

use base qw( App::POS::Hardware::CustomerDisplays );

sub type { "SERIAL" }

sub init {
  my $self = shift;
}

sub write {
  my $self = shift;
  my $str = shift || undef;
  return unless defined $str;

  my $port = $self->serial_obj();
  $port->close();

  my $info = $self->{info};
  $info->{TextVoided} //= "";

  my @t = split /#/, $str;
  #print STDERR Dumper(\@t);
  return if uc($t[0]) eq 'CUSTOM' || uc($t[0]) eq 'VOID';

  # product
  if (scalar @t == 3) {
    return if $t[2] eq 'is_nif';

    my @v = split /,/, $t[1];
    my $product_total = $v[0];

    system("/bin/stty 2400 cs8 -cstopb -parenb -crtscts -F $info->{Device}");
    open(F, "> $info->{Device}");
    binmode(F);
    print F chr(12);
    print F chr(2).chr(76). "1   "; # product
    #print F chr(2).chr(76). " 1  "; # total
    #print F chr(2).chr(76). "   1"; # change
    print F chr(27).chr(81).chr(65) . $product_total . chr(13);
    close(F);
  }
  # total or change
  elsif (scalar @t == 2) {
    system("/bin/stty 2400 cs8 -cstopb -parenb -crtscts -F $info->{Device}");
    open(F, "> $info->{Device}");
    binmode(F);
    print F chr(12);
    if ($t[1] == 0 || $t[0] == $t[1]) {
      print F chr(2).chr(76). " 1  "; # total
      print F chr(27).chr(81).chr(65) . $t[0] . chr(13);
      close(F);
    }
    else {
      print F chr(2).chr(76). "   1"; # change
      print F chr(27).chr(81).chr(65) . $t[1] . chr(13);
    }
    close(F);
  }
}

sub text_pre_process {
  my $self = shift;
  my( $string, $nchars, $nchars_l2 ) = @_;

  return ( $string, $nchars, $nchars_l2 ) unless defined $string && $string;

  my @toks = split /#/, $string;

  # validate if we're allowed to show customer nifs on the customer display
  if (scalar @toks == 3 && $toks[2] eq 'is_nif') {
    $string = "";
    return ( $string, $nchars, $nchars_l2 );
  }

  if (scalar @toks == 4) {
    $string = "";
    return ( $string, $nchars, $nchars_l2 );
  }
  #
  # product registration
  #
  elsif (scalar @toks == 3) {
    $string = "";
    return ( $string, $nchars, $nchars_l2 );
  }
  #
  # idle
  #
  elsif (scalar @toks == 1) {
    $string = "";
    return ( $string, $nchars, $nchars_l2 );
  }
  #
  # void
  #
  elsif (scalar @toks == 2) {
    $self->info->{TextVoided}      ||= "";
    $self->info->{TextCheckTotal}  ||= "TOTAL:";
    $self->info->{TextCheckChange} ||= "CHANGE:";

    $toks[0] ||= 0;
    $toks[1] ||= 0;

    if ($toks[0] eq 'VOID') {
      $string = "";
      return ( $string, $nchars, $nchars_l2 );
    }
    elsif ($toks[0] eq 'CUSTOM') {
      $string = "";
      return ( $string, $nchars, $nchars_l2 );
    }
    else {
      $self->info->{TextCheckTotal} = $toks[0];
      $self->info->{TextCheckChange} = $toks[1];
      return ( $string, $nchars, $nchars_l2 );
    }
  }
  else {
    print STDERR "Unknown string to handle: $string\n";
  }

  $string = "";
  return ( $string, $nchars, $nchars_l2 );
}


1;
