#ifndef LCD_MENU_UTILS_H
#define LCD_MENU_UTILS_H

#include "lcd_menu_constants.h"
#include <Arduino.h>

inline void substring(const char* str, uint8_t start, uint8_t size, char* substr) {
    strncpy(substr, str + start, size);
    substr[size] = '\0';
}

inline void concat(const char* first, char second, const char* third, char* result) {
    size_t len1 = strlen(first);
    size_t len3 = strlen(third);

    memcpy(result, first, len1);
    result[len1] = second;
    memcpy(result + len1 + 1, third, len3);
    result[len1 + 1 + len3] = '\0';
}

inline void concat(const char* first, char second, char* result) {
    size_t len1 = strlen(first);
    memcpy(result, first, len1);
    result[len1] = second;
    result[len1 + 1] = '\0';
}

inline void concat(const char* first, const char* second, char* result) {
    strcpy(result, first);
    strcat(result, second);
}

inline void remove(char* str, uint8_t index, uint8_t count) {
    uint8_t len = strlen(str);
    if (index + count > len) {
        count = len - index;
    }
    memmove(str + index, str + index + count, len - count - index + 1);
}

#ifdef DEBUG
#define LOG(...) log(__VA_ARGS__)
inline void log(const __FlashStringHelper* command) {
    String message(F("#LOG# "));
    message += command;
    message += '\n';
    Serial.print(message);
    Serial.flush();
}

template <typename T>
inline void log(const __FlashStringHelper* command, T value) {
    String message(F("#LOG# "));
    message += command;
    message += F("=");
    message += String(value);
    message += '\n';
    Serial.print(message);
    Serial.flush();
}
#else
#define LOG(...)  // No-op
#endif

#endif  // LCD_MENU_UTILS_H
