#pragma once
#include <WiFiUdp.h>
#include <Preferences.h>
#include <ESPUI.h>
#include "Config.h"

// Global objects
extern WiFiUDP udp;
extern Preferences prefs;
extern int udpPort;

// Duty tracking + slew
extern float currentDuty[NUM_CHANNELS];
extern float targetDuty[NUM_CHANNELS];
extern float slewStartDuty[NUM_CHANNELS];

extern unsigned long slewStartTime;
extern unsigned long lastPacketTime;
extern unsigned long lastFadeTime;

// Calibration
extern float logicalPoints[5];
extern float calibratedPoints[NUM_CHANNELS][5];
extern bool overrideActive[NUM_CHANNELS];

// Lighting parameters (0–255 range for now)
extern uint8_t lightingHue;        // 0–255
extern uint8_t lightingSaturation; // 0–255
extern uint8_t lightingBrightness; // 0–255

// State
extern ConnectionState connectionState;

// UI IDs used by Core
extern uint16_t connectionStatusLabel;
extern uint16_t calChannelDropdown;
extern uint16_t calInputs[5];
extern uint16_t calTestValueInput;
extern uint16_t calSaveButton;
extern uint16_t calOverrideSwitch;
extern uint8_t selectedCalChannel;

// Core API
void coreInit();          // called from setup()
void coreHandleUDP();     // called from loop()
void coreUpdateState();   // called from loop()

// Helpers used by UI
float applyCalibration(uint8_t ch, float logicalDuty);
void updateConnectionStatusUI(ConnectionState state);
void applyLighting();
