#!/usr/bin/env perl

use strict;
use warnings;
#no warnings qw(redefine);
use Pod::Usage;
use Getopt::Long;

eval 'use Term::ANSIColor ()';
eval { require Win32::Console::ANSI } if (($^O =~ /MSWin32/) and ($ENV{TERM} eq 'dumb'));

use vars qw($nocolor);
$nocolor = 0;
my $result  = GetOptions ('nocolor|n' => \$nocolor,
			  'help|h' => sub{ pod2usage(1); exit 0},);

pod2usage(1) && exit 0 if not $ARGV[0] or not $ARGV[1];

use Chemistry::Elements qw(get_Z get_symbol get_name);
use Xray::Absorption;
my $element = ucfirst( lc($ARGV[0]) );
die b("$element is not an element", 'red').$/ if not get_Z($element);
my $edge = lc($ARGV[1]);
my $energy = Xray::Absorption->get_energy($element, $edge);

my $which = ($edge =~ m{\A(?:k|l[1-3]?|m[1-5]?|n[1-7]|o[1-5]|p[1-3])\z}) ? 'edge' : 'line';

if ($which eq 'edge') {
  if (($edge eq 'l') or ($edge eq 'm')) {
    my ($ed, $energy) = (qw{}, qw{});
    my $n = ($edge eq 'l') ? 3 : 5;
    foreach my $i (reverse(1 .. $n)) {
      $ed .= b(uc($edge).$i, 'green') . '/';
      $energy .= b(Xray::Absorption->get_energy($element, $edge.$i), 'yellow') . '/';
    };
    chop $ed;
    chop $energy;
    printf "\n%s (%s, #%d) %s %s energies : %s eV\n\n",
      b(get_name($element), 'cyan'), get_symbol($element), get_Z($element),
      $ed, $which, $energy;
  } else {
    printf "\n%s (%s, #%d) %s %s energy = %s eV\n\n",
      b(get_name($element), 'cyan'), get_symbol($element), get_Z($element),
      b(ucfirst($edge), 'green'), $which,
      b($energy, 'yellow');
  };
} else {
  die b("\n$edge is not an edge or line symbol\n", 'red').$/ if not Xray::Absorption->get_IUPAC($edge);
  printf "\n%s (%s, #%d) %s (%s) %s energy = %s eV\n\n",
    b(get_name($element), 'cyan'), get_symbol($element), get_Z($element),
      b(Xray::Absorption->get_Siegbahn_full($edge), 'green'), Xray::Absorption->get_IUPAC($edge), $which,
	b($energy, 'yellow');
};

sub b {
  my ($message, $color) = @_;
  my $string = ($nocolor) ? $message : Term::ANSIColor::colored($message, "bold $color");
  return $string;
};


=head1 NAME

denergy - Quick display of edge and line energies

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

   denergy [options] element edge_or_line

The element can be the 1- or 2-letter symbol, the Z number or the
English language name.  The edge or line can be an edge symbol, a
Siegbahn line symbol, or an IUPAC line symbol.

Examples:

   deneregy Cu K
    ==>  Copper (Cu, #29) K edge energy = 8979 eV

   denergy Pt La1
    ==> Platinum (Pt, #78) Lalpha1 (L3-M5) line energy = 9442 eV

=head1 OPTIONS

  -n   suppress colored output
  -h   print this message

=head1 BUGS and SHORTCOMINGS

=over 4

=item *

Leaving of the edge/line argument should print a table of edge and
line energies.

=back

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel (L<http://bruceravel.github.io/home>)

http://cars.uchicago.edu/ifeffit/Demeter

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

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.

=cut
