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

import com.liskovsoft.sharedutils.helpers.Helpers;

public class SubtitleTrack extends MediaTrack {
    public SubtitleTrack(int rendererIndex) {
        super(rendererIndex);
    }

    @Override
    public int inBounds(MediaTrack track2) {
        return compare(track2);
    }

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

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

        int result = -1;

        // Subs id is not constant. Don't rely on it.
        //if (Helpers.equals(format.id, track2.format.id)) {
        //    result = 0;
        //}

        if (Helpers.equals(track2.format.language, format.language)) { // exact match
            result = 0;
        } else if (Helpers.startsWith(track2.format.language, trim(format.language))) { // partial match
            // Use autogenerated subs only if ones was selected before
            if (isAutogen(track2.format.language) && !isAutogen(format.language)) {
                result = -1;
            } else {
                result = 1;
            }
        }

        return result;
    }

    /**
     * Contains autogenerated subs?
     */
    private static boolean isAutogen(String language) {
        if (language == null) {
            return false;
        }

        return language.contains("(") && language.contains(")");
    }

    ///**
    // * Remove autogenerated and other stuff
    // */
    //private static String trim(String language) {
    //    if (language == null) {
    //        return null;
    //    }
    //
    //    return language.split(" ")[0];
    //}

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

        return language
                .replaceAll(" \\(.*", "") // english (us) bla -> english
                .replaceAll(" - .*", ""); // english - us bla -> english
    }
}
