/*
*  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 "sessionloop.h"

using namespace cryptrav;
using namespace commander;


int            sessionloop(ContextGlob* pContext, StateGlob* pState)
{
 QString       input;
 QStringList   argList;
 QString       command;

 bool          isQuit = false;


 Console::putLine("");
 Console::putLine("Session OPENED on " + (pState -> getDate()).toString("yyyy.MM.dd") + ", at " + (pState -> getTime()).toString("HH:mm:ss"), EGA_LIGHTMAGENTA);

 do
  {
   Console::putLine("");
   Console::putLine("[-------------------------------- " + (pState -> getTime()).toString("HH:mm:ss") + " --------------------------------]", EGA_LIGHTBLUE);
   Console::putLine("In Hallway: \"" + pState -> getPathInHallway() + "\"");
   Console::putLine("In Nominis: \"" + pState -> getPathInNominis() + "\"");


   input = Console::getLine();

   CommandLineParser::parse(input, &argList);


   if (argList.length() > 0)
    {
     command = argList.at(0);

     if (command == "cd") commander::cd(pContext, pState, argList.mid(1));
     else if (command == "cdh") commander::cd(pContext, pState, QStringList("hallway") << argList.mid(1));
     else if (command == "cdn") commander::cd(pContext, pState, QStringList("nominis") << argList.mid(1));

     else if (command == "md") commander::md(pContext, pState, argList.mid(1));
     else if (command == "mdh") commander::md(pContext, pState, QStringList("hallway") << argList.mid(1));
     else if (command == "mdn") commander::md(pContext, pState, QStringList("nominis") << argList.mid(1));

     else if (command == "ls") commander::ls(pContext, pState, argList.mid(1));
     else if (command == "lsh") commander::ls(pContext, pState, QStringList("hallway") << argList.mid(1));
     else if (command == "lshd")commander::ls(pContext, pState, QStringList("hallway") << QString("--detail") << argList.mid(1));
     else if (command == "lsn") commander::ls(pContext, pState, QStringList("nominis") << argList.mid(1));
     else if (command == "lsnd") commander::ls(pContext, pState, QStringList("nominis") << QString("--detail") << argList.mid(1));


     else if (command == "sz") commander::sz(pContext, pState, argList.mid(1));
     else if (command == "szn") commander::sz(pContext, pState, QStringList("near") << argList.mid(1));
     else if (command == "szf") commander::sz(pContext, pState, QStringList("far") << argList.mid(1));


     else if (command == "put") commander::put(pContext, pState, argList.mid(1));

     else if (command == "get") commander::get(pContext, pState, argList.mid(1));


     else if (command == "test") commander::test(pContext, pState, argList.mid(1));


     else if (command == "idle") commander::idle(pContext, pState, argList.mid(1));

     else if (command == "push") commander::push(pContext, pState, argList.mid(1));

     else if (command == "pull") commander::pull(pContext, pState, argList.mid(1));

     else if (command == "shade") commander::shade(pContext, pState, argList.mid(1));

     else if (command == "erase") commander::erase(pContext, pState, argList.mid(1));


     else if (command == "about") commander::about();

     else if (command == "help") commander::help();


     else if (command == "quit") isQuit = true;


     else Console::putFailure("Unknown command (try 'help')");

    }


  } while ( !isQuit );

 Console::LF();
 Console::putLine("Session CLOSED on " + (pState -> getDate()).toString("yyyy.MM.dd") + ", at " + (pState -> getTime()).toString("HH:mm:ss"), EGA_LIGHTMAGENTA);

 Console::setTextAttr(TEXTCOLOR_DEFAULT, BACKCOLOR_DEFAULT);


 return 0;
}
