/*
 * This file is part of the weatherdaemon project.
 * Copyright 2025 Edward V. Emelianov <edward.emelianoff@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/>.
 */

#pragma once

#include <pthread.h>
#include <signal.h> // pthread_kill
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <usefull_macros.h>

// length (in symbols) of key, value and comment
#define KEY_LEN         (8)
// length of STR type (without terminal zero)
#define STRT_LEN        (11)
#define VAL_LEN         (31)
#define COMMENT_LEN     (63)
// maximal full length of "KEY=val / comment" (as for sfitsio)
#define FULL_LEN        (81)
// name of meteo-plugin
#define NAME_LEN        (127)

// importance of values
typedef enum{
    VAL_FORCEDSHTDN,     // if this value is `terrible`, `forced sthudtown` flag will be set
    VAL_OBLIGATORY,     // can't be omitted
    VAL_RECOMMENDED,    // recommended to show
    VAL_UNNECESSARY,    // may be shown by user request
    VAL_BROKEN,         // sensor is broken, omit it
    VAL_AMOUNT          // amount of values
} valsense_t;

// meaning of values
typedef enum{
    IS_WIND,            // wind, m/s
    IS_WINDDIR,         // wind direction, degr
    IS_HUMIDITY,        // humidity, percent
    IS_AMB_TEMP,        // ambient temperature, degC
    IS_INNER_TEMP,      // in-dome temperature, degC
    IS_HW_TEMP,         // hardware (?) termperature, degC
    IS_PRESSURE,        // atmospheric pressure, mmHg
    IS_PRECIP,          // precipitation (1 - yes, 0 - no)
    IS_PRECIP_LEVEL,    // precipitation level (mm)
    IS_MIST,            // mist (1 - yes, 0 - no)
    IS_CLOUDS,          // integral clouds value (bigger - better)
    IS_SKYTEMP,         // mean sky temperatyre
    //IS_LIGTDIST,        // distance to lightning
    IS_OTHER,           // something other - read "name" and "comment"
    // values after `IS_OTHER` have no pre-defined names and comments!!!
    IS_BADWEATH,        // if meet this flag, set weather level to "BAD"
    IS_TERRIBLEWEATH,   // -//- "TERRIBLE"
    IS_FORCEDSHTDN,     // like "on battery" flag from UPS
} valmeaning_t;

typedef union{
    uint32_t u;
    int32_t i;
    float f;
    char str[STRT_LEN+1];// up to 8 symbols (with terminating zero)
} num_t;

// type of value
typedef enum{
    VALT_UINT,
    VALT_INT,
    VALT_FLOAT,
    VALT_STRING,
} valtype_t;

// value
typedef struct{
    char name[KEY_LEN+1];   // max VAL_LEN symbols FITS header keyword name
    char comment[COMMENT_LEN+1];// max COMMENT_LEN symbols of comment to FITS header
    valsense_t sense;       // importance
    valtype_t type;         // type of given value
    valmeaning_t meaning;   // what type of sensor is it
    num_t value;            // value itself
    time_t time;            // last changing time
} val_t;

// all sensor's data
// all functions have `this` as first arg
typedef struct sensordata_t{
    char name[NAME_LEN+1];  // sensor's name (e.g. "rain sensor @ localhost")
    char path[PATH_MAX];    // path to sensor's device or socket description in format D:/path, U:path, N:host:port
    int Nvalues;            // amount of values
    int PluginNo;           // plugin number in array (if several)
    int IsMuted;            // ==1 for "muted" station (don't refresh sensors' data)
    int (*init)(struct sensordata_t*); // main init function
    int (*onrefresh)(struct sensordata_t*, void (*handler)(struct sensordata_t*)); // handler of new data; return TRUE if OK
    int (*get_value)(struct sensordata_t*, val_t*, int); // getter of Nth value
    void (*kill)(struct sensordata_t*); // close everything and remove sensor
    // private members:
    val_t *values;          // array of values
    pthread_t thread;       // main thread
    pthread_mutex_t valmutex;// value getter/setter mutex
    // !!! if your plugin don't use file descriptor, you should set fdes to any non-negative value after running main thread
    int fdes;               // file descriptor of device/socket or "init" flag (should be > -1)
    sl_ringbuffer_t *ringbuffer; // ringbuffer for device reading
    time_t tpoll;           // forced polling time for sensor
    void (*freshdatahandler)(struct sensordata_t*); // handler of fresh data
    void (*privdatafree)(void*); // free private data (if don't wanna write own `kill` instead of `common kill`
    void *privdata;         // some private data like struct
} sensordata_t;

// type for function extraction
typedef int (*sensor_init_t)(sensordata_t *);

// init meteostation with given PluginNo, poll_interval and descriptor
//sensordata_t *sensor_init(int PluginNo, time_t poll_interval, const char *descr);  // external initial function for any plugin
int sensor_alive(sensordata_t *s);
sensordata_t *sensor_new(int N, const char *descr);

// private function (for plugins usage only)
void common_kill(sensordata_t *s);
int common_onrefresh(sensordata_t *s, void (*handler)(sensordata_t *));
int common_getval(struct sensordata_t *s, val_t *o, int N);

int getFD(const char *path);
