#include "cpuminer-config.h"
#include "version.h"
#include "miner.h"
#include <stdio.h>
#include "yespower-1.0.1/yespower.h"
#include "yespower-1.0.1/sysendian.h"
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#ifdef TARGET_RASPBERRY
    #define BLOCK_VERSION BLOCK_VERSION_RASPBERRY
#elif defined(TARGET_NOARM)
    #define BLOCK_VERSION BLOCK_VERSION_NOARM
#else
    #define BLOCK_VERSION BLOCK_VERSION_DEFAULT 
#endif

/* Auswahl der Yespower-Parameter basierend auf nVersion */
const yespower_params_t *select_yespower_params_by_version(int32_t nVersion) {
    if ((nVersion & BLOCK_VERSION_RASPBERRY) == BLOCK_VERSION_RASPBERRY) {
        static const yespower_params_t params_raspberry = {
            .version = YESPOWER_1_0,
            .N = 2048,
            .r = 8,
            .pers = (const uint8_t *)"Raspberry",
            .perslen = 9
        };
        return &params_raspberry;
    } else if ((nVersion & BLOCK_VERSION_NOARM) == BLOCK_VERSION_NOARM) {
        static const yespower_params_t params_noarm = {
            .version = YESPOWER_1_0,
            .N = 4096,
            .r = 16,
            .pers = (const uint8_t *)"NoARM",
            .perslen = 5
        };
        return &params_noarm;
    } else {
        static const yespower_params_t params_default = {
            .version = YESPOWER_1_0,
            .N = 4096,
            .r = 16,
            .pers = (const uint8_t *)"Default",
            .perslen = 7
        };
        return &params_default;
    }
}

/* Berechnung des Yespower-Hashes */
int yespower_hash(const char *input, char *output, int32_t nVersion) {
    const yespower_params_t *params = select_yespower_params_by_version(nVersion);
    return yespower_tls((const uint8_t *)input, 80, params, (yespower_binary_t *)output);
}

/* Scanhash-Funktion für ARM-Architektur mit Yespower */
int scanhash_arm_yespower(int thr_id, uint32_t *pdata, const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done, int32_t nVersion) {
    const yespower_params_t *params = select_yespower_params_by_version(nVersion);

    union {
        uint8_t u8[80]; // 80 Bytes Blockheader
        uint32_t u32[20];
    } data;

    union {
        yespower_binary_t yb;
        uint32_t u32[8];
    } hash;

    uint32_t n = pdata[19] - 1; // Start Nonce
    const uint32_t Htarg = ptarget[7]; // Zielwert (kleiner ist besser)
    int i;

    // Debug-Ausgabe: Parameter
    printf("Debug: Using Yespower parameters - N: %d, r: %d, pers: %s\n", params->N, params->r, params->pers);

    // Blockdaten in Big-Endian formatieren
    for (i = 0; i < 20; i++) {
        be32enc(&data.u32[i], pdata[i]);
    }

    do {
        be32enc(&data.u32[19], ++n); // Nonce inkrementieren

        if (yespower_tls(data.u8, 80, params, &hash.yb)) {
            abort(); // Fehlerbehandlung
        }

        // Debug-Ausgabe: Nonce und Hash
        printf("Debug: Nonce: %u, Hash: %08x%08x%08x%08x%08x%08x%08x%08x\n",
               n,
               hash.u32[0], hash.u32[1], hash.u32[2], hash.u32[3],
               hash.u32[4], hash.u32[5], hash.u32[6], hash.u32[7]);

        // Überprüfung des Hash-Werts
        if (le32dec(&hash.u32[7]) <= Htarg) {
            for (i = 0; i < 8; i++) {
                hash.u32[i] = le32dec(&hash.u32[i]); // Konvertiere zu Little-Endian
            }
            if (fulltest(hash.u32, ptarget)) {
                // Debug-Ausgabe: Erfolg
                printf("Debug: Valid block found! Nonce: %u\n", n);
                *hashes_done = n - pdata[19] + 1;
                pdata[19] = n;
                return 1; // Gültiger Block gefunden
            }
        }
    } while (n < max_nonce && !work_restart[thr_id].restart);

    // Debug-Ausgabe: Kein Block gefunden
    printf("Debug: No valid block found. Last Nonce: %u\n", n);

    *hashes_done = n - pdata[19] + 1;
    pdata[19] = n;
    return 0; // Kein gültiger Block gefunden
}
