#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_PI
//    #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 der Zielplattform */
const yespower_params_t *select_yespower_params(void) {
    #ifdef TARGET_RASPBERRY
        static const yespower_params_t params_raspberry = {
            .version = YESPOWER_1_0,
            .N = 2048, // Angepasster Wert für bessere Performance
            .r = 8,
            .pers = (const uint8_t *)"Raspberry",
            .perslen = 10 // "Raspberry" hat 10 Zeichen
        };
        //printf("Raspberry Pi Parameters: N=%d, r=%d\n", params_raspberry.N, params_raspberry.r);
        return &params_raspberry;

    #elif defined(TARGET_NOARM)
        static const yespower_params_t params_noarm = {
            .version = YESPOWER_1_0,
            .N = 4096,
            .r = 16,
            .pers = (const uint8_t *)"NoARM",
            .perslen = 5 // "NoARM" hat 5 Zeichen
        };
        printf("NoARM Parameters: N=%d, r=%d\n", params_noarm.N, params_noarm.r);
        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 // "Default" hat 7 Zeichen
        };
        printf("Default Parameters: N=%d, r=%d\n", params_default.N, params_default.r);
        return &params_default;
    #endif
}

/* Berechnung des Yespower-Hashes */
int yespower_hash(const char *input, char *output) {
    const yespower_params_t *params = select_yespower_params();
    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) {
    const yespower_params_t *params = select_yespower_params(); // Dynamische Parameter-Auswahl

    union {
        uint8_t u8[80]; // 20 * 4 bytes
        uint32_t u32[20];
    } data;

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

    uint32_t n = pdata[19] - 1;
    const uint32_t Htarg = ptarget[7];
    int i;

    unsigned long hash_count = 0;

    // Kodierung der Daten in Big-Endian
    for (i = 0; i < 20; i++)
        be32enc(&data.u32[i], pdata[i]);

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

        if (yespower_tls(data.u8, 80, params, &hash.yb))
            abort();
        
        hash_count++;
        if (hash_count % 450 == 0) {
            printf("Hash #%lu: %08x\n", hash_count, le32dec(&hash.u32[7]));
        }
        if (le32dec(&hash.u32[7]) <= Htarg) {
            for (i = 0; i < 8; i++)
                hash.u32[i] = le32dec(&hash.u32[i]);
            if (fulltest(hash.u32, ptarget)) {
                *hashes_done = n - pdata[19] + 1;
                pdata[19] = n;
                return 1;
            }
        }
    } while (n < max_nonce && !work_restart[thr_id].restart);

    *hashes_done = n - pdata[19] + 1;
    pdata[19] = n;
    return 0;
}
