///
/// @file ui.h
/// @authors k. headley, bob herlien
/// @date 01 mar 2020

/// user interface API

/// @sa doxygen-examples.c for more examples of Doxygen markup


/////////////////////////
// Terms of use
/////////////////////////
/*
 Copyright Information
  
 Copyright 2002-2020 MBARI
 Monterey Bay Aquarium Research Institute, all rights reserved.
 
 Terms of Use
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version. You can access the GPLv3 license at
 http://www.gnu.org/licenses/gpl-3.0.html
 
 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.  See the
 GNU General Public License for more details
 (http://www.gnu.org/licenses/gpl-3.0.html)
 
 MBARI provides the documentation and software code "as is", with no warranty,
 express or implied, as to the software, title, non-infringement of third party
 rights, merchantability, or fitness for any particular purpose, the accuracy of
 the code, or the performance or results which you may obtain from its use. You
 assume the entire risk associated with use of the code, and you agree to be
 responsible for the entire cost of repair or servicing of the program with
 which you are using the code.
 
 In no event shall MBARI be liable for any damages, whether general, special,
 incidental or consequential damages, arising out of your use of the software,
 including, but not limited to, the loss or corruption of your data or damages
 of any kind resulting from use of the software, any prohibited use, or your
 inability to use the software. You agree to defend, indemnify and hold harmless
 MBARI and its officers, directors, and employees against any claim, loss,
 liability or expense, including attorneys' fees, resulting from loss of or
 damage to property or the injury to or death of any person arising out of the
 use of the software.
 
 The MBARI software is provided without obligation on the part of the
 Monterey Bay Aquarium Research Institute to assist in its use, correction,
 modification, or enhancement.
 
 MBARI assumes no responsibility or liability for any third party and/or
 commercial software required for the database or applications. Licensee agrees
 to obtain and maintain valid licenses for any additional third party software
 required.
 */

#ifndef USER_INTERFACE_H
#define USER_INTERFACE_H

/////////////////////////
// Includes 
/////////////////////////

#include "cwgsys.h"

/////////////////////////
// Macros
/////////////////////////

/// maximum command line entry length
#ifndef UI_CMD_BUF_BYTES
#define UI_CMD_BUF_BYTES   80  
#endif

#ifdef WITH_UI_HISTORY
/// default history queue len
#ifndef UI_HIST_QUEUE_LEN
#define UI_HIST_QUEUE_LEN 16
#endif
#endif //WITH_UI_HISTORY

#define UI_PROMPT_STR ">>"
#define UI_PROMPT(port) do{ \
   uint16_t UI_PROMPT_TMP=PROMPT_CONS; \
   config_get(CK_PROMPT,&UI_PROMPT_TMP); \
   if(UI_PROMPT_TMP==PROMPT_ALL || \
           (port==SER_CONS && (UI_PROMPT_TMP&PROMPT_CONS)!=0) || \
           (port==SER_HOST && (UI_PROMPT_TMP&PROMPT_HOST)!=0) ){\
   serPutString(port, UI_PROMPT_STR); \
   serTxWait(port); \
   }\
}while(0)


/////////////////////////
// Type Definitions
/////////////////////////

typedef enum{GL_NONE=0,GL_CASE=0x1,GL_ECHO=0x2}ui_glflags;

/// declaration defined in commands.h
struct CmdStruct_s; 

/////////////////////////
// Exports
/////////////////////////

#ifdef __cplusplus
extern "C" {
#endif

extern return_code_t g_cmd_status;

/// @brief did user interface timeout
/// @return TRUE of USER_TIME_OUT elapsed without console input

int uiDidUITO();
/// @brief Start USER_TIME_OUT timer

void uiStartTmr();
/// @brief match a line to a command name
/// @param str string to be scanned
/// @param name string to match
/// @return NULL if no match, otherwise a pointer to after @p name in @p str

char* uiStrName(char* str, const char* name);
/// @brief parse line and execute command if matched
/// @param line command string
/// @param cmds  list of commands to search through
/// @return result of command or ERR_NOCOMMAND if no match is made
return_code_t ui_parse(int argc, char **argv, const struct CmdStruct_s* cmds) ;

return_code_t uiParse(char* line, const struct CmdStruct_s* cmds);

/// @brief read from console input
/// @param cmd command buffer
/// @param len  buffer length (bytes)
/// @param serPort serial port to read from
/// @return TRUE if end-line is reached
bool uiGetLine(char* cmd, int len, char **cur, int serPort, ui_glflags flags );

/// @brief get console input, parse line and display ErrCode results
/// @return ErrCode of command or ERR_NO_LINE if no user input

int ui_console_update();
/// @brief get HOST input and do it
/// @return ErrCode of cmd

int ui_host_update();

int ui_tokenize(char *src, char **dest, char *del, int ntok, bool preserve_quote);
void ui_release_toks(char **tokens, int ntoks);
char *ui_unescape(char *src);

#ifdef WITH_UI_HISTORY
void ui_hist_show(bool verbose);
return_code_t ui_hist_exec(int id);
#endif

#ifdef __cplusplus
}
#endif

#endif // THIS_FILE