/* This file is part of dedit - a tool for editing Teeworlds/DDNet demo files
   Copyright (C) 2024, 2026 JHHM

   Dedit 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.

   Dedit 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 dedit.  If not, see <https://www.gnu.org/licenses/>. */

#define _POSIX_C_SOURCE 200112L

#include "args.h"
#include "chat.h"
#include "commands.h"
#include "demo.h"
#include "huffman.h"
#include "merge.h"

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

int mapname(char *mappath, char *out)
{
    int start = 0;
    int end;
    int i = 0;
    while (1)
    {
        if (mappath[i] == '/')
            start = i + 1;
        if (mappath[i] == '.' || mappath[i] == '\0')
        {
            end = i;
            break;
        }
        i++;
    }

    if ((end - start) > 31)
        return 0;

    i = 0;
    while (start != end)
    {
        out[i] = mappath[start];
        start++;
        i++;
    }
    out[i] = '\0';
    return 1;
}

FILE *demofile = NULL, *outfile = NULL, *mapfile = NULL, *extmapfile = NULL, *mergefile = NULL;
demo DEMO;

void exitperror(char *str)
{
    perror(str);
    exit(EXIT_FAILURE);
}

void setdemo(arg *argument)
{
    demofile = fopen(argument->opts[0], "r");
    if (!demofile)
    {
        fprintf(stderr, "Error: failed to open demofile '%s', ", argument->opts[0]);
        exitperror("reason");
    }
    if (readdemo(demofile, &DEMO) < 0)
    {
        fprintf(stderr, "Error: failed to parse demo\n");
        exit(EXIT_FAILURE);
    }
    fclose(demofile);
}

void setmap(arg *argument)
{
    mapfile = fopen(argument->opts[0], "r");
    if (!mapfile)
        exitperror("Error: failed to open mapfile, reason");

    char mapnamebuff[32];
    if (mapname(argument->opts[0], mapnamebuff))
    {
        if (changemap(mapfile, mapnamebuff, &DEMO) < 1)
        {
            fprintf(stderr, "Error: failed to set map '%s'\n", argument->opts[0]);
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        fprintf(stderr, "Error: map name is too long (> 32)\n");
        exit(EXIT_FAILURE);
    }
    fclose(mapfile);
}

void runmerge(arg *mergearg)
{
    demo other;
    int ret;

    mergefile = fopen(mergearg->opts[0], "r");
    if (!mergefile)
    {
        fprintf(stderr, "Error: failed to open merge demofile '%s', ", mergearg->opts[0]);
        exitperror("reason");
    }
    if (readdemo(mergefile, &other) < 0)
    {
        fprintf(stderr, "Error: failed to parse merge demo '%s'\n", mergearg->opts[0]);
        exit(EXIT_FAILURE);
    }
    fclose(mergefile);

    ret = mergedemo(&DEMO, &other);
    if (ret < 0)
    {
        fprintf(stderr, "Error: failed to merge demo '%s'\n", mergearg->opts[0]);
        exit(EXIT_FAILURE);
    }

    freedemo(&other);
}

void runrename(arg *renamearg)
{
    int intid;
    int ret;

    if (sscanf(renamearg->opts[0], "%d", &intid) == 1)
        ret = setnamebyid(intid, renamearg->opts[1], &DEMO);
    else
        ret = setnamebyname(renamearg->opts[0], renamearg->opts[1], &DEMO);

    if (ret < 0)
    {
        printf("Error: failed to rename player '%s', reason: name too long\n", renamearg->opts[0]);
        exit(EXIT_FAILURE);
    }

    if (ret == 0)
    {
        printf("Info: found no player with id of '%s'", renamearg->opts[0]);
    }
}

void runsetskin(arg *skinarg)
{
    int intid;
    int ret;
    if (sscanf(skinarg->opts[0], "%d", &intid) == 1)
        ret = setskinbyid(intid, skinarg->opts[1], &DEMO);
    else
        ret = setskinbyname(skinarg->opts[0], skinarg->opts[1], &DEMO);

    if (ret < 0)
    {
        printf("Error: failed to set skin of player '%s', reason: skin name too long\n", skinarg->opts[0]);
        exit(EXIT_FAILURE);
    }

    if (ret == 0)
    {
        printf("Info: found no player with id of '%s'", skinarg->opts[0]);
    }
}

void runextractmap(arg *extarg)
{
    extmapfile = fopen(extarg->opts[0], "w");
    if (!extmapfile)
        exitperror("Error: failed to write to extract map file, reason");

    if (exportmap(extmapfile, &DEMO) < 0)
    {
        fprintf(stderr, "Error: failed to write demo map to extract map file\n");
        exit(EXIT_FAILURE);
    }
    fclose(extmapfile);
}

void runoutput(arg *outarg)
{
    outfile = fopen(outarg->opts[0], "w");
    if (!outfile)
        exitperror("Error: failed to write to outputfile, reason");

    if (writedemo(outfile, &DEMO) < 0)
    {
        fprintf(stderr, "Error: failed to write demo file to outputfile\n");
        exit(EXIT_FAILURE);
    }
    fclose(outfile);
}

int main(int argc, char *argv[])
{
    inithuff(NULL);

    setinfo("dedit - a tool for editing Teeworlds/DDNet demo files");
    setusage("dedit <demo> [OPTIONS]");

    addarg("<demo>", "Sets input demo", setdemo);

    addopt("-r", "--rename", 2, "<id/name> <name>", "Renames player with id/name to name", runrename);
    addopt("-s", "--skin", 2, "<id/name> <skin>", "Set skin of player with id/name to skin", runsetskin);
    addopt("-m", "--map", 1, "<map>", "Changes the map of demo to map", setmap);
    addopt("-M", "--merge", 1, "<demo>", "Merges the game data of demo into input demo", runmerge);
    addopt("-e", "--extract-map", 1, "<file>", "Saves the map of demo to file", NULL);
    addopt("-o", "--output", 1, "<file>", "Saves the output demo to file", NULL);
    addopt("-i", "--info", 0, NULL, "Prints info of demo", NULL);
    addopt("-I", "--extended-info", 0, NULL, "Prints extended info of demo", NULL);
    addopt("-c", "--chat", 0, NULL, "Prints the demo chat log", NULL);
    addopt("-h", "--help", 0, NULL, "Prints this help info", NULL);

    if (!parseargs(argc, argv))
    {
        paerror("Error:");
        printf("For more help run 'dedit --help'\n");
    }

    if (argc == 1 || getopt("-h", 0))
    {
        printhelp();
        exit(EXIT_SUCCESS);
    }

    arg *demoarg = getarg(0);
    if (demoarg == NULL)
    {
        printf("Error: Missing required argument <demo>\n");
        exit(EXIT_FAILURE);
    }

    demoarg->runarg(demoarg);

    int i = 0;
    arg *opt;
    while (1)
    {
        opt = getopt("-M", i);
        if (opt == NULL)
            break;
        opt->runarg(opt);
        i++;
    }

    arg *outarg = getopt("-o", 0);
    arg *maparg = getopt("-m", 0);
    arg *exmarg = getopt("-e", 0);
    arg *infarg = getopt("-i", 0);
    arg *Infarg = getopt("-I", 0);
    arg *chtarg = getopt("-c", 0);

    if (exmarg)
        runextractmap(exmarg);

    if (maparg)
        setmap(maparg);

    if (Infarg)
        printdemo(&DEMO, 1);
    else if (infarg)
        printdemo(&DEMO, 0);

    if (chtarg)
        printchat(&DEMO);

    while (1)
    {
        opt = getopt("-s", i);
        if (opt == NULL)
            break;
        opt->runarg(opt);
        i++;
    }

    i = 0;
    while (1)
    {
        opt = getopt("-r", i);
        if (opt == NULL)
            break;
        opt->runarg(opt);
        i++;
    }

    if (outarg)
        runoutput(outarg);

    freeargs();

    exit(EXIT_SUCCESS);
}
