#define OPENMOUSE_MOUSESEND_TEST 1
#include "../native/openmouse-mousesend.m"

static int failures = 0;

static void expect_click_state(const char *label,
                               const char *action,
                               CGMouseButton button,
                               double x,
                               double y,
                               double eventMs,
                               double intervalMs,
                               int expected) {
  int actual = click_state_for_event(action, button, x, y, eventMs, intervalMs);
  if (actual != expected) {
    fprintf(stderr, "%s: expected clickState=%d got=%d\n", label, expected, actual);
    failures += 1;
  }
}

int main(void) {
  reset_click_tracker();
  expect_click_state("first down", "down", kCGMouseButtonLeft, 100, 200, 1000, 500, 1);
  expect_click_state("first up", "up", kCGMouseButtonLeft, 100, 200, 1020, 500, 1);
  expect_click_state("second down", "down", kCGMouseButtonLeft, 100, 200, 1200, 500, 2);
  expect_click_state("second up", "up", kCGMouseButtonLeft, 100, 200, 1220, 500, 2);
  expect_click_state("third down", "down", kCGMouseButtonLeft, 100, 200, 1400, 500, 3);
  expect_click_state("third up", "up", kCGMouseButtonLeft, 100, 200, 1420, 500, 3);

  reset_click_tracker();
  expect_click_state("slow second down", "down", kCGMouseButtonLeft, 100, 200, 1000, 500, 1);
  expect_click_state("slow second up", "up", kCGMouseButtonLeft, 100, 200, 1020, 500, 1);
  expect_click_state("slow reset down", "down", kCGMouseButtonLeft, 100, 200, 1601, 500, 1);

  reset_click_tracker();
  expect_click_state("far first down", "down", kCGMouseButtonLeft, 100, 200, 1000, 500, 1);
  expect_click_state("far first up", "up", kCGMouseButtonLeft, 100, 200, 1020, 500, 1);
  expect_click_state("far reset down", "down", kCGMouseButtonLeft, 120, 200, 1200, 500, 1);

  reset_click_tracker();
  expect_click_state("button first down", "down", kCGMouseButtonLeft, 100, 200, 1000, 500, 1);
  expect_click_state("button first up", "up", kCGMouseButtonLeft, 100, 200, 1020, 500, 1);
  expect_click_state("different button reset down", "down", kCGMouseButtonRight, 100, 200, 1200, 500, 1);

  reset_click_tracker();
  expect_click_state("move has no click state", "move", kCGMouseButtonLeft, 100, 200, 1000, 500, 0);
  expect_click_state("drag has no click state", "drag", kCGMouseButtonLeft, 100, 200, 1020, 500, 0);

  if (failures) {
    return 1;
  }

  printf("mouse click state tests passed\n");
  return 0;
}
