package com.liskovsoft.smartyoutubetv2.common.exoplayer.selector.track;

import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.youtubeapi.videoinfo.models.TranslatedCaptionTrack;

import java.util.regex.Pattern;

public class SubtitleTrack extends MediaTrack {
    private static final Pattern AUTO_PATTERN = Pattern.compile(" \\(.*\\)$"); // May have mismatches e.g. 'English (United Kingdom)'
    private static final Pattern TRIM_PATTERN1 = Pattern.compile(" \\(.*\\) - .*");
    private static final Pattern TRIM_PATTERN2 = Pattern.compile(" - .*");
    private static final Pattern MARKER_PATTERN = Pattern.compile(".$");

    public SubtitleTrack(int rendererIndex) {
        super(rendererIndex);
    }

    @Override
    public int inBounds(MediaTrack track2) {
        if (format == null) {
            return -1;
        }

        if (track2 == null || track2.format == null) {
            return 1;
        }

        int result = -1;

        if (Helpers.startsWith(track2.format.language, trim(format.language))) { // partial match
            if (!isAuto(track2.format.language)) {
                // Prefer original subs
                result = 0;
            } else {
                result = 1;
            }
        }

        return result;
    }

    @Override
    public int compare(MediaTrack track2) {
        if (format == null) {
            return -1;
        }

        if (track2 == null || track2.format == null) {
            return 1;
        }

        int result = -1;

        if (Helpers.startsWith(track2.format.language, trim(format.language))) { // partial match
            if (!isAuto(format.language)) {
                // Prefer original subs
                result = 1;
            } else if (isAuto(format.language) && isAuto(track2.format.language)) {
                result = 0;
            }
        }

        return result;
    }

    /**
     * Autogenerated subs by the user <br/>
     * NOTE: Has an exceptions, like English (United Kingdom)
     */
    private static boolean isAutoUser(String language) {
        if (language == null) {
            return false;
        }

        return Helpers.matchAll(language, AUTO_PATTERN);
    }

    /**
     * Remove autogenerated and other stuff
     */
    public static String trim(String language) {
        if (language == null) {
            return null;
        }

        return trimMarker(Helpers.replace(Helpers.replace(language, TRIM_PATTERN1, ""), TRIM_PATTERN2, "")); // english - us bla -> english
    }

    public static String trimIfAuto(String language) {
        return isAuto(language) ? trim(language) : language;
    }

    /**
     * NOTE: Breaks Portuguese (Portugal) but fixes other similar languages
     */
    private static String trimAuto(String language) {
        if (language == null) {
            return null;
        }

        return Helpers.replace(trimMarker(language), AUTO_PATTERN, ""); // english (us) bla -> english
    }

    /**
     * Removes auto translate marker
     */
    private static String trimMarker(String language) {
        if (language != null && language.endsWith(TranslatedCaptionTrack.TRANSLATE_MARKER)) {
            return Helpers.replace(language, MARKER_PATTERN, "");
        } else {
            return language;
        }
    }

    public static boolean isAuto(String language) {
        return hasMarker(language);
    }

    private static boolean hasMarker(String language) {
        return language != null && language.endsWith(TranslatedCaptionTrack.TRANSLATE_MARKER);
    }
}
