/*
 * gcal-month-view.c
 *
 * Copyright 2023 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#define G_LOG_DOMAIN "GcalMonthView"

#include "config.h"
#include "gcal-debug.h"
#include "gcal-event-widget-pool.h"
#include "gcal-month-cell.h"
#include "gcal-month-popover.h"
#include "gcal-month-view.h"
#include "gcal-month-view-row.h"
#include "gcal-range-tree.h"
#include "gcal-timeline-subscriber.h"
#include "gcal-utils.h"
#include "gcal-view-private.h"

#include <math.h>
#include <adwaita.h>

#define N_ROWS_PER_PAGE 5
#define N_PAGES 3
#define N_TOTAL_ROWS (N_ROWS_PER_PAGE * N_PAGES)
#define FIRST_VISIBLE_ROW_INDEX (N_ROWS_PER_PAGE * (N_PAGES - 1) / 2)
#define LAST_VISIBLE_ROW_INDEX (FIRST_VISIBLE_ROW_INDEX + N_ROWS_PER_PAGE - 1)
#define CELL_ACTIVATED_OFFSET 18.0

// N_PAGES must be an odd number
G_STATIC_ASSERT (N_PAGES % 2 != 0);

struct _GcalMonthView
{
  GtkWidget          parent;

  GtkWidget          *header;
  GtkWidget          *weekday_label[N_WEEKDAYS];

  GtkEventController *motion_controller;

  GPtrArray          *week_rows;

  GcalEventWidgetPool *event_widget_pool;

  struct {
    GtkWidget        *popover;
    GcalMonthViewRow *row;
    GtkWidget        *relative_to;
  } overflow;

  struct {
    GDateTime        *start;
    GDateTime        *end;
  } selection;

  /* Ranges from [0.0, 1.0] */
  gdouble             row_offset;
  AdwAnimation       *row_offset_animation;

  AdwAnimation       *kinetic_scroll_animation;
  gdouble             last_velocity;

  GDateTime          *date;

  GtkWidget          *last_focused_widget;

  GdkModifierType     state;
};

static void          gcal_view_interface_init                    (GcalViewInterface  *iface);

static void          gtk_buildable_interface_init                (GtkBuildableIface  *iface);

static void          gcal_timeline_subscriber_interface_init     (GcalTimelineSubscriberInterface *iface);


G_DEFINE_FINAL_TYPE_WITH_CODE (GcalMonthView, gcal_month_view, GTK_TYPE_WIDGET,
                               G_IMPLEMENT_INTERFACE (GCAL_TYPE_TIMELINE_SUBSCRIBER, gcal_timeline_subscriber_interface_init)
                               G_IMPLEMENT_INTERFACE (GCAL_TYPE_VIEW, gcal_view_interface_init)
                               G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_buildable_interface_init))

enum
{
  PROP_0,
  PROP_DATE,
  PROP_TIME_DIRECTION,
  N_PROPS,
};


/*
 * Auxiliary methods
 */

static void
allocate_overflow_popover (GcalMonthView *self,
                           gint           width,
                           gint           height,
                           gint           baseline)
{
  graphene_point_t origin, end;
  GtkAllocation popover_allocation;
  GtkAllocation cell_allocation;
  gint popover_min_width;
  gint popover_nat_width;
  gint popover_height;
  gint popover_width;
  gint header_height;
  gint popover_x2;
  gint popover_y2;

  g_assert (self->overflow.relative_to != NULL);

  header_height = gtk_widget_get_height (self->header);

  if (!gtk_widget_compute_point (self->overflow.relative_to,
                                 GTK_WIDGET (self),
                                 &GRAPHENE_POINT_INIT (0, 0),
                                 &origin))
    g_assert_not_reached ();

  if (!gtk_widget_compute_point (self->overflow.relative_to,
                                 GTK_WIDGET (self),
                                 &GRAPHENE_POINT_INIT (gtk_widget_get_width (self->overflow.relative_to),
                                                       gtk_widget_get_height (self->overflow.relative_to)),
                                 &end))
    g_assert_not_reached ();

  cell_allocation = (GtkAllocation) {
    .x = origin.x,
    .y = origin.y,
    .width = end.x - origin.x,
    .height = end.y - origin.y,
  };

  gtk_widget_measure (GTK_WIDGET (self->overflow.popover),
                      GTK_ORIENTATION_HORIZONTAL,
                      -1,
                      &popover_min_width,
                      &popover_nat_width,
                      NULL, NULL);

  gtk_widget_measure (GTK_WIDGET (self->overflow.popover),
                      GTK_ORIENTATION_VERTICAL,
                      -1,
                      NULL,
                      &popover_height,
                      NULL, NULL);

  popover_width = CLAMP (popover_nat_width,
                         MAX (popover_min_width, cell_allocation.width * 1.25),
                         cell_allocation.width * 1.5);
  popover_height = CLAMP (popover_height, cell_allocation.height * 1.5, height);

  popover_allocation = (GtkAllocation) {
    .x = MAX (0, cell_allocation.x - (popover_width - cell_allocation.width) / 2.0),
    .y = MAX (header_height, cell_allocation.y - (popover_height - cell_allocation.height) / 2.0),
    .width = popover_width,
    .height = popover_height,
  };

  popover_x2 = popover_allocation.x + popover_allocation.width;
  if (popover_x2 > width)
    popover_allocation.x -= (popover_x2 - width);

  popover_y2 = popover_allocation.y + popover_allocation.height;
  if (popover_y2 > height)
    popover_allocation.y -= (popover_y2 - height);

  gtk_widget_size_allocate (self->overflow.popover, &popover_allocation, baseline);
}

static inline void
update_weekday_labels (GcalMonthView *self)
{
  const gint first_weekday = get_first_weekday ();

  for (gint i = 0; i < N_WEEKDAYS; i++)
    {
      g_autofree gchar *weekday_name = NULL;

      weekday_name = g_utf8_strup (gcal_get_weekday ((i + first_weekday) % N_WEEKDAYS), -1);

      gtk_label_set_label (GTK_LABEL (self->weekday_label[i]), weekday_name);
    }
}

static void
cancel_deceleration (GcalMonthView *self)
{
  if (self->kinetic_scroll_animation == 0)
    return;

  adw_animation_pause (self->kinetic_scroll_animation);
  g_clear_object (&self->kinetic_scroll_animation);
}

static void
cancel_row_offset_animation (GcalMonthView *self)
{
  if (!self->row_offset_animation)
    return;

  adw_animation_pause (self->row_offset_animation);
  g_clear_object (&self->row_offset_animation);
}

static void
update_active_date (GcalMonthView *self)
{
  g_autoptr (GcalRange) top_row_range = NULL;
  GtkWidget *top_row;

  top_row = g_ptr_array_index (self->week_rows, FIRST_VISIBLE_ROW_INDEX);
  top_row_range = gcal_month_view_row_get_range (GCAL_MONTH_VIEW_ROW (top_row));
  g_assert (top_row_range != NULL);

  gcal_view_set_date (GCAL_VIEW (self), gcal_range_get_start (top_row_range));
  g_object_notify (G_OBJECT (self), "active-date");
}

static inline void
maybe_popdown_overflow_popover (GcalMonthView *self)
{
  uint32_t row_index;
  gboolean found;

  if (!self->overflow.row)
    return;

  found = g_ptr_array_find (self->week_rows, self->overflow.row, &row_index);
  g_assert (found);

  if (floor (row_index / (gdouble) N_ROWS_PER_PAGE) != floor (N_PAGES / 2.0))
    gcal_month_popover_popdown (GCAL_MONTH_POPOVER (self->overflow.popover));
}

static void
move_bottom_row_to_top (GcalMonthView *self)
{
  g_autoptr (GcalRange) first_row_range = NULL;
  g_autoptr (GcalRange) new_range = NULL;
  GDateTime *first_row_range_start;
  GtkWidget *first_row;
  GtkWidget *last_row;

  first_row = g_ptr_array_index (self->week_rows, 0);
  first_row_range = gcal_month_view_row_get_range (GCAL_MONTH_VIEW_ROW (first_row));
  g_assert (first_row_range != NULL);
  first_row_range_start = gcal_range_get_start (first_row_range);
  g_assert (first_row_range_start != NULL);

  new_range = gcal_range_new_take (g_date_time_add_weeks (first_row_range_start, -1),
                                   g_date_time_ref (first_row_range_start),
                                   GCAL_RANGE_DEFAULT);

#ifdef GCAL_ENABLE_TRACE
    {
      g_autofree gchar *first_row_range_str = gcal_range_to_string (first_row_range);
      g_autofree gchar *new_range_str = gcal_range_to_string (new_range);
      GCAL_TRACE_MSG ("Moved bottom row to top, new range: %s (first row range: %s)", new_range_str, first_row_range_str);
    }
#endif

  last_row = g_ptr_array_steal_index (self->week_rows, self->week_rows->len - 1);
  gcal_month_view_row_set_range (GCAL_MONTH_VIEW_ROW (last_row), new_range);
  g_ptr_array_insert (self->week_rows, 0, last_row);
}

static void
move_top_row_to_bottom (GcalMonthView *self)
{
  g_autoptr (GcalRange) last_row_range = NULL;
  g_autoptr (GcalRange) new_range = NULL;
  GDateTime *last_row_range_end;
  GtkWidget *first_row;
  GtkWidget *last_row;

  last_row = g_ptr_array_index (self->week_rows, self->week_rows->len - 1);
  last_row_range = gcal_month_view_row_get_range (GCAL_MONTH_VIEW_ROW (last_row));
  g_assert (last_row_range != NULL);
  last_row_range_end = gcal_range_get_end (last_row_range);
  g_assert (last_row_range_end != NULL);

  new_range = gcal_range_new_take (g_date_time_ref (last_row_range_end),
                                   g_date_time_add_weeks (last_row_range_end, 1),
                                   GCAL_RANGE_DEFAULT);

#ifdef GCAL_ENABLE_TRACE
    {
      g_autofree gchar *last_row_range_str = gcal_range_to_string (last_row_range);
      g_autofree gchar *new_range_str = gcal_range_to_string (new_range);
      GCAL_TRACE_MSG ("Moved top row to bottom, new range: %s (last row range: %s)", new_range_str, last_row_range_str);
    }
#endif

  first_row = g_ptr_array_steal_index (self->week_rows, 0);
  gcal_month_view_row_set_range (GCAL_MONTH_VIEW_ROW (first_row), new_range);
  g_ptr_array_insert (self->week_rows, -1, first_row);
}

static inline void
dump_row_ranges (GcalMonthView *self)
{
#if defined(GCAL_ENABLE_TRACE) && 0
  for (guint i = 0; i < self->week_rows->len; i++)
    {
      g_autoptr (GcalRange) range = NULL;
      g_autofree gchar *range_str = NULL;
      GtkWidget *row;

      row = g_ptr_array_index (self->week_rows, i);
      range = gcal_month_view_row_get_range (GCAL_MONTH_VIEW_ROW (row));
      range_str = gcal_range_to_string (range);

      GCAL_TRACE_MSG ("    Row %u: %s", i, range_str);
    }
#endif
}

static void
update_week_ranges (GcalMonthView *self,
                    GDateTime     *new_date)
{
  g_autoptr (GcalRange) current_range = NULL;
  g_autoptr (GDateTime) current_date = NULL;
  gint n_weeks_before;

  /*
   * The current date corresponds to the week of the first
   * visible row.
   */
  n_weeks_before = N_ROWS_PER_PAGE * (N_PAGES - 1) / 2;

  current_date = g_steal_pointer (&self->date);
  if (current_date)
    current_range = gcal_timeline_subscriber_get_range (GCAL_TIMELINE_SUBSCRIBER (self));

  gcal_set_date_time (&self->date, new_date);

  if (current_range && gcal_range_contains_datetime (current_range, new_date))
    {
      g_autoptr (GcalRange) row_range = NULL;
      GcalMonthViewRow *row;
      gint diff;

      diff = g_date_time_compare (new_date, current_date);
      row = g_ptr_array_index (self->week_rows, n_weeks_before);
      row_range = gcal_month_view_row_get_range (row);

      while (!gcal_range_contains_datetime (row_range, new_date))
        {
          if (diff > 0)
            move_top_row_to_bottom (self);
          else
            move_bottom_row_to_top (self);

          g_clear_pointer (&row_range, gcal_range_unref);

          row = g_ptr_array_index (self->week_rows, n_weeks_before);
          row_range = gcal_month_view_row_get_range (row);
        }
    }
  else
    {
      for (gint i = 0; i < self->week_rows->len; i++)
        {
          g_autoptr (GDateTime) week_start = NULL;
          g_autoptr (GDateTime) week_end = NULL;
          g_autoptr (GcalRange) range = NULL;
          g_autoptr (GDateTime) date = NULL;
          GcalMonthViewRow *row;

          date = g_date_time_add_weeks (self->date, i - n_weeks_before);
          week_start = gcal_date_time_get_start_of_week (date);
          week_end = g_date_time_add_weeks (week_start, 1);
          range = gcal_range_new (week_start, week_end, GCAL_RANGE_DEFAULT);

          row = g_ptr_array_index (self->week_rows, i);
          gcal_month_view_row_set_range (row, range);
        }
    }

  maybe_popdown_overflow_popover (self);
  dump_row_ranges (self);
}

static inline gint
get_grid_height (GcalMonthView *self)
{
  return gtk_widget_get_height (GTK_WIDGET (self)) - gtk_widget_get_height (self->header);
}

static inline void
update_visible_rows (GcalMonthView *self)
{
  int actual_first_visible_row;
  int actual_last_visible_row;

  actual_first_visible_row = MAX (FIRST_VISIBLE_ROW_INDEX - (self->row_offset < 0.0 ? 1 : 0), 0);
  actual_last_visible_row = MIN (LAST_VISIBLE_ROW_INDEX + (self->row_offset > 0.0 ? 1 : 0), N_TOTAL_ROWS);

  for (unsigned int i = 0; i < self->week_rows->len; i++)
    {
      GtkWidget *row;
      gboolean child_visible;
      gboolean visible;

      row = g_ptr_array_index (self->week_rows, i);

      child_visible = i >= actual_first_visible_row && i <= actual_last_visible_row;
      visible = (i >= actual_first_visible_row - 1) && (i <= actual_last_visible_row + 1);

      gtk_widget_set_visible (row, visible);
      gtk_widget_set_child_visible (row, child_visible);
    }
}

static void
offset_and_shuffle_rows (GcalMonthView *self,
                         gdouble        dy)
{
  GCAL_ENTRY;

  self->row_offset += dy;

  if (fabs (self->row_offset) > 0.5)
    {
      gint rows_to_shuffle = round (fabs (self->row_offset) + 0.5);

      g_assert (rows_to_shuffle > 0);

      if (self->row_offset >= 0.0)
        {
          /* Positive offset: move top rows to the bottom */
          while (rows_to_shuffle--)
            move_top_row_to_bottom (self);
          self->row_offset = fmod (self->row_offset, 0.5) - 0.5;
        }
      else
        {
          /* Negative offset: move bottom rows to the top */
          while (rows_to_shuffle--)
            move_bottom_row_to_top (self);
          self->row_offset = fmod (self->row_offset, 0.5) + 0.5;
        }

      maybe_popdown_overflow_popover (self);
      update_active_date (self);
      dump_row_ranges (self);
    }

  update_visible_rows (self);
  gtk_widget_queue_allocate (GTK_WIDGET (self));

  GCAL_EXIT;
}

static void
offset_and_shuffle_rows_by_pixels (GcalMonthView *self,
                                   gdouble        dy_pixels)
{
  gdouble row_height;
  gint grid_height;

  GCAL_ENTRY;

  grid_height = get_grid_height (self);
  row_height = grid_height / (gdouble) N_ROWS_PER_PAGE;

  offset_and_shuffle_rows (self, dy_pixels / row_height);

  GCAL_EXIT;
}

static void
animate_row_offset_cb (gdouble  value,
                       gpointer user_data)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (user_data);

  self->row_offset = value;

  update_visible_rows (self);
  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
on_row_offset_animation_done (AdwAnimation  *animation,
                              GcalMonthView *self)
{
  gtk_widget_remove_css_class (GTK_WIDGET (self), "scrolling");
  cancel_row_offset_animation (self);

  self->row_offset = 0.0;

  update_visible_rows (self);
  gtk_widget_queue_allocate (GTK_WIDGET (self));
}

static void
snap_to_top_row (GcalMonthView *self)
{
  g_autoptr (AdwAnimationTarget) animation_target = NULL;

  GCAL_ENTRY;

  update_active_date (self);
  dump_row_ranges (self);

  animation_target = adw_callback_animation_target_new (animate_row_offset_cb, self, NULL);

  g_clear_object (&self->row_offset_animation);
  self->row_offset_animation = adw_timed_animation_new (GTK_WIDGET (self),
                                                        self->row_offset,
                                                        0.0,
                                                        200, // ms
                                                        g_steal_pointer (&animation_target));
  adw_timed_animation_set_easing (ADW_TIMED_ANIMATION (self->row_offset_animation),
                                  ADW_EASE_OUT_SINE);
  g_signal_connect (self->row_offset_animation,
                    "done",
                    G_CALLBACK (on_row_offset_animation_done),
                    self);

  adw_animation_play (self->row_offset_animation);

  GCAL_EXIT;
}

static void
animate_row_scroll_cb (gdouble  value,
                       gpointer user_data)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (user_data);
  gdouble *last_offset_location;
  gdouble dy;

  last_offset_location = g_object_get_data (G_OBJECT (self->row_offset_animation), "last-offset");
  g_assert (last_offset_location != NULL);

  dy = value - *last_offset_location;

  offset_and_shuffle_rows (self, dy);

  *last_offset_location = value;
}

static void
animate_row_scroll (GcalMonthView *self,
                    gint           n_rows)
{
  GCAL_ENTRY;

  g_assert (n_rows != 0);

  update_active_date (self);
  dump_row_ranges (self);

  if (!self->row_offset_animation)
    {
      g_autoptr (AdwAnimationTarget) animation_target = NULL;
      g_autofree gdouble *last_offset_location = NULL;
      GtkReducedMotion reduced_motion;
      gboolean increase_duration;

      /*
       * Fallback property in case "gtk-interface-reduced-motion"
       * does not exist
       */
      g_object_get (gtk_widget_get_settings (GTK_WIDGET (self)),
                    "gtk-enable-animations", &increase_duration,
                    NULL);

      g_object_get (gtk_widget_get_settings (GTK_WIDGET (self)),
                    "gtk-interface-reduced-motion", &reduced_motion,
                    NULL);

      if (reduced_motion == GTK_REDUCED_MOTION_REDUCE)
        increase_duration = FALSE;

      animation_target = adw_callback_animation_target_new (animate_row_scroll_cb, self, NULL);

      self->row_offset_animation = adw_timed_animation_new (GTK_WIDGET (self),
                                                            self->row_offset,
                                                            n_rows,
                                                            increase_duration ? 500 : 200, // ms
                                                            g_steal_pointer (&animation_target));
      adw_timed_animation_set_easing (ADW_TIMED_ANIMATION (self->row_offset_animation),
                                      ADW_EASE_OUT_QUART);
      adw_animation_set_follow_enable_animations_setting (self->row_offset_animation, FALSE);

      g_signal_connect (self->row_offset_animation,
                        "done",
                        G_CALLBACK (on_row_offset_animation_done),
                        self);

      last_offset_location = g_malloc (sizeof (gdouble));
      *last_offset_location = self->row_offset;
      g_object_set_data_full (G_OBJECT (self->row_offset_animation),
                              "last-offset",
                              g_steal_pointer (&last_offset_location),
                              g_free);

      adw_animation_play (self->row_offset_animation);
    }
  else
    {
      gdouble target_value;

      target_value = adw_timed_animation_get_value_to (ADW_TIMED_ANIMATION (self->row_offset_animation));

      adw_animation_pause (self->row_offset_animation);
      adw_timed_animation_set_value_to (ADW_TIMED_ANIMATION (self->row_offset_animation), target_value + n_rows);
      adw_animation_play (self->row_offset_animation);
    }

  GCAL_EXIT;
}

static void
update_selection_range (GcalMonthView *self)
{
  g_autoptr (GcalRange) selection_range = NULL;
  GDateTime *selection_start;
  GDateTime *selection_end;

  selection_start = self->selection.start;
  selection_end = self->selection.end;

  if (selection_start)
    {
      if (!selection_end)
        selection_end = selection_start;

      /* Swap dates if end is before start */
      if (gcal_date_time_compare_date (selection_start, selection_end) > 0)
        {
          GDateTime *aux = selection_end;
          selection_end = selection_start;
          selection_start = aux;
        }

      selection_range = gcal_range_new (selection_start, selection_end, GCAL_RANGE_DATE_ONLY);
    }
  else
    {
      g_assert (selection_end == NULL);
      selection_range = NULL;
    }

  for (gint i = 0; i < self->week_rows->len; i++)
    {
      GcalMonthViewRow *row = g_ptr_array_index (self->week_rows, i);
      gcal_month_view_row_update_selection (row, selection_range);
    }
}

static gboolean
child_focus_from_keyval (GtkWidget *widget,
                         guint      keyval)
{
  switch (keyval)
    {
    case GDK_KEY_Up:
    case GDK_KEY_KP_Up:
      return gtk_widget_child_focus (widget, GTK_DIR_UP);

    case GDK_KEY_Right:
    case GDK_KEY_KP_Right:
      return gtk_widget_child_focus (widget, GTK_DIR_RIGHT);

    case GDK_KEY_Down:
    case GDK_KEY_KP_Down:
      return gtk_widget_child_focus (widget, GTK_DIR_DOWN);

    case GDK_KEY_Left:
    case GDK_KEY_KP_Left:
      return gtk_widget_child_focus (widget, GTK_DIR_LEFT);

    default:
      return FALSE;
    }
}

static void
trigger_scroll (GcalMonthView *self,
                gint           n_rows)
{
  gtk_widget_add_css_class (GTK_WIDGET (self), "scrolling");

  maybe_popdown_overflow_popover (self);
  cancel_row_offset_animation (self);
  cancel_deceleration (self);

  animate_row_scroll (self, n_rows);
}

static gint
compute_horizontal_axis_of_cell (GcalMonthView *self,
                                 GtkWidget     *cell)
{
  gint cell_center;
  graphene_point_t point;

  cell_center = gtk_widget_get_width (cell) / 2;

  if (!gtk_widget_compute_point (cell, GTK_WIDGET (self),
                                 &GRAPHENE_POINT_INIT (cell_center, 0),
                                 &point))
    g_assert_not_reached ();

  return point.x;
}

static gboolean
focus_month_cell (GcalMonthView    *self,
                  GtkWidget        *cell,
                  GtkDirectionType  direction)
{
  GtkWidget *row, *new_row, *new_cell;
  GtkDirectionType lateral;
  gboolean is_rtl;
  gint n_rows, new_cell_x;
  guint row_index, new_row_index;

  row = gtk_widget_get_ancestor (cell, GCAL_TYPE_MONTH_VIEW_ROW);

  if (gtk_widget_child_focus (row, direction))
    return TRUE;

  is_rtl = gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL;

  switch (direction)
    {
    case GTK_DIR_LEFT:
      lateral = is_rtl ? GTK_DIR_DOWN : GTK_DIR_UP;
      break;
    case GTK_DIR_RIGHT:
      lateral = is_rtl ? GTK_DIR_UP : GTK_DIR_DOWN;
      break;
    default:
      g_assert (direction == GTK_DIR_UP || direction == GTK_DIR_DOWN);
      lateral = direction;
      break;
    }

  if (!g_ptr_array_find (self->week_rows, row, &row_index))
    g_assert_not_reached ();

  if (direction == GTK_DIR_LEFT)
    new_cell_x = gtk_widget_get_width (row) - gtk_widget_get_width (cell) / 2;
  else if (direction == GTK_DIR_RIGHT)
    new_cell_x = gtk_widget_get_width (cell) / 2;
  else
    new_cell_x = compute_horizontal_axis_of_cell (self, cell);

  if (lateral == GTK_DIR_UP)
    n_rows = -1;
  else
    n_rows = 1;

  new_row_index = row_index + n_rows;
  new_row = g_ptr_array_index (self->week_rows, new_row_index);
  new_cell = gcal_month_view_row_get_cell_at_x (GCAL_MONTH_VIEW_ROW (new_row), new_cell_x);

  if (row_index < FIRST_VISIBLE_ROW_INDEX || row_index > LAST_VISIBLE_ROW_INDEX)
    return TRUE;

  if (new_row_index < FIRST_VISIBLE_ROW_INDEX || new_row_index > LAST_VISIBLE_ROW_INDEX)
    trigger_scroll (self, n_rows);

  return gtk_widget_grab_focus (new_cell);
}



/*
 * Callbacks
 */

static void
on_click_gesture_pressed_cb (GtkGestureClick *click_gesture,
                             gint             n_press,
                             gdouble          x,
                             gdouble          y,
                             GcalMonthView   *self)
{
  GtkWidget *widget_at_position;
  GtkWidget *day_cell;

  GCAL_ENTRY;

  widget_at_position = gtk_widget_pick (GTK_WIDGET (self), x, y, GTK_PICK_DEFAULT);
  day_cell = gtk_widget_get_ancestor (widget_at_position, GCAL_TYPE_MONTH_CELL);

  if (!day_cell)
    GCAL_RETURN ();

  g_assert (GCAL_IS_MONTH_CELL (day_cell));

  gcal_clear_date_time (&self->selection.start);
  self->selection.start = g_date_time_ref (gcal_month_cell_get_date (GCAL_MONTH_CELL (day_cell)));

  gtk_event_controller_set_propagation_phase (self->motion_controller, GTK_PHASE_BUBBLE);

  update_selection_range (self);

  GCAL_EXIT;
}

static void
on_click_gesture_released_cb (GtkGestureClick *click_gesture,
                              gint             n_press,
                              gdouble          x,
                              gdouble          y,
                              GcalMonthView   *self)
{
  g_autoptr (GcalRange) selection_range = NULL;
  g_autoptr (GDateTime) selection_start = NULL;
  g_autoptr (GDateTime) selection_end = NULL;
  GtkWidget *widget_at_position, *cell, *row;
  GtkRoot *root;

  GCAL_ENTRY;

  gtk_event_controller_set_propagation_phase (self->motion_controller, GTK_PHASE_NONE);

  widget_at_position = gtk_widget_pick (GTK_WIDGET (self), x, y, GTK_PICK_DEFAULT);

  if (!self->selection.start ||
      !widget_at_position ||
      !(row = gtk_widget_get_ancestor (widget_at_position, GCAL_TYPE_MONTH_VIEW_ROW)))
    {
      gcal_view_clear_marks (GCAL_VIEW (self));
      GCAL_RETURN ();
    }

  root = gtk_widget_get_root (GTK_WIDGET (self));
  cell = gcal_month_view_row_get_cell_at_x (GCAL_MONTH_VIEW_ROW (row), x);
  gtk_root_set_focus (root, cell);

  selection_start = self->selection.start;
  selection_end = self->selection.end ?: selection_start;

  /* Swap dates if end is before start */
  if (gcal_date_time_compare_date (selection_start, selection_end) > 0)
    {
      GDateTime *aux = selection_end;
      selection_end = selection_start;
      selection_start = aux;
    }

  selection_start = g_date_time_ref (selection_start);
  selection_end = g_date_time_add_days (selection_end, 1);

  selection_range = gcal_range_new (selection_start, selection_end, GCAL_RANGE_DATE_ONLY);
  gcal_view_create_event (GCAL_VIEW (self), selection_range, x, y);

  GCAL_EXIT;
}

static void
on_motion_controller_motion_cb (GtkEventControllerMotion *motion_controller,
                                gdouble                   x,
                                gdouble                   y,
                                GcalMonthView            *self)
{
  GtkWidget *widget_at_position;
  GtkWidget *day_cell;
  GtkWidget *row;

  GCAL_ENTRY;

  widget_at_position = gtk_widget_pick (GTK_WIDGET (self), x, y, GTK_PICK_DEFAULT);

  if (!widget_at_position)
    GCAL_RETURN ();

  row = gtk_widget_get_ancestor (widget_at_position, GCAL_TYPE_MONTH_VIEW_ROW);

  if (!row)
    GCAL_RETURN ();

  day_cell = gcal_month_view_row_get_cell_at_x (GCAL_MONTH_VIEW_ROW (row), x);

  g_assert (day_cell != NULL);
  g_assert (GCAL_IS_MONTH_CELL (day_cell));

  gcal_clear_date_time (&self->selection.end);
  self->selection.end = g_date_time_ref (gcal_month_cell_get_date (GCAL_MONTH_CELL (day_cell)));

  update_selection_range (self);

  GCAL_EXIT;
}

static void
on_event_widget_activated_cb (GcalMonthViewRow *row,
                              GcalEventWidget  *event_widget,
                              GcalMonthView    *self)
{
  gcal_view_event_activated (GCAL_VIEW (self), event_widget);
}

static void
on_month_row_cell_activated_cb (GcalMonthViewRow *row,
                                GcalMonthCell    *cell,
                                GcalMonthView    *self)
{
  g_autoptr (GcalRange) selection_range = NULL;
  g_autoptr (GDateTime) selection_start = NULL;
  g_autoptr (GDateTime) selection_end = NULL;
  gint cell_width, cell_height;
  graphene_point_t point;

  GCAL_ENTRY;

  self->selection.end = g_date_time_ref (gcal_month_cell_get_date (cell));

  selection_start = self->selection.start ?: self->selection.end;
  selection_end = self->selection.end;

  /* Swap dates if end is before start */
  if (gcal_date_time_compare_date (selection_start, selection_end) > 0)
    {
      GDateTime *aux = selection_end;
      selection_end = selection_start;
      selection_start = aux;
    }

  cell_width = gtk_widget_get_width (GTK_WIDGET (cell));
  cell_height = gtk_widget_get_height (GTK_WIDGET (cell));

  selection_start = g_date_time_ref (selection_start);
  selection_end = g_date_time_add_days (selection_end, 1);

  if (!gtk_widget_compute_point (GTK_WIDGET (cell), GTK_WIDGET (self),
                                 &GRAPHENE_POINT_INIT (cell_width / 2, cell_height - CELL_ACTIVATED_OFFSET),
                                 &point))
    g_assert_not_reached ();

  selection_range = gcal_range_new (selection_start, selection_end, GCAL_RANGE_DATE_ONLY);
  gcal_view_create_event (GCAL_VIEW (self), selection_range, point.x, point.y);

  GCAL_EXIT;
}

static gboolean
on_key_controller_key_pressed_cb (GtkEventControllerKey *event_controller,
                                  guint                  keyval,
                                  guint                  keycode,
                                  GdkModifierType        state,
                                  GcalMonthView         *self)
{
  GDateTime *date = NULL;
  GtkWidget *focused_widget;
  GtkRoot *root;

  root = gtk_widget_get_root (GTK_WIDGET (self));
  focused_widget = gtk_root_get_focus (root);

  self->state = state;

  if (GCAL_IS_MONTH_CELL (focused_widget) && (state & GDK_SHIFT_MASK))
    {
      if (keyval == GDK_KEY_Escape)
        {
          gcal_view_clear_marks (GCAL_VIEW (self));
          return GDK_EVENT_STOP;
        }

      if (!child_focus_from_keyval (GTK_WIDGET (self), keyval))
        return GDK_EVENT_PROPAGATE;

      if (!self->selection.start)
        {
          date = gcal_month_cell_get_date (GCAL_MONTH_CELL (focused_widget));
          self->selection.start = g_date_time_ref (date);

          update_selection_range (self);
        }

      focused_widget = gtk_root_get_focus (root);

      g_assert (GCAL_IS_MONTH_CELL (focused_widget));

      gcal_clear_date_time (&self->selection.end);
      date = gcal_month_cell_get_date (GCAL_MONTH_CELL (focused_widget));
      self->selection.end = g_date_time_ref (date);

      update_selection_range (self);
    }

  return GDK_EVENT_PROPAGATE;
}

static void
on_key_controller_key_released_cb (GtkEventControllerKey *event_controller,
                                   guint                  keyval,
                                   guint                  keycode,
                                   GdkModifierType        state,
                                   GcalMonthView         *self)
{
  GtkWidget *focused_widget;
  GtkRoot *root;

  root = gtk_widget_get_root (GTK_WIDGET (self));
  focused_widget = gtk_root_get_focus (root);

  if (GCAL_IS_MONTH_CELL (focused_widget))
    {
      switch (keyval)
        {
        case GDK_KEY_Shift_L:
        case GDK_KEY_Shift_R:
        case GDK_KEY_Shift_Lock:
          if (!self->selection.end)
            break;

          gtk_widget_activate (focused_widget);
          break;
        default:
          break;
        }
    }
}

static void
on_scroll_controller_scroll_begin_cb (GtkEventControllerScroll *scroll_controller,
                                      GcalMonthView            *self)
{
  GdkEvent *event;

  GCAL_ENTRY;

  event = gtk_event_controller_get_current_event (GTK_EVENT_CONTROLLER (scroll_controller));
  if (gdk_event_get_event_type (event) != GDK_TOUCHPAD_HOLD ||
      gdk_touchpad_event_get_n_fingers (event) > 1)
    {
      gtk_widget_add_css_class (GTK_WIDGET (self), "scrolling");
      cancel_row_offset_animation (self);
      cancel_deceleration (self);
    }

  GCAL_EXIT;
}

static gboolean
on_scroll_controller_scroll_cb (GtkEventControllerScroll *scroll_controller,
                                gdouble                   dx,
                                gdouble                   dy,
                                GcalMonthView            *self)
{
  GdkEvent *current_event;

  GCAL_ENTRY;

  current_event = gtk_event_controller_get_current_event (GTK_EVENT_CONTROLLER (scroll_controller));

  switch (gdk_scroll_event_get_direction (current_event))
    {
    case GDK_SCROLL_SMOOTH:
      cancel_row_offset_animation (self);
      cancel_deceleration (self);
      offset_and_shuffle_rows_by_pixels (self, dy);
      GCAL_RETURN (GDK_EVENT_STOP);

    default:
      GCAL_RETURN (GDK_EVENT_PROPAGATE);
    }
}

static gboolean
on_discrete_scroll_controller_scroll_cb (GtkEventControllerScroll *scroll_controller,
                                         gdouble                   dx,
                                         gdouble                   dy,
                                         GcalMonthView            *self)
{
  GdkEvent *current_event;
  gint n_rows = 0;

  GCAL_ENTRY;

  current_event = gtk_event_controller_get_current_event (GTK_EVENT_CONTROLLER (scroll_controller));

  switch (gdk_scroll_event_get_direction (current_event))
    {
    case GDK_SCROLL_UP:
      n_rows = -1;
      break;

    case GDK_SCROLL_DOWN:
      n_rows = 1;
      break;

    default:
      GCAL_RETURN (GDK_EVENT_PROPAGATE);
    }

  trigger_scroll (self, n_rows);

  GCAL_RETURN (GDK_EVENT_STOP);
}

static void
on_scroll_controller_scroll_end_cb (GtkEventControllerScroll *scroll_controller,
                                    GcalMonthView            *self)
{
  GCAL_ENTRY;

  snap_to_top_row (self);

  GCAL_EXIT;
}

static void
decelerate_scroll_cb (gdouble  value,
                      gpointer user_data)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (user_data);
  gdouble dy;

  dy = self->last_velocity - value;
  self->last_velocity = value;

  if (fabs (value) > (get_grid_height (self) / (gdouble) N_ROWS_PER_PAGE / 5.0))
    offset_and_shuffle_rows_by_pixels (self, dy);
  else
    adw_animation_skip (self->kinetic_scroll_animation);
}

static void
on_kinetic_scroll_done_cb (AdwAnimation  *animation,
                           GcalMonthView *self)
{
  GCAL_ENTRY;

  snap_to_top_row (self);

  GCAL_EXIT;
}

static void
on_scroll_controller_decelerate_cb (GtkEventControllerScroll *scroll_controller,
                                    gdouble                   velocity_x,
                                    gdouble                   velocity_y,
                                    GcalMonthView            *self)
{
  g_autoptr (AdwAnimationTarget) animation_target = NULL;
  gdouble row_height;
  gdouble duration;
  gint grid_height;

  GCAL_ENTRY;

  /* XXX: I don't understand, but this just feels better */
  velocity_y /= 2.0;

  cancel_row_offset_animation (self);
  cancel_deceleration (self);

  grid_height = get_grid_height (self);
  if (fabs (velocity_y) < (grid_height / (gdouble) N_ROWS_PER_PAGE / 5.0))
    {
      snap_to_top_row (self);
      GCAL_RETURN ();
    }

  duration = fabs (velocity_y) / (gdouble) gtk_widget_get_height (GTK_WIDGET (self));
  animation_target = adw_callback_animation_target_new (decelerate_scroll_cb, self, NULL);

  /* Hijack the velocity so that it always ends in an actual row */
  row_height = grid_height / (gdouble) N_ROWS_PER_PAGE;
  velocity_y -= fmod (velocity_y, row_height) + self->row_offset * row_height;

  g_assert (self->kinetic_scroll_animation == NULL);
  self->kinetic_scroll_animation = adw_timed_animation_new (GTK_WIDGET (self),
                                                            velocity_y,
                                                            0.0,
                                                            duration * 1000, // ms
                                                            g_steal_pointer (&animation_target));
  self->last_velocity = velocity_y;
  adw_timed_animation_set_easing (ADW_TIMED_ANIMATION (self->kinetic_scroll_animation),
                                  ADW_EASE_OUT_EXPO);
  adw_animation_set_follow_enable_animations_setting (self->kinetic_scroll_animation, FALSE);

  g_signal_connect (self->kinetic_scroll_animation,
                    "done",
                    G_CALLBACK (on_kinetic_scroll_done_cb),
                    self);

  adw_animation_play (self->kinetic_scroll_animation);

  GCAL_EXIT;
}

static void
on_month_row_show_overflow_cb (GcalMonthViewRow *row,
                               GcalMonthCell    *cell,
                               GcalMonthView    *self)
{
  GcalMonthPopover *popover;
  GtkWidget *focused;
  GtkRoot *root;

  GCAL_ENTRY;

  popover = GCAL_MONTH_POPOVER (self->overflow.popover);

  root = gtk_widget_get_root (GTK_WIDGET (self));
  focused = gtk_root_get_focus (root);

  g_set_weak_pointer (&self->last_focused_widget, focused);

  self->overflow.row = row;
  self->overflow.relative_to = GTK_WIDGET (cell);

  gcal_month_popover_set_date (popover, gcal_month_cell_get_date (cell));
  gcal_month_popover_popup (popover);

  /* FIXME: here we forcefully disable selection, but I wonder if there's a better
   * way to do it.
   */
  gtk_event_controller_set_propagation_phase (self->motion_controller, GTK_PHASE_NONE);
  gcal_view_clear_marks (GCAL_VIEW (self));

  GCAL_EXIT;
}

static void
on_month_popover_event_activated_cb (GcalMonthPopover *month_popover,
                                     GcalEventWidget  *event_widget,
                                     GcalMonthViewRow *self)
{
  gcal_view_event_activated (GCAL_VIEW (self), event_widget);
}

static void
on_month_popover_closed_cb (GcalMonthPopover *month_popover,
                            GcalMonthView    *self)
{
  if (self->last_focused_widget)
    {
      gtk_widget_grab_focus (self->last_focused_widget);
      g_clear_weak_pointer (&self->last_focused_widget);
    }
}


/*
 * GcalTimelineSubscriber iface
 */

static GcalRange*
gcal_month_view_get_range (GcalTimelineSubscriber *subscriber)
{
  g_autoptr (GcalRange) first_row_range = NULL;
  g_autoptr (GcalRange) last_row_range = NULL;
  GcalMonthView *self;

  self = GCAL_MONTH_VIEW (subscriber);

  first_row_range = gcal_month_view_row_get_range (g_ptr_array_index (self->week_rows, 0));
  last_row_range = gcal_month_view_row_get_range (g_ptr_array_index (self->week_rows,
                                                                     self->week_rows->len - 1));

  return gcal_range_union (first_row_range, last_row_range);
}

static void
gcal_month_view_set_model (GcalTimelineSubscriber *subscriber,
                           GListModel             *model)
{
  GcalMonthView *self;

  GCAL_ENTRY;

  self = GCAL_MONTH_VIEW (subscriber);

  for (guint i = 0; i < self->week_rows->len; i++)
    {
      GcalMonthViewRow *row = g_ptr_array_index (self->week_rows, i);

      gcal_month_view_row_set_model (row, model);
    }

  gcal_month_popover_set_model (GCAL_MONTH_POPOVER (self->overflow.popover), model);

  GCAL_EXIT;
}

static void
gcal_timeline_subscriber_interface_init (GcalTimelineSubscriberInterface *iface)
{
  iface->get_range = gcal_month_view_get_range;
  iface->set_model = gcal_month_view_set_model;
}


/*
 * GcalView interface
 */

static GDateTime*
gcal_month_view_get_date (GcalView *view)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (view);

  return self->date;
}

static void
gcal_month_view_set_date (GcalView  *view,
                          GDateTime *date)
{
  GcalMonthView *self;
  gboolean week_changed;

  GCAL_ENTRY;

  self = GCAL_MONTH_VIEW (view);

  week_changed = !self->date ||
                 !date ||
                 g_date_time_get_month (self->date) != g_date_time_get_month (date) ||
                 g_date_time_get_week_of_year (self->date) != g_date_time_get_week_of_year (date);

  if (!week_changed)
    GCAL_RETURN ();

#ifdef GCAL_ENABLE_TRACE
  {
    g_autofree gchar *new_date_string = g_date_time_format (date, "%x %X %z");
    GCAL_TRACE_MSG ("New date: %s", new_date_string);
  }
#endif

  update_week_ranges (self, date);
  update_visible_rows (self);

  gcal_timeline_subscriber_range_changed (GCAL_TIMELINE_SUBSCRIBER (view));

  GCAL_EXIT;
}

static void
gcal_month_view_clear_marks (GcalView *view)
{
  GcalMonthView *self;

  GCAL_ENTRY;

  self = GCAL_MONTH_VIEW (view);

  gcal_clear_date_time (&self->selection.start);
  gcal_clear_date_time (&self->selection.end);

  update_selection_range (self);

  GCAL_EXIT;
}

static GList*
gcal_month_view_get_children_by_uuid (GcalView              *view,
                                      GcalRecurrenceModType  mod,
                                      const gchar           *uuid)
{
  g_autoptr (GList) children = NULL;
  GcalMonthView *self;

  GCAL_ENTRY;

  self = GCAL_MONTH_VIEW (view);

  for (guint i = 0; i < self->week_rows->len; i++)
    {
      g_autoptr (GList) row_children = NULL;
      GcalMonthViewRow *row;

      row = g_ptr_array_index (self->week_rows, i);
      row_children = gcal_month_view_row_get_children_by_uuid (row, mod, uuid);

      children = g_list_concat (children, g_steal_pointer (&row_children));
    }

  GCAL_RETURN (g_steal_pointer (&children));
}

static GDateTime*
gcal_month_view_get_next_date (GcalView *view)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (view);
  g_autoptr (GDateTime) date = NULL;
  gint year, month;

  g_assert_nonnull (self->date);
  date = g_date_time_add_months (self->date, 1);
  g_date_time_get_ymd (date, &year, &month, NULL);

  return g_date_time_new_local (year, month, 1, 0, 0, 0);
}


static GDateTime*
gcal_month_view_get_previous_date (GcalView *view)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (view);
  g_autoptr (GDateTime) date = NULL;
  gint year, month;

  g_assert_nonnull (self->date);
  date = g_date_time_add_months (self->date, -1);
  g_date_time_get_ymd (date, &year, &month, NULL);

  return g_date_time_new_local (year, month, 1, 0, 0, 0);
}

static void
gcal_view_interface_init (GcalViewInterface *iface)
{
  iface->get_date = gcal_month_view_get_date;
  iface->set_date = gcal_month_view_set_date;
  iface->clear_marks = gcal_month_view_clear_marks;
  iface->get_children_by_uuid = gcal_month_view_get_children_by_uuid;
  iface->get_next_date = gcal_month_view_get_next_date;
  iface->get_previous_date = gcal_month_view_get_previous_date;
}


/*
 * GtkBuildable interface
 */

static GtkBuildableIface *parent_buildable_iface;

static void
gcal_month_view_add_child (GtkBuildable *buildable,
                           GtkBuilder   *builder,
                           GObject      *child,
                           const gchar  *type)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (buildable);

  if (g_strcmp0 (type, "header") == 0)
    {
      self->header = GTK_WIDGET (child);
      gtk_widget_set_parent (self->header, GTK_WIDGET (self));
    }
  else
    {
      parent_buildable_iface->add_child (buildable, builder, child, type);
    }
}

static void
gtk_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);

  iface->add_child = gcal_month_view_add_child;
}


/*
 * GtkWidget overrides
 */

static gboolean
gcal_month_view_focus (GtkWidget        *widget,
                       GtkDirectionType  direction)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (widget);
  GtkWidget *candidate = NULL;
  GtkWidget *focused, *row;
  GtkRoot *root;

  root = gtk_widget_get_root (widget);
  focused = gtk_root_get_focus (root);

  if (direction == GTK_DIR_TAB_FORWARD || direction == GTK_DIR_TAB_BACKWARD)
    {
      g_assert (focused == NULL || GTK_IS_WIDGET (focused));

      if (self->state & GDK_CONTROL_MASK && gtk_widget_is_ancestor (focused, widget))
        {
          g_assert_nonnull (focused);

          gcal_month_popover_popdown (GCAL_MONTH_POPOVER (self->overflow.popover));

          if (gtk_widget_is_ancestor (focused, self->overflow.popover))
            return gtk_widget_grab_focus (self->overflow.relative_to);

          row = gtk_widget_get_ancestor (focused, GCAL_TYPE_MONTH_VIEW_ROW);

          return gcal_month_view_row_focus_adjacent_cell (GCAL_MONTH_VIEW_ROW (row), focused);
        }
      else if (focused)
        {
          if (GCAL_IS_MONTH_CELL (focused))
            {
              gcal_view_clear_marks (GCAL_VIEW (self));
              return FALSE;
            }

          if (direction == GTK_DIR_TAB_FORWARD && GCAL_IS_EVENT_WIDGET (focused))
            {
              if (GTK_WIDGET_CLASS (gcal_month_view_parent_class)->focus (widget, direction))
                return TRUE;

              row = gtk_widget_get_ancestor (focused, GCAL_TYPE_MONTH_VIEW_ROW);
              return gcal_month_view_row_focus_adjacent_cell (GCAL_MONTH_VIEW_ROW (row), focused);
            }
        }

      return GTK_WIDGET_CLASS (gcal_month_view_parent_class)->focus (widget, direction);
    }

  if (focused && !gtk_widget_is_ancestor (focused, widget))
    return FALSE;

  do
    {
      if (!GTK_WIDGET_CLASS (gcal_month_view_parent_class)->focus (widget, direction))
        break;

      candidate = gtk_root_get_focus (root);
    }
  while (self->state & GDK_CONTROL_MASK);

  g_assert (candidate == NULL || GTK_IS_WIDGET (candidate));

  if (candidate == NULL)
    {
      if (GCAL_IS_MONTH_CELL (focused) && !(self->state & GDK_CONTROL_MASK))
        return focus_month_cell (self, focused, direction);
      else
        return gtk_widget_keynav_failed (widget, direction);
    }

  return TRUE;
}

static void
gcal_month_view_measure (GtkWidget      *widget,
                         GtkOrientation  orientation,
                         gint            for_size,
                         gint           *minimum,
                         gint           *natural,
                         gint           *minimum_baseline,
                         gint           *natural_baseline)
{
  GcalMonthView *self;
  gint natural_header_size;
  gint minimum_header_size;
  gint minimum_row_size = 0;
  gint natural_row_size = 0;

  self = GCAL_MONTH_VIEW (widget);

  gtk_widget_measure (self->header,
                      orientation,
                      for_size,
                      &minimum_header_size,
                      &natural_header_size,
                      NULL,
                      NULL);

  for (guint i = 0; i < self->week_rows->len; i++)
    {
      GtkWidget *row;
      gint row_minimum;
      gint row_natural;

      row = g_ptr_array_index (self->week_rows, i);

      if (!gtk_widget_should_layout (row))
        continue;

      gtk_widget_measure (row,
                          orientation,
                          for_size,
                          &row_minimum,
                          &row_natural,
                          NULL,
                          NULL);

      if (i == 0)
        {
          minimum_row_size = row_minimum;
          natural_row_size = row_natural;
        }
      else
        {
          minimum_row_size = MIN (minimum_row_size, row_minimum);
          natural_row_size = MIN (natural_row_size, row_natural);
        }
    }


  if (orientation == GTK_ORIENTATION_VERTICAL)
    {
      if (minimum)
        *minimum = minimum_header_size + minimum_row_size;

      if (natural)
        *natural = natural_header_size + natural_row_size;
    }
  else
    {
      if (minimum)
        *minimum = MAX(minimum_header_size, minimum_row_size);

      if (natural)
        *natural = MAX(natural_header_size, natural_row_size);
    }

  if (minimum_baseline)
    *minimum_baseline = -1;

  if (natural_baseline)
    *natural_baseline = -1;
}

static void
gcal_month_view_size_allocate (GtkWidget *widget,
                               gint       width,
                               gint       height,
                               gint       baseline)
{
  GcalMonthView *self;
  gdouble row_scroll_offset;
  gdouble row_height;
  gdouble y_offset;
  gint header_height;
  gint grid_height;

  self = GCAL_MONTH_VIEW (widget);

  gtk_widget_measure (self->header, GTK_ORIENTATION_VERTICAL, width, &header_height, NULL, NULL, NULL);
  gtk_widget_allocate (self->header, width, header_height, baseline, NULL);

  grid_height = height - header_height;
  row_height = grid_height / (gdouble) N_ROWS_PER_PAGE;
  row_scroll_offset = self->row_offset * row_height;
  y_offset = header_height - row_scroll_offset - (grid_height * ((N_PAGES - 1) / 2.0));

  for (guint i = 0; i < self->week_rows->len; i++)
    {
      GtkAllocation row_allocation;
      GtkWidget *row;

      row = g_ptr_array_index (self->week_rows, i);

      if (!gtk_widget_should_layout (row))
        continue;

#define ROW_Y(_i) (row_height * (_i) + y_offset)

      row_allocation.x = 0;
      row_allocation.y = round (ROW_Y (i));
      row_allocation.width = width;
      row_allocation.height = round (ROW_Y (i + 1)) - row_allocation.y;
      gcal_month_view_row_set_ceiled_height (GCAL_MONTH_VIEW_ROW (row), row_height < row_allocation.height);

#undef ROW_Y

      gtk_widget_size_allocate (row, &row_allocation, baseline);
    }

  /* Overflow popover */
  if (gtk_widget_should_layout (self->overflow.popover))
    allocate_overflow_popover (self, width, height, baseline);
}

static void
gcal_month_view_snapshot (GtkWidget   *widget,
                          GtkSnapshot *snapshot)
{
  GcalMonthView *self;
  gint header_height;
  gint height;
  gint width;

  self = GCAL_MONTH_VIEW (widget);

  /* Snapshot rows with a clip */
  width = gtk_widget_get_width (widget);
  height = gtk_widget_get_height (widget);
  header_height = gtk_widget_get_height (self->header);

  gtk_snapshot_push_clip (snapshot,
                          &GRAPHENE_RECT_INIT (0, header_height, width, height - header_height));
  for (guint i = 0; i < self->week_rows->len; i++)
    gtk_widget_snapshot_child (widget, g_ptr_array_index (self->week_rows, i), snapshot);

  gtk_snapshot_pop (snapshot);

  /* Snapshot header */
  gtk_widget_snapshot_child (widget, self->header, snapshot);

  /* Overflow popover */
  gtk_widget_snapshot_child (widget, self->overflow.popover, snapshot);
}


/*
 * GObject overrides
 */

static void
gcal_month_view_dispose (GObject *object)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (object);

  GCAL_ENTRY;

  g_clear_object (&self->kinetic_scroll_animation);
  g_clear_object (&self->row_offset_animation);

  g_clear_pointer (&self->overflow.popover, gtk_widget_unparent);
  g_clear_pointer (&self->header, gtk_widget_unparent);
  g_clear_pointer (&self->week_rows, g_ptr_array_unref);

  g_clear_weak_pointer (&self->last_focused_widget);

  G_OBJECT_CLASS (gcal_month_view_parent_class)->dispose (object);

  GCAL_EXIT;
}

static void
gcal_month_view_finalize (GObject *object)
{
  GcalMonthView *self = (GcalMonthView *)object;

  g_clear_object (&self->event_widget_pool);
  gcal_clear_date_time (&self->selection.start);
  gcal_clear_date_time (&self->selection.end);
  gcal_clear_date_time (&self->date);

  G_OBJECT_CLASS (gcal_month_view_parent_class)->finalize (object);
}

static void
gcal_month_view_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (object);

  switch (prop_id)
    {
    case PROP_DATE:
      g_value_set_boxed (value, self->date);
      break;

    case PROP_TIME_DIRECTION:
      g_value_set_enum (value, GTK_ORIENTATION_VERTICAL);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gcal_month_view_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GcalMonthView *self = GCAL_MONTH_VIEW (object);

  switch (prop_id)
    {
    case PROP_DATE:
      gcal_view_set_date (GCAL_VIEW (self), g_value_get_boxed (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gcal_month_view_class_init (GcalMonthViewClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->dispose = gcal_month_view_dispose;
  object_class->finalize = gcal_month_view_finalize;
  object_class->get_property = gcal_month_view_get_property;
  object_class->set_property = gcal_month_view_set_property;

  widget_class->focus = gcal_month_view_focus;
  widget_class->measure = gcal_month_view_measure;
  widget_class->size_allocate = gcal_month_view_size_allocate;
  widget_class->snapshot = gcal_month_view_snapshot;

  g_object_class_override_property (object_class, PROP_DATE, "active-date");
  g_object_class_override_property (object_class, PROP_TIME_DIRECTION, "time-direction");

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/views/gcal-month-view.ui");

  gtk_widget_class_bind_template_child (widget_class, GcalMonthView, motion_controller);

  gtk_widget_class_bind_template_child_full (widget_class, "label_0", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[0]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_1", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[1]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_2", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[2]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_3", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[3]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_4", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[4]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_5", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[5]));
  gtk_widget_class_bind_template_child_full (widget_class, "label_6", FALSE, G_STRUCT_OFFSET (GcalMonthView, weekday_label[6]));

  gtk_widget_class_bind_template_callback (widget_class, on_click_gesture_pressed_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_click_gesture_released_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_discrete_scroll_controller_scroll_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_motion_controller_motion_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_scroll_controller_scroll_begin_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_scroll_controller_scroll_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_scroll_controller_scroll_end_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_scroll_controller_decelerate_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_key_controller_key_pressed_cb);
  gtk_widget_class_bind_template_callback (widget_class, on_key_controller_key_released_cb);

  gtk_widget_class_set_css_name (widget_class, "calendar-view");
}

static void
gcal_month_view_init (GcalMonthView *self)
{
  g_autoptr (GDateTime) now = NULL;

  self->event_widget_pool = gcal_event_widget_pool_new ();

  gtk_widget_init_template (GTK_WIDGET (self));
  update_weekday_labels (self);

  gtk_accessible_update_relation (GTK_ACCESSIBLE (self),
                                  GTK_ACCESSIBLE_RELATION_COL_COUNT, N_WEEKDAYS,
                                  GTK_ACCESSIBLE_RELATION_ROW_COUNT, N_ROWS_PER_PAGE,
                                  -1);

  self->week_rows = g_ptr_array_new_full (N_TOTAL_ROWS, (GDestroyNotify) gtk_widget_unparent);
  for (gint i = 0; i < N_TOTAL_ROWS; i++)
    {
      GtkWidget *row = gcal_month_view_row_new (self->event_widget_pool);
      g_signal_connect (row, "event-activated", G_CALLBACK (on_event_widget_activated_cb), self);
      g_signal_connect (row, "cell-activated", G_CALLBACK (on_month_row_cell_activated_cb), self);
      g_signal_connect (row, "show-overflow", G_CALLBACK (on_month_row_show_overflow_cb), self);
      gtk_widget_set_parent (row, GTK_WIDGET (self));
      g_ptr_array_add (self->week_rows, row);
    }

  gtk_widget_insert_before (self->header, GTK_WIDGET (self), NULL);

  /* Overflow popover */
  self->overflow.popover = gcal_month_popover_new ();
  g_signal_connect (self->overflow.popover, "event-activated", G_CALLBACK (on_month_popover_event_activated_cb), self);
  g_signal_connect (self->overflow.popover, "closed", G_CALLBACK (on_month_popover_closed_cb), self);

  gtk_widget_set_parent (self->overflow.popover, GTK_WIDGET (self));

  now = g_date_time_new_now_local ();
  gcal_view_set_date (GCAL_VIEW (self), now);

  update_visible_rows (self);
}
