#include "selfdrive/ui/qt/body.h"

#include <cmath>
#include <algorithm>

#include <QPainter> 
#include <QStackedLayout>

#include <QApplication>
#include <QGridLayout>
#include <QString>
#include <QTransform>
#include <QPixmap>

#include "common/params.h"
#include "common/timing.h"

#include "system/hardware/hw.h" 
#include "selfdrive/ui/qt/qt_window.h"
#include "selfdrive/ui/qt/util.h"

LogoWidget::LogoWidget(QWidget *parent) : QWidget(parent) {
  setAttribute(Qt::WA_OpaquePaintEvent);
  setFixedSize(spinner_size);

  // pre-compute all the track imgs. 
  QPixmap comma_img = loadPixmap("../assets/img_spinner_comma.png", spinner_size);
  QPixmap track_img = loadPixmap("../assets/img_spinner_track.png", spinner_size);

  QTransform transform(1, 0, 0, 1, width() / 2, height() / 2);
  QPixmap pm(spinner_size);
  QPainter p(&pm);
  p.setRenderHint(QPainter::SmoothPixmapTransform);
    // p.resetTransform();
    // p.fillRect(0, 0, spinner_size.width(), spinner_size.height(), Qt::black);
    p.fillRect(0, 0, spinner_size.width(), spinner_size.height(), Qt::red);
    p.drawPixmap(0, 0, comma_img);
    // p.setTransform(transform.rotate(360 / spinner_fps));
    // p.drawPixmap(-width() / 2, -height() / 2, track_img);
    // track_imgs[i] = pm.copy();

  // m_anim.setDuration(1000);
  // m_anim.setStartValue(0);
  // m_anim.setEndValue(int(track_imgs.size() -1));
  // m_anim.setLoopCount(-1);
  // m_anim.start();

  // connect(&m_anim, SIGNAL(valueChanged(QVariant)), SLOT(update()));
}

void LogoWidget::paintEvent(QPaintEvent *event) {
  QPainter painter(this);
}

BodyWindow::BodyWindow(QWidget *parent) : QWidget(parent) {
  QGridLayout *layout = new QGridLayout(this);
  layout->setSpacing(0);
  layout->setMargin(200);

  layout->addWidget(new LogoWidget(this), 0, 0, Qt::AlignHCenter | Qt::AlignVCenter);

  setStyleSheet(R"(
    BodyWindow {
      background-color: blue;
    }
  )");

  // notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read);
  QObject::connect(uiState(), &UIState::uiUpdate, this, &BodyWindow::updateState);
}

void BodyWindow::paintEvent(QPaintEvent *event) {
  QPainter painter(this);
}

void BodyWindow::updateState(const UIState &s) {
  // if (!isVisible()) {
  //   return;
  // }
}

// void BodyWindow::paintEvent(QPaintEvent *event) {
//   QPainter p(this);
//   p.setRenderHint(QPainter::Antialiasing);

//   p.fillRect(rect(), QColor(0, 0, 0));

//   // battery outline + detail
//   p.translate(width() - 136, 16);
//   const QColor gray = QColor("#737373");
//   p.setBrush(Qt::NoBrush);
//   p.setPen(QPen(gray, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
//   p.drawRoundedRect(2, 2, 78, 36, 8, 8);

//   p.setPen(Qt::NoPen);
//   p.setBrush(gray);
//   p.drawRoundedRect(84, 12, 6, 16, 4, 4);
//   p.drawRect(84, 12, 3, 16);

//   // battery level
//   double fuel = std::clamp(fuel_filter.x(), 0.2f, 1.0f);
//   const int m = 5; // manual margin since we can't do an inner border
//   p.setPen(Qt::NoPen);
//   p.setBrush(fuel > 0.25 ? QColor("#32D74B") : QColor("#FF453A"));
//   p.drawRoundedRect(2 + m, 2 + m, (78 - 2*m)*fuel, 36 - 2*m, 4, 4);

//   // charging status
//   if (charging) {
//     p.setPen(Qt::NoPen);
//     p.setBrush(Qt::white);
//     const QPolygonF charger({
//       QPointF(12.31, 0),
//       QPointF(12.31, 16.92),
//       QPointF(18.46, 16.92),
//       QPointF(6.15, 40),
//       QPointF(6.15, 23.08),
//       QPointF(0, 23.08),
//     });
//     p.drawPolygon(charger.translated(98, 0));
//   }
// }

void BodyWindow::offroadTransition(bool offroad) {
//   btn->setChecked(true);
//   btn->setEnabled(true);
//   fuel_filter.reset(1.0);
}

// void BodyWindow::updateState(const UIState &s) {
//   if (!isVisible()) {
//     return;
//   }

//   const SubMaster &sm = *(s.sm);
//   auto cs = sm["carState"].getCarState();

//   charging = cs.getCharging();
//   fuel_filter.update(cs.getFuelGauge());

//   // TODO: use carState.standstill when that's fixed
//   const bool standstill = std::abs(cs.getVEgo()) < 0.01;
//   QMovie *m = standstill ? sleep : awake;
//   if (m != face->movie()) {
//     face->setMovie(m);
//     face->movie()->start();
//   }

//   // update record button state
//   if (sm.updated("managerState") && (sm.rcv_time("managerState") - last_button)*1e-9 > 0.5) {
//     for (auto proc : sm["managerState"].getManagerState().getProcesses()) {
//       if (proc.getName() == "loggerd") {
//         btn->setEnabled(true);
//         btn->setChecked(proc.getRunning());
//       }
//     }
//   }

//   update();
// }
