#include "functions.hh"
#include "statistical_class.hh"
#include <QDateTime>
#include <cassert>
#include <fstream>

namespace {

bool isTradingTime(const QDateTime& dateTime)
{
    int dayOfWeek = dateTime.date().dayOfWeek();
    QTime time = dateTime.time();

    // Tuesdays, wednesdays and thursdays
    if (dayOfWeek != 2 && dayOfWeek != 3 && dayOfWeek != 4)
        return false;

    // London market trading hours with start delayed by 4 hours in Eastern
    // Standard Time (EST) WITHOUT Day Light Savings adjustments
    if (!(time >= QTime(6, 0, 0) && time < QTime(11, 0, 0)))
        return false;

    return true;
}

} // namespace

bool trainingTimeFilter(const QDateTime& dateTime)
{
    int year = dateTime.date().year();
    int month = dateTime.date().month();

    /*if (!((year == 2013 && (month == 12)) ||
          (year == 2014 && ( month == 1 || month == 2))))
    {
        return false;
    }*/

    if (year != 2014)
        return false;

    if (month != 9 && month != 10 && month != 11)
        return false;

    if (!isTradingTime(dateTime))
        return false;

    return true;
}

bool validationTimeFilter(const QDateTime& dateTime)
{
    int year = dateTime.date().year();
    int month = dateTime.date().month();

    if (year != 2014)
        return false;

    if (month != 12)
        return false;

    if (!isTradingTime(dateTime))
        return false;

    return true;
}

std::vector<StatisticalClass> generateClasses(
    double from, int count, double step)
{
    assert(count >= 1);
    std::vector<StatisticalClass> classes;

    for (int i = 0; i < count; i++) {
        StatisticalClass statClass(from + step * i, from + step * (i + 1));
        classes.push_back(statClass);
    }

    return classes;
}

bool fileExists(const std::string& fileName)
{
    std::ifstream fileStream(fileName);
    return fileStream.good();
}

