/*
 * Copyright 2014 Uwe Trottmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.battlelancer.seriesguide.settings;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import com.battlelancer.seriesguide.Constants;
import com.battlelancer.seriesguide.R;

/**
 * Settings related to appearance, display formats and sort orders.
 */
public class DisplaySettings {

    public static final String KEY_THEME = "com.battlelancer.seriesguide.theme";

    public static final String KEY_LANGUAGE = "language";

    public static final String KEY_NUMBERFORMAT = "numberformat";

    public static final String NUMBERFORMAT_DEFAULT = "default";

    public static final String NUMBERFORMAT_ENGLISHLOWER = "englishlower";

    public static final String KEY_NO_RELEASED_EPISODES = "onlyFutureEpisodes";

    public static final String KEY_NO_WATCHED_EPISODES
            = "com.battlelancer.seriesguide.activity.nowatched";

    public static final String KEY_SEASON_SORT_ORDER = "seasonSorting";

    public static final String KEY_EPISODE_SORT_ORDER = "episodeSorting";

    public static final String KEY_HIDE_SPECIALS = "onlySeasonEpisodes";

    public static final String KEY_SORT_IGNORE_ARTICLE
            = "com.battlelancer.seriesguide.sort.ignorearticle";

    /**
     * Returns true for xlarge, xlarge-land or sw720dp screens.
     */
    public static boolean isVeryLargeScreen(Context context) {
        return context.getResources().getBoolean(R.bool.isLargeTablet);
    }

    /**
     * Returns true for all screens with dpi higher than {@link DisplayMetrics#DENSITY_HIGH}.
     */
    public static boolean isVeryHighDensityScreen(Context context) {
        return context.getResources().getDisplayMetrics().densityDpi > DisplayMetrics.DENSITY_HIGH;
    }

    /**
     * Returns true for xlarge, xlarge-land or sw720dp screens with xhdpi or higher dpi screens.
     */
    public static boolean isVeryLargeAndHighResScreen(Context context) {
        return isVeryLargeScreen(context)
                && context.getResources().getDisplayMetrics().densityDpi
                > DisplayMetrics.DENSITY_HIGH;
    }

    public static String getThemeIndex(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
                getString(KEY_THEME, "0");
    }

    public static String getContentLanguage(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getString(KEY_LANGUAGE, "en");
    }

    public static String getNumberFormat(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getString(KEY_NUMBERFORMAT, NUMBERFORMAT_DEFAULT);
    }

    public static Constants.EpisodeSorting getEpisodeSortOrder(Context context) {
        String orderId = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(KEY_EPISODE_SORT_ORDER, Constants.EpisodeSorting.OLDEST_FIRST.value());
        return Constants.EpisodeSorting.fromValue(orderId);
    }

    public static Constants.SeasonSorting getSeasonSortOrder(Context context) {
        String orderId = PreferenceManager.getDefaultSharedPreferences(context)
                .getString(KEY_SEASON_SORT_ORDER, Constants.SeasonSorting.LATEST_FIRST.value());
        return Constants.SeasonSorting.fromValue(orderId);
    }

    public static boolean isNoReleasedEpisodes(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getBoolean(KEY_NO_RELEASED_EPISODES, false);
    }

    public static boolean isNoWatchedEpisodes(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getBoolean(KEY_NO_WATCHED_EPISODES, false);
    }

    /**
     * Whether to exclude special episodes wherever possible (except in the actual seasons and
     * episode lists of a show).
     */
    public static boolean isHidingSpecials(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
                KEY_HIDE_SPECIALS, false);
    }

    /**
     * Whether shows and movies sorted by title should ignore the leading article.
     */
    public static boolean isSortOrderIgnoringArticles(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getBoolean(KEY_SORT_IGNORE_ARTICLE, false);
    }
}

@@ -672,16 +672,58 @@
 ontext;%0A
+import android.content.res.Configuration;%0A
 import a
@@ -2139,80 +2139,33 @@
 rue 
-for all screens with dpi higher than %7B@link DisplayMetrics#DENSITY_HIGH%7D
+if this is a large screen
 .%0A  
@@ -2202,23 +2202,13 @@
 n is
-VeryHighDensity
+Large
 Scre
@@ -2236,32 +2236,33 @@
 %0A        return 
+(
 context.getResou
@@ -2275,65 +2275,133 @@
 .get
-DisplayMetrics().densityDpi %3E DisplayMetrics.DENSITY_HIGH
+Configuration().screenLayout%0A                & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE
 ;%0A  
@@ -2441,38 +2441,11 @@
 for 
-xlarge, xlarge-land or sw720dp
+all
  scr
@@ -2458,35 +2458,59 @@
 ith 
-xh
 dpi 
-or 
 higher 
-dpi screens
+than %7B@link DisplayMetrics#DENSITY_HIGH%7D
 .%0A  
@@ -2551,23 +2551,19 @@
 Very
-LargeAndHighRes
+HighDensity
 Scre
@@ -2602,54 +2602,8 @@
 turn
- isVeryLargeScreen(context)%0A                &&
  con
@@ -2652,32 +2652,16 @@
 nsityDpi
-%0A               
  %3E Displ

Drop DisplaySettings.isVeryLargeAndHighResScreen, add .isLargeScreen.