#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDir>
#include <QDateTime>
#include <QTableWidgetItem>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->treeWidget->setColumnCount(3);
    on_pushButton_clicked();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showDir (QTreeWidgetItem * target,
                          QDir dir,
                          int level)
{
    QFileInfoList list = dir.entryInfoList
            (QDir::NoDotAndDotDot | QDir::AllEntries, QDir::Name);

    for (QFileInfo info : list)
    {
        QTreeWidgetItem * node = new QTreeWidgetItem;
        node->setText (0, info.fileName());

        QString path = info.absoluteFilePath();
        node->setToolTip(0, path);
        node->setData (0, Qt::UserRole, path);

        node->setText(1, QString::number(info.size()));

        // #include <QDateTime>
        QString s = info.lastModified().toString ("dd-MM-yyyy hh:mm");
        node->setText(2, s);

        QColor color;
        if (info.isDir())
            color = QColor ("red");
        else
            color= QColor ("blue");
        node->setForeground (0, color);

        QIcon icon ;
        if (info.isDir())
           icon = QIcon::fromTheme ("folder");
        else
           if (info.suffix() == "png")
              icon = QIcon (path);
        node->setIcon (0, icon);

        target->addChild (node);

        if (info.isDir() && level > 1)
           showDir (node, QDir (path), level-1);
    }
}

void MainWindow::on_pushButton_clicked()
{
    // QDir dir ("/usr/share/icons/oxygen/base/64x64");
    QDir dir ("../tree");

    QTreeWidgetItem * item = new QTreeWidgetItem;
    QString path = dir.absolutePath();
    item->setText (0, path);
    item->setToolTip(0, path);
    item->setData (0, Qt::UserRole, path);
    item->setForeground (0, QColor ("red"));
    // ui->treeWidget->addTopLevelItem (item);
    ui->treeWidget->invisibleRootItem()->addChild(item);

    showDir (item, dir, 5);
    ui->treeWidget->expandAll();
}

void MainWindow::on_treeWidget_itemClicked
     (QTreeWidgetItem *item, int column)
{
    QString path = item->data (0, Qt::UserRole).toString();

    QFileInfo fi (path);
    QString ext = fi.suffix();

    if (ext == "cpp" || ext == "h")
        readText (path);
    else if (ext == "dat")
        readData (path);
}

void MainWindow::readText (QString path)
{
    ui->tabWidget->setCurrentIndex (0);
    QFile f (path);

    if (f.open (QIODevice::ReadOnly | QIODevice::Text))
    {
        QByteArray b = f.readAll();
        ui->textEdit->setText (b);
    }
}

void MainWindow::readData (QString path)
{
    ui->tabWidget->setCurrentIndex (1);
    ui->tableWidget->setColumnCount (0);
    ui->tableWidget->setRowCount (0);
    int line = 0;

    QFile f (path);
    if (f.open (QIODevice::ReadOnly | QIODevice::Text))
    {
        while (! f.atEnd())
        {
            QString s = f.readLine();
            QStringList list = s.split (",");
            int col = 0;
            for (QString t : list)
            {
                t = t.simplified ();
                if (line+1 > ui->tableWidget->rowCount())
                    ui->tableWidget->setRowCount (line+1);
                if (col+1 > ui->tableWidget->columnCount())
                    ui->tableWidget->setColumnCount (col+1);
                QTableWidgetItem * item = new QTableWidgetItem;
                item->setText (t);
                ui->tableWidget->setItem (line, col, item);
                if (t == "0")
                    item->setForeground(QColor ("red"));
                else
                    item->setForeground(QColor ("blue"));
                col ++;
            }
            line ++;
        }
    }
}

void MainWindow::on_action_Run_triggered()
{
    int r = ui->tableWidget->rowCount();
    int s = ui->tableWidget->columnCount();
    for (int i = 0; i < r; i ++)
        for (int j = 0; j < s; j ++)
        {
           QTableWidgetItem * item = ui->tableWidget->item (i, j);
           QString s = item->text().simplified();
           double x;
           bool ok;
           x = s.toDouble (&ok);
           if (! ok)
               item->setForeground(QColor ("orange"));

        }


}

void MainWindow::on_action_Quit_triggered()
{
    close ();
}