// ATENCIÓ! Este programa no fa ninguna gestió de errors. Si no troba els arxius on els espera, peta i au. Es una xicoteta ferramenta

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>

#define NUM_RESOURCES       8

#define DIAPO_ESPERAR		0
#define DIAPO_FADEIN		1
#define DIAPO_SHOW		    2
#define DIAPO_PRINT		    3
#define DIAPO_MUSICA		4
#define DIAPO_FADEOUT		5
#define DIAPO_FADEMUSIC	    6

const char *resources[NUM_RESOURCES] = {
    "../data/seqIN.seq",
    "../data/seq00.seq",
    "../data/seq05.seq",
    "../data/seq10.seq",
    "../data/seq15.seq",
    "../data/seq20.seq",
    "../data/seq25.seq",
    "../data/seq30.seq"
};

char *p = nullptr;

uint8_t getByte() { return *(p++); }

uint16_t getWord() { return getByte() + (getByte() << 8); }

std::string getString()
{
    uint8_t tamanyCadena = getByte();
    char filename[256];
    for (int i=0; i<tamanyCadena; i++) filename[i] = getByte();
    filename[tamanyCadena] = '\0';
    return std::string(filename);
}

int main(int argc, char *argv[])
{
    for (int i = 0; i<NUM_RESOURCES; ++i)
    {
        const char *resourcename = resources[i];
        FILE *f = fopen(resourcename, "rb");
        fseek(f, 0, SEEK_END);
        int filesize = ftell(f);
        fseek(f, 0, SEEK_SET);
        char *buffer = (char *)malloc(filesize);
        fread(buffer, filesize, 1, f);
        fclose(f);
        p = buffer;

        std::string filename(resourcename);
        size_t index = 0;
        index = filename.find(".seq", index);
        std::string new_filename(filename);
        new_filename.replace(index, 4, ".txt");
        FILE *fp = fopen(new_filename.c_str(), "w");

        int num_diapositives = getByte();
        //fprintf(fp, "%i\n", num_diapositives);

        while (num_diapositives>0)
        {
            const char tipo_diapositiva = getByte();
            switch ( tipo_diapositiva )
            {
                case DIAPO_ESPERAR:
                {
                    const uint16_t time = getWord();
                    fprintf(fp, "ESPERAR %i\n", time);
                    break;
                }
                case DIAPO_FADEIN:
                {
                    std::string filename = getString();
                    fprintf(fp, "FADEIN '%s'\n", filename.c_str());
                    break;
                }
                case DIAPO_SHOW:
                {
                    std::string filename = getString();
                    fprintf(fp, "SHOW '%s'\n", filename.c_str());
                    break;
                }
                case DIAPO_PRINT:
                {
                    const uint16_t x = getWord();
                    const uint16_t y = getWord();
                    const uint8_t col = getByte();
                    std::string text = getString();
                    fprintf(fp, "PRINT %i %i %i '%s'\n", x, y, col, text.c_str());
                    break;
                }
                case DIAPO_MUSICA:
                {
                    std::string filename = getString();
                    fprintf(fp, "PLAYMUSIC '%s'\n", filename.c_str());
                    break;
                }
                case DIAPO_FADEOUT:
                {
                    fprintf(fp, "FADEOUT\n");
                    break;
                }
                case DIAPO_FADEMUSIC:
                    fprintf(fp, "FADEOUTMUSIC\n");
                    break;
             }
             num_diapositives--;
        }
        fclose(fp);
        free(buffer);
    }
}