#include "util.h"
#include "wayland.hpp"

#include <unistd.h>

#include <stdlib.h>
#include <string.h>

#include <map>
#include <memory>
#include <string>
#include <vector>

#include <sys/poll.h>

struct conn {
   std::vector<std::unique_ptr<wl::output>> outputs;
   std::unique_ptr<genivi::controller> c;
};

namespace {
int check_events(struct wl::display *d, struct conn *c, int fd) {
   struct pollfd pfd[2] = {{.fd = d->get_fd(), .events = POLLIN, .revents = 0},
                           {.fd = fd, .events = POLLIN, .revents = 0}};

   d->flush();

   if (poll(pfd, fd != -1 ? 2 : 1, -1) != -1 && errno != EINTR) {
      int ret = 0;

      if (pfd[0].revents & POLLIN) {
         ret = d->dispatch();
      }

      if (ret == -1)
         return ret;

      if (fd != -1 && (pfd[1].revents & POLLIN)) {
         char buf[256];

         // read all there is ...
         while (read(pfd[1].fd, buf, sizeof(buf)) == sizeof(buf))
            ;

         // Display current status
         if (!c->c->surfaces.empty()) {
            puts("Surfaces:");
            for (auto const &i : c->c->surfaces) {
               struct genivi::rect const &r = i.second->dst_rect;
               struct genivi::size const &s = i.second->size;
               printf("%d [%ux%u] (%ux%u@%dx%d), ", i.first, s.w, s.h, r.w, r.h,
                      r.x, r.y);
            }
            puts("\b\b ");
         }

         if (!c->c->layers.empty()) {
            puts("Layers:");
            for (auto const &i : c->c->layers) {
               struct genivi::rect const &r = i.second->dst_rect;
               struct genivi::size const &s = i.second->size;
               printf("%d [%ux%u] (%ux%u@%dx%d), ", i.first, s.w, s.h, r.w, r.h,
                      r.x, r.y);
            }
            puts("\b\b ");
         }
      }
   }

   return 0;
}

void init_layout(struct conn &c) {
   auto &o = c.outputs.front();
   auto &s = c.c->screens.begin()->second;
   auto &layers = c.c->layers;

   // XXX: Write output dimensions to ivi controller...
   c.c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};

   // Clear scene
   layers.clear();

   // Clear screen
   s->clear();

   // Setup our dummy scene...
   c.c->layer_create(100, 0, 0);  // bottom layer, anything else
   c.c->layer_create(1000, 0, 0); // top layer, mandelbrot

   auto &l100 = c.c->layers[100];
   auto &l1k = c.c->layers[1000];

   // Set layers fullscreen
   l100->set_destination_rectangle(0, 0, o->width, o->height);
   l1k->set_destination_rectangle(0, 0, o->width, o->height);

   // Add layers to screen
   s->add_layer(l100.get());
   s->add_layer(l1k.get());

   c.c->commit_changes();
   // Note: this does not flush the display!
}
}

int main(int argc, char **argv) {
   lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);

   if (!getenv("XDG_RUNTIME_DIR"))
      fatal("Environment variable XDG_RUNTIME_DIR not set");

   auto d = std::make_unique<wl::display>();
   if (!d->ok())
      fatal("Could not connect to compositor");

   struct conn c = {};

   d->r->add_global_handler(
      "ivi_controller", [&](wl_registry *r, uint32_t name, uint32_t v) {
         c.c = std::make_unique<genivi::controller>(r, name, v);
      });

   d->r->add_global_handler(
      "wl_output", [&](wl_registry *r, uint32_t name, uint32_t v) {
         c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
      });

   // First level objects
   d->roundtrip();
   // Second level objects
   d->roundtrip();
   // Third level objects
   d->roundtrip();

   if (!c.c)
      fatal("ivi_controller global not available");

   if (c.outputs.empty())
      fatal("no output was set up!");

   init_layout(c);

   while (check_events(d.get(), &c, STDIN_FILENO) != -1) {
      c.c->execute_pending();
      d->flush();
   }

   d->roundtrip();

   return 0;
}