package godau.fynn.dsbdirect.table.reader;

import android.content.Context;
import androidx.annotation.Nullable;
import godau.fynn.dsbdirect.Utility;

public abstract class ReaderFactory {

    // List of parser names

    public static final String NULL = "automatic";
    public static final String UNTIS = "untis";
    public static final String UNTIS4 = "untis4"; // technically a SUPER SECRET SETTING
    public static final String ROENTGEN = "roentgen";
    public static final String GOETHE = "goethe";
    public static final String WHITE_YELLOW = "whiteyellow";
    public static final String DUMMY = "dummy"; // SUPER SECRET SETTING for debugging

    /**
     * Get parser for this html by id and user preference.
     * @param html The html the parser is for
     * @param id Auth id
     * @return A parser set by the user or figured out using id in {@link String getParserIdentifier(id)} or
     * identified through checking the html file for ads.
     */
    public static @Nullable
    Reader getReader(String html, String id, Context context) {

        // User preference overwrite hardcoded preference overwrite dynamic decision
        switch (getParserIdentifier(id, html, context)) {
            case UNTIS:
                return new Untis(html, context);
            case UNTIS4:
                return new Untis4(html, context);
            case ROENTGEN:
                return new Roentgen(html, context);
            case GOETHE:
                return new GoetheHamburg(html, context);
            case WHITE_YELLOW:
                return new WhiteYellow(html, context);
            case DUMMY:
                return new Dummy(context);
            case NULL:
            default:
                return null;
        }
    }

    /**
     * Get user preference or, if user has no preference, decide based on hardcoded ids or autodetect ads.
     * @param id id to base decision on if user has not configured a preference
     * @return The string name of the parser
     */
    private static String getParserIdentifier(String id, String html, Context context) {

        // Get user setting

        String userSetting = getParserUserSetting(context);
        // NULL means that the preference is "Automatic"
        if (!userSetting.equals(NULL)) {
            return userSetting;
            /* Note that this does not verify the user's preference; if an invalid preference has been
             * configured, it won't be overwritten by the hardcoded id preference or the dynamic decision.
             */
        }

        // Hardcoded preference

        switch (id) {
            case "162161": // Alexander-von-Humboldt-Gymnasium Schweinfurt
                return UNTIS;
            case "219261": // Röntgen-Gymnasium Würzburg
                return ROENTGEN;
            case "189802": // Goethe-Gymnasium Hamburg
                return GOETHE;
            case "177841":
                return WHITE_YELLOW;
        }

        // Dynamic decision: try to recognize substitution plan by an ad it contains

        if (html.contains("Untis Stundenplan Software")) {
            return UNTIS;
        } else if (html.contains("HeinekingMedia Vertretungsplan")) {
            return GOETHE;
        }

        // Nothing found? Then we can't parse this, sorry.
        return NULL;
    }

    /**
     * @return The parser option configured in user's shared preferences
     */
    private static String getParserUserSetting(Context context) {
        return new Utility(context).getSharedPreferences().getString("parser", NULL);
    }
}
