/*
 * DWN - Desktop Window Manager
 * retoor <retoor@molodetz.nl>
 * System tray widgets (WiFi, Audio, etc.) and XEmbed protocol
 */

#ifndef DWN_SYSTRAY_H
#define DWN_SYSTRAY_H

#include "dwn.h"
#include "slider.h"
#include <stdbool.h>

#define MAX_TRAY_ICONS 32
#define MAX_BATTERIES 4
#define TRAY_ICON_SIZE 22
#define TRAY_ICON_SPACING 4

#define SYSTEM_TRAY_REQUEST_DOCK    0
#define SYSTEM_TRAY_BEGIN_MESSAGE   1
#define SYSTEM_TRAY_CANCEL_MESSAGE  2

#define XEMBED_EMBEDDED_NOTIFY      0
#define XEMBED_WINDOW_ACTIVATE      1
#define XEMBED_WINDOW_DEACTIVATE    2
#define XEMBED_REQUEST_FOCUS        3
#define XEMBED_FOCUS_IN             4
#define XEMBED_FOCUS_OUT            5

#define XEMBED_MAPPED               (1 << 0)
#define XEMBED_PROTOCOL_VERSION     0

typedef struct {
    Window window;
    int x;
    int width;
    int height;
    bool mapped;
} TrayIcon;

extern TrayIcon tray_icons[MAX_TRAY_ICONS];
extern int tray_icon_count;
extern Window tray_selection_owner;

typedef struct {
    bool enabled;
    bool connected;
    char current_ssid[64];
    int signal_strength;
} WifiState;

typedef struct {
    int volume;
    bool muted;
} AudioState;

typedef struct {
    bool present;
    bool charging;
    int percentage;
    int time_remaining;
} BatteryState;

typedef struct {
    char name[32];
    bool present;
    bool charging;
    int percentage;
    unsigned long energy_now;
    unsigned long energy_full;
} SingleBattery;

typedef struct {
    int count;
    SingleBattery batteries[MAX_BATTERIES];
    bool any_charging;
    int combined_percentage;
} MultiBatteryState;

typedef struct {
    Window window;
    int x, y;
    int width, height;
    bool visible;
    bool dragging;
} VolumeSlider;

/* Generic slider wrapper for fade controls */
typedef struct {
    GenericSlider *slider;
    int icon_x;  /* Position for popup */
} FadeControl;

extern WifiState wifi_state;
extern AudioState audio_state;
extern BatteryState battery_state;
extern MultiBatteryState multi_battery_state;
extern VolumeSlider *volume_slider;

/* Fade control externs */
extern FadeControl fade_speed_control;
extern FadeControl fade_intensity_control;

void systray_init(void);
void systray_cleanup(void);

void wifi_update_state(void);
const char *wifi_get_icon(void);

void audio_update_state(void);
void audio_set_volume(int volume);
void audio_toggle_mute(void);
const char *audio_get_icon(void);

void battery_update_state(void);
const char *battery_get_icon(void);

VolumeSlider *volume_slider_create(int x, int y);
void volume_slider_destroy(VolumeSlider *slider);
void volume_slider_show(VolumeSlider *slider);
void volume_slider_hide(VolumeSlider *slider);
void volume_slider_render(VolumeSlider *slider);
void volume_slider_handle_click(VolumeSlider *slider, int x, int y);
void volume_slider_handle_motion(VolumeSlider *slider, int x, int y);
void volume_slider_handle_release(VolumeSlider *slider);

/* Fade control functions */
void fade_controls_init(void);
void fade_controls_cleanup(void);
void fade_controls_render(Panel *panel, int x);
int fade_controls_get_width(void);
void fade_controls_handle_click(int control_id, int x, int y, int button);
int fade_controls_hit_test(int x);
void fade_controls_hide_all(void);

void systray_render(Panel *panel, int x, int *width);
int systray_get_width(void);
void systray_handle_click(int x, int y, int button);
int systray_hit_test(int x);

void systray_update(void);

void systray_lock(void);
void systray_unlock(void);

BatteryState systray_get_battery_snapshot(void);
MultiBatteryState systray_get_multi_battery_snapshot(void);
AudioState systray_get_audio_snapshot(void);

void xembed_init(void);
void xembed_cleanup(void);
bool xembed_dock_icon(Window icon_win);
void xembed_remove_icon(Window icon_win);
TrayIcon *xembed_find_icon(Window icon_win);
void xembed_send_message(Window icon, long message, long detail, long data1, long data2);
int xembed_get_icons_width(void);
void xembed_render_icons(Panel *panel, int x);
void xembed_handle_click(int x, int y, int button);
int xembed_hit_test(int x);
void xembed_update_icon_state(Window icon_win);

#endif
