package de.jeyp91;
import de.jeyp91.apifootball.APIFootballUpdater;
import de.jeyp91.tippliga.*;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

/**
 * Hello world!
 *
 */
public class App {

    private static String mode;
    private static int season;
    private static int league;
    private static String configFile;

    public static void main(String[] args) throws Exception {
        initOptions(args);
        String sql = "";
        switch(mode) {
            case "MatchdaysUpdater":
                TLWMatchdaysUpdater matchdaysUpdater = new TLWMatchdaysUpdater(season, league, configFile);
                sql = matchdaysUpdater.getUpdateSql();
                break;
            case "MatchesCreatorFootball":
                TLWMatchesCreatorFootball creator = new TLWMatchesCreatorFootball(season, league, configFile);
                sql = creator.getSQLInsertString();
                break;
            case "MatchesUpdaterFootball":
                TLWMatchesUpdaterFootball tlwMatchesUpdaterFootball = new TLWMatchesUpdaterFootball(season, league, configFile);
                sql = tlwMatchesUpdaterFootball.getUpdateSQL();
                break;
            case "TeamsUpdater":
                TLWTeamsUpdater teamsUpdater = new TLWTeamsUpdater(season, league, configFile);
                sql = teamsUpdater.getInsertSQL();
            case "APIFootballUpdater":
                APIFootballUpdater apiFootballUpdater = new APIFootballUpdater(season);
                apiFootballUpdater.updateAllFixtures(configFile);
            default:
                break;
        }
        if(!StatusHolder.getError() && !sql.equals("")) {
            TippligaSQLConnector con = new TippligaSQLConnector();
            con.executeQuery(sql);
        }
    }

    private static void initOptions(String[] args) {
        ArgumentParser parser = ArgumentParsers.newFor("tlw").build()
                .defaultHelp(true)
                .description("Test");

        parser.addArgument("-m", "--mode")
                .dest("mode")
                .choices("MatchdaysUpdater", "MatchesCreatorFootball", "MatchesUpdaterFootball", "TeamsUpdater", "APIFootballUpdater")
                .help("")
                .required(true)
                .type(String.class);

        parser.addArgument("-s", "--season")
                .dest("season")
                .required(true)
                .type(Integer.class);

        parser.addArgument("-l", "--league")
                .dest("league")
                .required(true)
                .type(Integer.class);

        parser.addArgument("-c", "--configFile")
                .dest("configFile")
                .required(true)
                .type(String.class);

        Namespace res = null;
        try {
            res = parser.parseArgs(args);
        } catch (ArgumentParserException e) {
            parser.printHelp();
            System.exit(1);
        }
        mode = res.get("mode");
        season = res.get("season");
        league = res.get("league");
        configFile = res.get("configFile");
    }
}