#include <iostream>
#include <string>

#include "print.hpp"
#include "file_types.hpp"

using namespace std;

bool verbose = false;
file input_file;
file output_file;
Logger logger;

int main(int argc, char *argv[]) {
	if (argc < 2) {
		logger.println("No arguments provided.");
		logger.println("Usage: \t%s [args...]", argv[0]);
		logger.println("\t-i <file>\tSpecify input file");
		logger.println("\t-o <file>\tSpecify output file");
		logger.println("\t-v \t\tEnable verbose mode");

		return 1;
	}

	for (int i = 1; i < argc; ++i) {
		string arg = argv[i];
		if (arg == "-i" && i + 1 < argc) {
            input_file.setFilename(argv[++i]);
            if (verbose)
				logger.log("Input file set to: %s\n", argv[i]);
		} else if (arg == "-o" && i + 1 < argc) {
            output_file.setFilename(argv[++i]);
			if (verbose)
				logger.log("Output file set to: %s\n", argv[i]);
		} else if (arg == "-v") {
			verbose = true;
			logger.log("Verbose mode enabled.\n");
		} else {
			logger.err("Unknown argument: %s \"%s\"\n", argv[i], (i + 1 < argc) ? argv[i + 1] : "");
			return 1;
		}
	}
    
    // INFO: Check if input files are video 
	if (input_file.getType() == unknown) {
        logger.err("Input file type is unknown or unsupported: %s\n", input_file.getFilename().c_str());
        return 1;
    } else {
        if (verbose)
            logger.log("Input file type recognized: %s\n", input_file.getFilename().c_str());
    }

    if (output_file.getType() == unknown) {
        logger.err("Output file type is unknown or unsupported: %s\n", output_file.getFilename().c_str());
        return 1;
    } else {
        if (verbose)
            logger.log("Output file type recognized: %s\n", output_file.getFilename().c_str());
    }

    if(!(input_file == output_file)) {
        logger.err("Input and output files have to be the same: %s != %s\n", input_file.getFilename().c_str(), output_file.getFilename().c_str());
        return 1;
    }

    if (verbose) {
        logger.log("Processing from %s to %s\n", input_file.getFilename().c_str(), output_file.getFilename().c_str());
    }

    return 0;
}
