/*
 * WebSocket SSH Library - Shared utilities implementation
 *
 * Copyright (C) 2024 Stefy Lanza <stefy@nexlab.net> and SexHack.me
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "wssshlib.h"

char *read_config_value(const char *key) {
    char *home = getenv("HOME");
    if (!home) return NULL;
    char path[PATH_MAX];
    snprintf(path, sizeof(path), "%s/.config/wsssh/wsssh.conf", home);
    FILE *f = fopen(path, "r");
    if (!f) return NULL;
    char line[256];
    while (fgets(line, sizeof(line), f)) {
        if (strncmp(line, key, strlen(key)) == 0 && line[strlen(key)] == '=') {
            char *value = strdup(line + strlen(key) + 1);
            // remove newline
            value[strcspn(value, "\n")] = 0;
            fclose(f);
            return value;
        }
    }
    fclose(f);
    return NULL;
}

void print_trans_flag(void) {
    // Transgender pride flag colors using ANSI escape codes
    const char *colors[] = {
        "\033[48;5;117m",  // Light blue background
        "\033[48;5;218m",  // Pink background
        "\033[48;5;231m",  // White background
        "\033[48;5;218m",  // Pink background
        "\033[48;5;117m"   // Light blue background
    };
    const char *reset = "\033[0m";

    // Print 10 rows of colored blocks (double height)
    for (int i = 0; i < 10; i++) {
        int color_index = i / 2;  // Each color appears twice for double height
        if (color_index >= 5) color_index = 4;  // Ensure we don't go out of bounds
        printf("%s", colors[color_index]);
        for (int j = 0; j < 40; j++) {
            printf(" ");
        }
        printf("%s\n", reset);
    }
    printf("\n");
}

void print_palestinian_flag(void) {
    const char *reset = "\033[0m";
    const char *red = "\033[41m";
    const char *black = "\033[40m";
    const char *white = "\033[47m";
    const char *green = "\033[42m";

    int height = 12;
    int width = 40;

    for (int i = 0; i < height; i++) {
        int triangle_width;
        if (i <= 5) {
            triangle_width = (i + 1) * 2;
        } else {
            triangle_width = (height - i) * 2;
        }

        const char *stripe_color;
        if (i < 4) {
            stripe_color = black;
        } else if (i < 8) {
            stripe_color = white;
        } else {
            stripe_color = green;
        }

        printf("%s", red);
        for (int j = 0; j < triangle_width; j++) {
            printf(" ");
        }
        printf("%s", stripe_color);
        for (int j = triangle_width; j < width; j++) {
            printf(" ");
        }
        printf("%s\n", reset);
    }
    printf("\n");
}

int find_available_port() {
    struct sockaddr_in addr;
    int sock;
    int port = 0;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation failed");
        return 0;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    addr.sin_port = 0;

    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Bind failed");
        close(sock);
        return 0;
    }

    socklen_t len = sizeof(addr);
    if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) {
        perror("Getsockname failed");
        close(sock);
        return 0;
    }

    port = ntohs(addr.sin_port);
    close(sock);

    return port;
}

void generate_request_id(char *request_id, size_t size) {
    const char charset[] = "0123456789abcdef";
    for (size_t i = 0; i < size - 1; i++) {
        request_id[i] = charset[rand() % (sizeof(charset) - 1)];
    }
    request_id[size - 1] = '\0';
}