#include <frost_utils.h>
#include <frost_utilsmanager.h>
#include <frost_utils_timemanager.h>
#include <frost_utils_file.h>

#include <ctime>
#include <iostream>

#include "state.h"
#include "vector.h"

using namespace Frost;

File mLog("tensor.log", File::O);

void LogCout(const s_str& sMessage, const s_bool& bStamps, const s_uint& uiOffset)
{
    std::cout << sMessage.Get() << std::endl;
    mLog.WriteLine(sMessage.Get());
    mLog.Flush();
}

s_double GetTime()
{
    return clock() / (double)CLOCKS_PER_SEC;
}

int main(int argc, char* argv[])
{
    s_ctnr<s_str> lArgs;
    for (int i = 0; i < argc; ++i)
        lArgs.PushBack(argv[i]);

    UtilsManager::GetSingleton()->SetLogFunction(&LogCout);
    TimeManager::GetSingleton()->SetGetTimeFunction(&GetTime);

    State s;

    Vector::Register(s);

    //s.ParseLine("a;b;c; r &= (a*b); 4*(1-r + 3*6)/c");

    if (lArgs.GetSize() > 1 && File::Exists(lArgs[1]))
    {
        // File mode
        File mFile(lArgs[1]);

        s_str sLine;
        while (mFile.IsValid())
        {
            sLine = mFile.GetLine();

            if (sLine.IsEmpty())
                continue;

            s.ParseLine(sLine);
        }
    }
    else
    {
        // Interactive mode
        s.SetInteractiveMode(true);

        s_str sLine;
        while (true)
        {
            std::cout << "> " << std::flush;
            getline(std::cin, sLine.Get());

            if (sLine.IsEmpty())
                continue;

            if (sLine == "q" || sLine == "exit")
                return 0;

            s.ParseLine(sLine);
        }
    }

    TimeManager::Delete();
    UtilsManager::Delete();

    return 0;
}
