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

using namespace cryptrav;


namespace commander
{


int            sz(ContextGlob* pContext, StateGlob* pState, QStringList argList)
{
 if ( (pContext == NULL) || (pState == NULL) )
  return COMERR_NULL_POINTER;


 QStringList comList;
 QStringList optList;
 CommandLineParser::separate(&argList, &comList, &optList);


 if ( optList.contains("--help") )
  {
   Console::putHelp("Available 'sz' subcommands:");
   Console::putHelp("  near <objectname>  (alias: 'szn <objectname>')");
   Console::putHelp("  far <objectname>   (alias: 'szf <objectname>')");
   Console::putHelp("  push <objectname>");
   Console::putHelp("  pull <objectname>");
   Console::putHelp("  erase <objectname>");
   Console::putHelp("Available 'sz' options:");
   Console::putHelp("  --help");

   return COMERR_OK;
  }


 QString       paramact = "";

 QString       objName = "";


 if (comList.count() < 1)
  {
   Console::putFailure("No subcommand (try 'sz --help')");
   return COMERR_NO_OBJECT_NAME;
  }

 paramact = comList.at(0);

 if (paramact.isEmpty())
  {
   Console::putFailure("Empty subcommand (try 'sz --help')");
   return COMERR_EMPTY_OBJECT_NAME;
  }


 if (comList.count() < 2)
  {
   Console::putFailure("No object name (try 'sz --help')");
   return COMERR_NO_OBJECT_NAME;
  }

 objName = comList.at(1);

 if (objName.isEmpty())
  {
   Console::putFailure("Empty object name (try 'sz --help')");
   return COMERR_EMPTY_OBJECT_NAME;
  }


 Sizer         sizer(pContext, pState);


 SizeType      sizeType;

 if (paramact == "near") sizeType = SizeNear;
 else if (paramact == "far") sizeType = SizeFar;
 else if (paramact == "push") sizeType = SizePush;
 else if (paramact == "pull") sizeType = SizePull;
 else if (paramact == "erase") sizeType = SizeErase;
 else
  {
   Console::putFailure("Unknown subcommand (try 'sz --help')");
   return COMERR_UNKNOWN_SUBCOMMAND;
  }


 qint64        objSize = sizer.size( pState -> getPathInNominis(), objName, sizeType );


 if (objSize >= 0)
  {
   Console::put(QString::number(objSize, 10) + " b", SIZECOLOR_DEFAULT);
   Console::put(" / ", SIZECOLOR_DEFAULT | 8); // brighter

   Console::put(QString::number(objSize >> 10, 10) + " Kb", SIZECOLOR_DEFAULT);
   Console::put(" / ", SIZECOLOR_DEFAULT | 8);

   Console::put(QString::number(objSize >> 20, 10) + " Mb", SIZECOLOR_DEFAULT);
   Console::put(" / ", SIZECOLOR_DEFAULT | 8);

   Console::putLine(QString::number(objSize >> 30, 10) + " Gb", SIZECOLOR_DEFAULT);

   Console::putOutcome(true);
  }
   else Console::putOutcome(false);


 return COMERR_OK;
}


} // namespace "commander"
