/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  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.
*
*  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.
*
*  You should have received a copy of the GNU General Public License
*  along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include "console.h"


namespace commander
{

namespace Console
{


// System-dependent part...


#ifdef Q_OS_WIN // Windows implementation


#include <conio.h>
#include <windows.h>
#include <winbase.h>
#include <wincon.h>

int            getKey(void)
{
 return        getch();
}

void           setTextAttr(quint8 textColor, quint8 backColor)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textColor | (backColor << 4));
}


#elif defined Q_OS_LINUX // Linux implementation


#include <termios.h>
#include <stdio.h>

int            getKey(void)
{
 struct termios        oldSett, newSett;
 int                   key;

 tcgetattr(0, &oldSett);
 newSett = oldSett;

 newSett.c_lflag &= ~ICANON; // disable buffered I/O
 newSett.c_lflag &= ~ECHO; // disable echo
 tcsetattr(0, TCSANOW, &newSett);

 key = getchar();

 tcsetattr(0, TCSANOW, &oldSett);

 return key;
}

void           setTextAttr(quint8 textColor, quint8 backColor)
{
 QTextStream   qStdOut(stdout, QIODevice::WriteOnly);

 switch (textColor) {
  case EGA_BLACK:
   qStdOut << "\033[30m";
   break;

  case EGA_BLUE:
   qStdOut << "\033[34m";
   break;

  case EGA_GREEN:
   qStdOut << "\033[32m";
   break;

  case EGA_CYAN:
   qStdOut << "\033[36m";
   break;

  case EGA_RED:
   qStdOut << "\033[31m";
   break;

  case EGA_MAGENTA:
   qStdOut << "\033[35m";
   break;

  case EGA_BROWN:
   qStdOut << "\033[33m";
   break;

  case EGA_GREY:
   qStdOut << "\033[37m";
   break;


  case EGA_DARKGREY:
   qStdOut << "\033[90m";
   break;

  case EGA_LIGHTBLUE:
   qStdOut << "\033[94m";
   break;

  case EGA_LIGHTGREEN:
   qStdOut << "\033[92m";
   break;

  case EGA_LIGHTCYAN:
   qStdOut << "\033[96m";
   break;

  case EGA_LIGHTRED:
   qStdOut << "\033[91m";
   break;

  case EGA_LIGHTMAGENTA:
   qStdOut << "\033[95m";
   break;

  case EGA_YELLOW:
   qStdOut << "\033[93m";
   break;

  case EGA_WHITE:
   qStdOut << "\033[97m";
   break;

 };


 switch (backColor) {
  case EGA_BLACK:
// TODO: implement back color actually;
//       when doing it in the following way, background is always faint
//
//   qStdOut << "\033[40m";
   break;

 };

}


#endif // Q_OS_WIN | Q_OS_LINUX


// System-independent part...


QString        getLine(QString prompt, quint8 textColor, quint8 backColor)
{
 QTextStream   qStdIn(stdin, QIODevice::ReadOnly);
 QString       qStr;


 put(prompt, textColor, backColor);

 setTextAttr(textColor, backColor);
 do qStr = qStdIn.readLine(); while (qStr.isEmpty());
 setTextAttr(TEXTCOLOR_DEFAULT, BACKCOLOR_DEFAULT);


 return qStr;
}


void           put(QString qStr, quint8 textColor, quint8 backColor)
{
 QTextStream   qStdOut(stdout, QIODevice::WriteOnly);

 setTextAttr(textColor, backColor);
 qStdOut << qStr;
// setTextAttr(TEXTCOLOR_DEFAULT, BACKCOLOR_DEFAULT);
}

void           LF(void)
{
 QTextStream   qStdOut(stdout, QIODevice::WriteOnly);

 qStdOut << endl;
}

void           putLine(QString qStr, quint8 textColor, quint8 backColor)
{
 put(qStr, textColor, backColor);
 LF();
}


void           putOutcome(bool state, quint8 successTextColor, quint8 failureTextColor, quint8 backColor)
{
 if (state)
  putLine("Done", successTextColor, backColor);
   else
  putLine("Failed", failureTextColor, backColor);

}

void           putHelp(QString qStr, quint8 textColor, quint8 backColor)
{
 putLine(qStr, textColor, backColor);
}

void           putFailure(QString qStr, quint8 textColor, quint8 backColor)
{
 putLine(qStr, textColor, backColor);
}


} // namespace "Console"

} // namespace "commander"



