#!/usr/bin/perl
# ************************************************************************* 
# Copyright (c) 2014, SUSE LLC
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 
# 3. Neither the name of SUSE LLC nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ************************************************************************* 
#
# Dochazka CLI script
#
use 5.012;
use strict;
use warnings;

use App::CELL qw( $CELL $log $site $meta );
use App::Dochazka::CLI::HTTP qw( send_req );
use App::Dochazka::CLI::Parser;
use App::Dochazka::Model::Employee;
use Data::Dumper;
use File::ShareDir;
use Getopt::Long 2.32;
use Log::Any::Adapter;
use Pod::Usage;
use Term::ReadLine;
use Time::Piece;
use Time::Seconds;
use Try::Tiny;

=head1 NAME

dochazka-cli - Command-line client for Dochazka Attendance & Time Tracking System

=head1 VERSION

Version 0.021

=cut

our $VERSION = '0.021';


=head1 SYNOPSIS

Dochazka command line client.

    bash$ dochazka-cli
    Dochazka(2014-08-08) demo>



=head1 DESCRIPTION

This is the Dochazka command line client.


=cut

local $Data::Dumper::Terse = 1;

my $_t = localtime;
my $today = $_t->ymd;
my $current_date = $today;
my $current_emp = App::Dochazka::Model::Employee->spawn;
my $current_priv;

#
# logger initialization routine
#
sub init_logger {
    unlink $site->DOCHAZKA_CLI_LOG_FILE if $site->DOCHAZKA_CLI_LOG_FILE_RESET;
    Log::Any::Adapter->set('File', $site->DOCHAZKA_CLI_LOG_FILE );
    $log->init( ident => 'dochazka-cli', debug_mode => 1 );
    $log->debug( 'Logger initialized' );
}

#
# CLI client initialization routine: might die
#
sub init_cli_client {
    my ( $user, $password ) = @_;

    # always load the App::Dochazka::CLI distro sharedir
    my $target = File::ShareDir::dist_dir('App-Dochazka-CLI');
    my $status = $CELL->load( sitedir => $target );
    die $status->text unless $status->ok;

    $status = $CELL->load( sitedir => '/etc/dochazka-cli', debug_mode => 1 );
    die $status->text unless $status->ok;

    init_logger(); 

    # initialize the LWP::UserAgent object
    App::Dochazka::CLI::HTTP::init_ua();

    # prompt for nick if none provided in site configuration
    #if ( ! $site->DOCHAZKA_REST_LOGIN_NICK ) {
    #    print "Server auth nick:     ";
    #    $meta->set( 'CURRENT_EMPLOYEE_NICK', <> );
    #}
    $meta->set( 'CURRENT_EMPLOYEE_NICK', $user );

    # prompt for password if necessary
    if ( $user ne 'demo' and $password eq 'demo' ) {
        print "Server auth password: ";
        my $input = <STDIN>;
        chomp( $input ); 
        $meta->set( 'CURRENT_EMPLOYEE_PASSWORD', $input );
        print "Password set to " . $meta->CURRENT_EMPLOYEE_PASSWORD . "\n";
    } else {
        $meta->set( 'CURRENT_EMPLOYEE_PASSWORD', $password );
    }

    # get info about us
    $status = send_req( 'GET', '/employee/current/priv' );
    return $status unless $status->ok;
    $current_emp->reset( %{ $status->payload->{'current_emp'} } );
    $current_priv = $status->payload->{'priv'};
    return $CELL->status_ok;
}

#
# get prompt
#
sub get_prompt {
    return "Dochazka($current_date) " . $current_emp->nick . " " . uc $current_priv . "> ";
}


# -------------------------------------------------------------------------
# main
# -------------------------------------------------------------------------

# process command-line options
my $help = 0;
my $user = 'demo';
my $password = 'demo';
GetOptions( 'help|?' => \$help, 'user|u=s' => \$user, 'password|p=s' => \$password );
pod2usage(1) if $help;
if ( $user eq 'root' and $password eq 'demo' ) {
    # use default root password instead of demo
    $password = 'immutable';
}

# initialize 
my $status;

if ( ( my $status = init_cli_client($user, $password) )->not_ok ) {
    print '(' . $status->level . ') ' . $status->text . "\n";
    exit;
} else {
    print "Server is alive\n";
}

my $term = new Term::ReadLine 'dochazka-cli';

my $cmd;
while ( defined ( $cmd = $term->readline( get_prompt() ) ) ) {
    my @tokens = split /\s+/, $cmd;
    #print join( ' ', @tokens ), "\n";
    try { 
        App::Dochazka::CLI::Parser::parse_tokens( [], \@tokens ); 
    } catch { 
        $status = $_; 
    };
    last if $status->code eq 'DOCHAZKA_CLI_EXIT';
    if ( $status->ok and $status->payload ) {
        print $status->code . " (OK) " . $status->text . "\n";
        print "Payload: " . Dumper( $status->payload );
    } else {
        print( ( $status->code eq 'DOCHAZKA_CLI_PARSE_ERROR' ) 
            ? $status->text . "\n"
            : $status->code . ' (' . $status->level . ') ' . $status->text . "\n" );
    }
}
