/*
 * Strawberry Music Player
 * This file was part of Clementine.
 * Copyright 2010, David Sansome <me@davidsansome.com>
 *
 * Strawberry is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Strawberry is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"

#include <algorithm>
#include <utility>

#include <QtGlobal>
#include <QWidget>
#include <QString>
#include <QStringList>
#include <QBoxLayout>
#include <QLabel>
#include <QFrame>

#include "utilities/strutils.h"
#include "coversearchstatistics.h"
#include "coversearchstatisticsdialog.h"
#include "ui_coversearchstatisticsdialog.h"

using namespace Qt::Literals::StringLiterals;

CoverSearchStatisticsDialog::CoverSearchStatisticsDialog(QWidget *parent)
    : QDialog(parent), ui_(new Ui_CoverSearchStatisticsDialog) {

  ui_->setupUi(this);
  details_layout_ = new QVBoxLayout(ui_->details);
  details_layout_->setSpacing(0);

  setStyleSheet(u"#details {"
                "  background-color: palette(base);"
                "}"
                "#details QLabel[type=\"label\"] {"
                "  border: 2px solid transparent;"
                "  border-right: 2px solid palette(midlight);"
                "  margin-right: 10px;"
                "}"
                "#details QLabel[type=\"value\"] {"
                "  font-weight: bold;"
                "  max-width: 100px;"
                "}"_s);
}

CoverSearchStatisticsDialog::~CoverSearchStatisticsDialog() { delete ui_; }

void CoverSearchStatisticsDialog::Show(const CoverSearchStatistics &statistics) {

  QStringList providers(statistics.total_images_by_provider_.keys());
  std::sort(providers.begin(), providers.end());

  ui_->summary->setText(tr("Got %1 covers out of %2 (%3 failed)").arg(statistics.chosen_images_).arg(statistics.chosen_images_ + statistics.missing_images_).arg(statistics.missing_images_));

  for (const QString &provider : std::as_const(providers)) {
    AddLine(tr("Covers from %1").arg(provider), QString::number(statistics.chosen_images_by_provider_[provider]));
  }

  if (!providers.isEmpty()) {
    AddSpacer();
  }

  AddLine(tr("Total network requests made"), QString::number(statistics.network_requests_made_));
  AddLine(tr("Average image size"), statistics.AverageDimensions());
  AddLine(tr("Total bytes transferred"), statistics.bytes_transferred_ > 0 ? Utilities::PrettySize(statistics.bytes_transferred_) : u"0 bytes"_s);

  details_layout_->addStretch();

  show();

}

void CoverSearchStatisticsDialog::AddLine(const QString &label, const QString &value) {

  QLabel *label1 = new QLabel(label);
  QLabel *label2 = new QLabel(value);

  label1->setProperty("type", u"label"_s);
  label2->setProperty("type", u"value"_s);

  QHBoxLayout *layout = new QHBoxLayout;
  layout->addWidget(label1);
  layout->addWidget(label2);
  details_layout_->addLayout(layout);

}

void CoverSearchStatisticsDialog::AddSpacer() {
  details_layout_->addSpacing(20);
}
