#include "test_littlefs_common.h"

/* Opt in locally; the v5.x unit-test-app doesn't load this repo's sdkconfig.defaults. */
#define ESP_LITTLEFS_RAW_API
#include "lfs.h"


const char littlefs_test_partition_label[] = "flash_test";
const char littlefs_test_hello_str[] = "Hello, World!\n";


void test_littlefs_create_file_with_text(const char* name, const char* text)
{
    printf("Writing to \"%s\"\n", name);
    FILE* f = fopen(name, "wb");
    TEST_ASSERT_NOT_NULL(f);
    TEST_ASSERT_TRUE(fputs(text, f) != EOF);
    TEST_ASSERT_EQUAL(0, fclose(f));
}

void test_littlefs_read_file(const char* filename)
{
    FILE* f = fopen(filename, "r");
    TEST_ASSERT_NOT_NULL(f);
    char buf[32] = { 0 };
    int cb = fread(buf, 1, sizeof(buf), f);
    TEST_ASSERT_EQUAL(strlen(littlefs_test_hello_str), cb);
    TEST_ASSERT_EQUAL(0, strcmp(littlefs_test_hello_str, buf));
    TEST_ASSERT_EQUAL(0, fclose(f));
}

void test_littlefs_read_file_with_content(const char* filename, const char* expected_content)
{
    FILE* f = fopen(filename, "r");
    TEST_ASSERT_NOT_NULL(f);
    char buf[32] = { 0 };
  
    const size_t expected_content_length = strlen(expected_content);
    size_t read_length = 0;
    int cb = 0;
    do {
       cb = fread(buf, 1, sizeof(buf), f);
       TEST_ASSERT_TRUE(read_length + cb <= expected_content_length);
       TEST_ASSERT_TRUE(memcmp(buf, &expected_content[read_length], cb) == 0);
       read_length += cb;
    } while(cb != 0);
    
    TEST_ASSERT_EQUAL(expected_content_length, read_length);
    TEST_ASSERT_EQUAL(0, fclose(f));
}

void test_setup() {
    esp_littlefs_format(littlefs_test_partition_label);
    const esp_vfs_littlefs_conf_t conf = {
        .base_path = littlefs_base_path,
        .partition_label = littlefs_test_partition_label,
        .format_if_mount_failed = true
    };
    TEST_ESP_OK(esp_vfs_littlefs_register(&conf));
    TEST_ASSERT_TRUE( heap_caps_check_integrity_all(true) );
    printf("Test setup complete.\n");
}

void test_teardown(){
    TEST_ESP_OK(esp_vfs_littlefs_unregister(littlefs_test_partition_label));
    TEST_ASSERT_TRUE( heap_caps_check_integrity_all(true) );
    printf("Test teardown complete.\n");
}

/* Same geometry and callbacks the VFS layer uses for the test partition. */
static int raw_part_err(esp_err_t err)
{
    return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO;
}

static int raw_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
    return raw_part_err(esp_partition_read(c->context, block * c->block_size + off, buffer, size));
}

static int raw_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
    return raw_part_err(esp_partition_write(c->context, block * c->block_size + off, buffer, size));
}

static int raw_erase(const struct lfs_config *c, lfs_block_t block)
{
    return raw_part_err(esp_partition_erase_range(c->context, block * c->block_size, c->block_size));
}

static int raw_sync(const struct lfs_config *c)
{
    (void)c;
    return LFS_ERR_OK;
}

void test_littlefs_get_raw_cfg(struct lfs_config *cfg)
{
    const esp_partition_t *part = get_test_data_partition();
    TEST_ASSERT_NOT_NULL(part);

    memset(cfg, 0, sizeof(*cfg));
    cfg->context        = (void *)part;
    cfg->read           = raw_read;
    cfg->prog           = raw_prog;
    cfg->erase          = raw_erase;
    cfg->sync           = raw_sync;
    cfg->read_size      = CONFIG_LITTLEFS_READ_SIZE;
    cfg->prog_size      = CONFIG_LITTLEFS_WRITE_SIZE;
    cfg->cache_size     = CONFIG_LITTLEFS_CACHE_SIZE;
    cfg->lookahead_size = CONFIG_LITTLEFS_LOOKAHEAD_SIZE;
    cfg->block_cycles   = CONFIG_LITTLEFS_BLOCK_CYCLES;
    cfg->block_size     = 4096;
    cfg->block_count    = part->size / cfg->block_size;
#if CONFIG_LITTLEFS_MULTIVERSION
#if CONFIG_LITTLEFS_DISK_VERSION_MOST_RECENT
    cfg->disk_version = 0;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_1
    cfg->disk_version = 0x00020001;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_0
    cfg->disk_version = 0x00020000;
#endif
#endif
}
