#include "analyzer.h"
#include "ui_analyzer.h"
#include "QFileDialog"
#include <QStandardPaths>
#include <QString>
#include <QDirIterator>
#include <QFileInfo>
#include <QDebug>
#include <QStringList>
#include "delegatepathitem.h"

analyzer::analyzer(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::analyzer)
{
    ui->setupUi(this);
    countSongs=0;
    firstAnalysisDone=false;

    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "Filename"<<"Loudness in dBFS"<<"Peaklevel in dBFS"<<"corrected peak in dBFS"<<"required Correction");
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
}

analyzer::~analyzer()
{
    delete ui;
    if (countSongs>0){
        delete [] songList;
    }
}

void analyzer::on_pushButton_clicked()
{
    QString folder=QFileDialog::getExistingDirectory(this,"Select the folder that is containing audio",QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
    ui->lineEdit_path->setText(folder);
}


void analyzer::on_pushButton_calculate_clicked()
{
    ui->pushButton_calculate->setDisabled(true);
    ui->doubleSpinBox_preferredLoudness->setDisabled(true);
    if (countSongs>0){
        delete [] songList;
    }
    countSongs=0;
    QDirIterator it_counter(ui->lineEdit_path->text(), {"*.mp3", "*.wav", "*.flac", "*.ogg" ,"*.opus", "*.wv", "*.mpc"}, QDir::NoFilter, QDirIterator::Subdirectories);
    while (it_counter.next()!="") {
        countSongs+=1;
    }
    QDirIterator it(ui->lineEdit_path->text(), {"*.mp3", "*.wav", "*.flac", "*.ogg" ,"*.opus", "*.wv", "*.mpc"}, QDir::NoFilter, QDirIterator::Subdirectories);
    unsigned int k=0;
    songList=new csong[countSongs];
    float preferedLoudness=ui->doubleSpinBox_preferredLoudness->value();
    ui->label_progress->setText(QString("0 of ")+QString::number(countSongs)+QString(" songs analyzed"));
    ui->progressBar->setMaximum(countSongs);
    while (it.hasNext()) {
        QFileInfo f(it.next());
        songList[k].setPath(f.absoluteFilePath());
        songList[k].analyze(preferedLoudness);
        k++;
        ui->progressBar->setValue(k);
        ui->label_progress->setText(QString::number(k)+QString(" of ")+QString::number(countSongs)+QString(" songs analyzed"));
        QApplication::processEvents();
    }
    ui->label_progress->setText(QString("done"));
    ui->tableWidget->setSortingEnabled(false);
    ui->tableWidget->setRowCount(countSongs);
    for (unsigned int i=0;i<countSongs;i++){
        for (int k=0;k<5;k++){
           ui->tableWidget->setItem(i,k,&songList[i].m_widgetItem[k]);
        }
    }
    ui->tableWidget->setItemDelegateForColumn(0, new DelegatePathItem);
    ui->pushButton_calculate->setDisabled(false);
    ui->doubleSpinBox_preferredLoudness->setDisabled(false);
    ui->tableWidget->setSortingEnabled(true);
    ui->tableWidget->sortItems(0);

    firstAnalysisDone=true;

}


void analyzer::on_doubleSpinBox_preferredLoudness_valueChanged(double arg1)
{
    if (firstAnalysisDone){
        for (unsigned int i=0; i<countSongs;i++){
            songList[i].setNewPreferedLoudness(arg1);
        }
    }
}

