#include "colorcommand.h"
#include "colordatamodel.h"
#include "color_constants.h"
#include "patterncommand.h"
#include <QObject>

ColorDataModel::ColorDataModel()
{
    createColorList();
    createPatternList();
    createPatternMap();
    createColorActions();
    createPatternActions();

    for(QString color : mColorList)
    {
        mPenColors << QColor(color);
    }

    mCurrentColor   = Qt::GlobalColor::white;
    mCurrentPattern = Qt::SolidPattern;
}

ColorDataModel::~ColorDataModel()
{
    mColorList.clear();
    mPatternList.clear();
    mColorActions.clear();
    mPenColors.clear();
}

void ColorDataModel::createColorList()
{
    QString COLOR_NAME_GREEN      = "Green";
    QString COLOR_NAME_MAGENTA    = "Magenta";
    QString COLOR_NAME_RED        = "Red";
    QString COLOR_NAME_BLUE       = "Blue";
    QString COLOR_NAME_YELLOW     = "Yellow";
    QString COLOR_NAME_PINK       = "Pink";
    QString COLOR_NAME_ORANGE     = "Orange";
    QString COLOR_NAME_CYAN       = "Cyan";
    QString COLOR_NAME_GREY       = "Grey";
    QString COLOR_NAME_BLACK      = "Black";
    QString COLOR_NAME_WHITE      = "White";
    mColorList.append(COLOR_NAME_GREEN);
    mColorList.append(COLOR_NAME_RED);
    mColorList.append(COLOR_NAME_BLUE);
    mColorList.append(COLOR_NAME_YELLOW);
    mColorList.append(COLOR_NAME_MAGENTA);
    mColorList.append(COLOR_NAME_CYAN);
    mColorList.append(COLOR_NAME_ORANGE);
    mColorList.append(COLOR_NAME_PINK);
}

void ColorDataModel::createPatternMap()
{
    mPatternMap.insert(Qt::SolidPattern, _("Solid"));
    mPatternMap.insert(Qt::Dense3Pattern, _("Dense 3"));
    mPatternMap.insert(Qt::Dense6Pattern, _("Dense 6"));
    mPatternMap.insert(Qt::Dense7Pattern, _("Dense 7"));
    mPatternMap.insert(Qt::HorPattern, _("Horizontal"));
    mPatternMap.insert(Qt::VerPattern, _("Vertical"));
    mPatternMap.insert(Qt::CrossPattern, _("Cross"));
    mPatternMap.insert(Qt::BDiagPattern, _("Backward Diagonal"));
    mPatternMap.insert(Qt::FDiagPattern, _("Forward Diagonal"));
    mPatternMap.insert(Qt::DiagCrossPattern, _("Diagonal Cross"));
}

void ColorDataModel::createColorActions()
{
    QString COLOR_NAME_WHITE      = "White";
    QAction * colorAction = nullptr;

    QVector<QString>    colors      = mColorList;
    ColorCommand  *     colorCmd    = nullptr;
    for(const QString & color : colors)
    {
        colorAction = new QAction(QString(_((char*)color.toLocal8Bit().data())));
        mColorActions.insert(color, colorAction);

        colorCmd = new ColorCommand(colorAction, color);
        QObject::connect(colorAction, &QAction::triggered, colorCmd, &ColorCommand::execute );
    }

    QString multicolor = "Mult-color";
    colorAction = new QAction(multicolor);
    mColorActions.insert(multicolor, colorAction);

    colorCmd = new ColorCommand(colorAction, COLOR_NAME_WHITE);
    QObject::connect(colorAction, &QAction::triggered, colorCmd, &ColorCommand::execute );
}

void ColorDataModel::createPatternActions()
{
    QAction *                   patternAction   = nullptr;

    QVector<QString>            patterns        = getSupportedPatterns();
    PatternCommand  *           patternCmd      = nullptr;
    Qt::BrushStyle              patternStyle;

    for(const QString & pattern : patterns)
    {
        patternAction = new QAction(pattern);
        mPatternActions.insert(pattern, patternAction);

        patternStyle = mPatternMap.key(pattern);

        patternCmd = new PatternCommand(patternAction, patternStyle);
        QObject::connect(patternAction, &QAction::triggered, patternCmd, &PatternCommand::execute );
    }
}

void ColorDataModel::createPatternList()
{
    mPatternList.append(Qt::SolidPattern);
    mPatternList.append(Qt::Dense3Pattern);
    mPatternList.append(Qt::Dense6Pattern);
    mPatternList.append(Qt::Dense7Pattern);
    mPatternList.append(Qt::HorPattern);
    mPatternList.append(Qt::VerPattern);
    mPatternList.append(Qt::CrossPattern);
    mPatternList.append(Qt::BDiagPattern);
    mPatternList.append(Qt::FDiagPattern);
    mPatternList.append(Qt::DiagCrossPattern);
}

QColor ColorDataModel::getNextColor()
{
    static int index = 0;
    QColor color;

    if (mCurrentColor != Qt::GlobalColor::white)
    {
        color = mCurrentColor;
    }

    else
    {
        if (index < mPenColors.length())
        {
            color = mPenColors[index];

            index++;
            index = (index >= mPenColors.length()-1) ? 0 : index+1;
        }
    }

    return color;
}

Qt::BrushStyle ColorDataModel::getPattern()
{
    return mCurrentPattern;
}

void ColorDataModel::setCurrentColor(const QString &color)
{
    QColor newColor(color);

    mCurrentColor = newColor;
}

void ColorDataModel::setCurrentPattern(const Qt::BrushStyle &pattern)
{
    mCurrentPattern = pattern;
}

QAction * ColorDataModel::getColorAction(QString color)
{
    QAction * action = nullptr;

    action = mColorActions.value(color);

    return action;
}

QAction *ColorDataModel::getPatternAction(QString pattern)
{
    QAction * action = nullptr;
    QString   styleStr;

//    styleStr = mPatternMap.value(style);
    action   = mPatternActions.value(pattern);

    return action;
}

QVector<QString> ColorDataModel::getSupportedColors()
{
    return mColorList;
}

QVector<QString> ColorDataModel::getSupportedPatterns()
{
    return mPatternMap.values().toVector();
}
