#include "pen_channel.h"

#include <flutter/encodable_value.h>
#include <flutter/event_channel.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/flutter_engine.h>
#include <flutter/standard_method_codec.h>

#include <memory>

namespace {

std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> g_pen_sink;

std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> g_pen_channel;

// Diagnostic counters so the Dart side can see WHAT the observer receives:
//  - g_ptr_msgs:   WM_POINTER* messages seen (is WM_POINTER reaching us at all?)
//  - g_pen_msgs:   of those, PT_PEN with a successful GetPointerPenInfo
//  - g_mouse_msgs: legacy mouse/touch input messages (Flutter on the old path?)
int g_ptr_msgs = 0;
int g_pen_msgs = 0;
int g_mouse_msgs = 0;
int g_last_msg = 0;

// OR-accumulated raw flag fields, so a momentary button press is CAPTURED and
// held (a live readout would miss it). These are the ground truth for "which
// field/bit does the side button / eraser set?".
int g_ptr_flags_or = 0;   // POINTER_INFO.pointerFlags  (POINTER_FLAG_SECONDBUTTON = barrel on many pens)
int g_pen_flags_or = 0;   // POINTER_PEN_INFO.penFlags   (PEN_FLAG_BARREL/INVERTED/ERASER)
int g_pen_mask_or = 0;    // POINTER_PEN_INFO.penMask
int g_btn_change_last = 0; // last non-zero POINTER_INFO.ButtonChangeType
int g_tilt_abs_max = 0;   // max |tiltX|,|tiltY| seen

}  // namespace

void RegisterPenChannel(flutter::FlutterEngine* engine) {
  g_pen_channel =
      std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
          engine->messenger(), "badnote/pen",
          &flutter::StandardMethodCodec::GetInstance());

  auto handler = std::make_unique<
      flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
      [](const flutter::EncodableValue* arguments,
         std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&&
             events)
          -> std::unique_ptr<
              flutter::StreamHandlerError<flutter::EncodableValue>> {
        g_pen_sink = std::move(events);
        return nullptr;
      },
      [](const flutter::EncodableValue* arguments)
          -> std::unique_ptr<
              flutter::StreamHandlerError<flutter::EncodableValue>> {
        g_pen_sink = nullptr;
        return nullptr;
      });

  g_pen_channel->SetStreamHandler(std::move(handler));
}

void ObservePenMessage(UINT message, WPARAM wparam, LPARAM lparam) {
  if (!g_pen_sink) {
    return;
  }

  const bool is_pointer =
      message == WM_POINTERENTER || message == WM_POINTERDOWN ||
      message == WM_POINTERUPDATE || message == WM_POINTERUP;
  // Legacy input path: if Flutter is feeding us mouse/touch instead of pointer
  // messages, these tell us so (so we know WM_POINTER never arrives here).
  const bool is_legacy_input =
      message == WM_LBUTTONDOWN || message == WM_LBUTTONUP ||
      message == WM_MOUSEMOVE || message == WM_TOUCH;

  if (!is_pointer && !is_legacy_input) {
    return;
  }
  g_last_msg = static_cast<int>(message);
  if (is_legacy_input) {
    ++g_mouse_msgs;
  }

  int flags = 0;
  double tilt_x = 0.0;
  double tilt_y = 0.0;
  int raw_ptr_flags = 0;
  int raw_pen_flags = 0;
  int raw_pen_mask = 0;
  int btn_change = 0;

  if (is_pointer) {
    ++g_ptr_msgs;
    UINT32 pointerId = GET_POINTERID_WPARAM(wparam);
    POINTER_INPUT_TYPE type = PT_POINTER;
    if (GetPointerType(pointerId, &type) && type == PT_PEN) {
      POINTER_PEN_INFO ppi{};
      if (GetPointerPenInfo(pointerId, &ppi)) {
        ++g_pen_msgs;
        raw_pen_flags = static_cast<int>(ppi.penFlags);
        raw_pen_mask = static_cast<int>(ppi.penMask);
        raw_ptr_flags = static_cast<int>(ppi.pointerInfo.pointerFlags);
        btn_change = static_cast<int>(ppi.pointerInfo.ButtonChangeType);
        tilt_x = static_cast<double>(ppi.tiltX);
        tilt_y = static_cast<double>(ppi.tiltY);

        // Barrel/side button can arrive in EITHER penFlags (PEN_FLAG_BARREL) or
        // pointerFlags (POINTER_FLAG_SECONDBUTTON) depending on the pen/driver,
        // so check both. Eraser end = inverted/eraser pen flags.
        const bool barrel = (ppi.penFlags & PEN_FLAG_BARREL) ||
                            (ppi.pointerInfo.pointerFlags & POINTER_FLAG_SECONDBUTTON);
        const bool inverted = (ppi.penFlags & PEN_FLAG_INVERTED) != 0;
        const bool eraser = (ppi.penFlags & PEN_FLAG_ERASER) != 0;
        if (barrel)   flags |= 1;
        if (inverted) flags |= 2;
        if (eraser)   flags |= 4;

        g_ptr_flags_or |= raw_ptr_flags;
        g_pen_flags_or |= raw_pen_flags;
        g_pen_mask_or |= raw_pen_mask;
        if (btn_change != 0) g_btn_change_last = btn_change;
        const int ax = ppi.tiltX < 0 ? -ppi.tiltX : ppi.tiltX;
        const int ay = ppi.tiltY < 0 ? -ppi.tiltY : ppi.tiltY;
        if (ax > g_tilt_abs_max) g_tilt_abs_max = ax;
        if (ay > g_tilt_abs_max) g_tilt_abs_max = ay;
      }
    }
    if (message == WM_POINTERUP) {
      flags = 0;  // lift-off clears held flags
    }
  }

  // Emit the resolved flags/tilt PLUS the full raw + OR-accumulated diagnostic
  // set, so a single device session reveals exactly which field carries the
  // button and what tilt/mask the pen reports.
  flutter::EncodableMap payload{
      {flutter::EncodableValue("flags"), flutter::EncodableValue(flags)},
      {flutter::EncodableValue("tiltX"), flutter::EncodableValue(tilt_x)},
      {flutter::EncodableValue("tiltY"), flutter::EncodableValue(tilt_y)},
      {flutter::EncodableValue("diagPtr"), flutter::EncodableValue(g_ptr_msgs)},
      {flutter::EncodableValue("diagPen"), flutter::EncodableValue(g_pen_msgs)},
      {flutter::EncodableValue("diagMouse"), flutter::EncodableValue(g_mouse_msgs)},
      {flutter::EncodableValue("diagMsg"), flutter::EncodableValue(g_last_msg)},
      {flutter::EncodableValue("rawPtrFlags"), flutter::EncodableValue(raw_ptr_flags)},
      {flutter::EncodableValue("rawPenFlags"), flutter::EncodableValue(raw_pen_flags)},
      {flutter::EncodableValue("rawPenMask"), flutter::EncodableValue(raw_pen_mask)},
      {flutter::EncodableValue("btnChange"), flutter::EncodableValue(btn_change)},
      {flutter::EncodableValue("orPtrFlags"), flutter::EncodableValue(g_ptr_flags_or)},
      {flutter::EncodableValue("orPenFlags"), flutter::EncodableValue(g_pen_flags_or)},
      {flutter::EncodableValue("orPenMask"), flutter::EncodableValue(g_pen_mask_or)},
      {flutter::EncodableValue("btnChangeLast"), flutter::EncodableValue(g_btn_change_last)},
      {flutter::EncodableValue("tiltAbsMax"), flutter::EncodableValue(g_tilt_abs_max)},
  };
  g_pen_sink->Success(flutter::EncodableValue(payload));
}
