package com.viaccessorca.voplayerdemo.activities;

import com.viaccessorca.voplayerdemo.Constants;
import com.viaccessorca.voplayerdemo.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;


public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    private static final String TAG = "SettingsActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        // get shared preferences and add a listener to monitor content host preference (for analytics)
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        updatePrefsDisplay(prefs);
    }

    private void updatePrefsDisplay(SharedPreferences prefs) {
        boolean enabled = false;

        if(prefs.getString(Constants.PREF_PROXY_TYPE, "No proxy").equals("No proxy")) {
            findPreference(Constants.PREF_PROXY_PORT).setEnabled(false);
            findPreference(Constants.PREF_PROXY_URL).setEnabled(false);
        } else {
            findPreference(Constants.PREF_PROXY_PORT).setEnabled(true);
            findPreference(Constants.PREF_PROXY_URL).setEnabled(true);
        }
        Preference p;
        p = findPreference(Constants.PREF_PROXY_TYPE);
        p.setSummary(prefs.getString(Constants.PREF_PROXY_TYPE, "No Proxy"));
        p = findPreference(Constants.PREF_PROXY_PORT);
        p.setSummary(prefs.getString(Constants.PREF_PROXY_PORT, ""));
        p = findPreference(Constants.PREF_PROXY_URL);
        p.setSummary(prefs.getString(Constants.PREF_PROXY_URL, ""));

        p = findPreference(Constants.PREF_DL_START_DELAY);
        p.setSummary(prefs.getString(Constants.PREF_DL_START_DELAY, "30 s"));

        p = findPreference(Constants.PREF_ENABLE_SCRUBBING);
        enabled = prefs.getBoolean(Constants.PREF_ENABLE_SCRUBBING, true);
        p.setSummary(enabled ? "Enabled" : "Disabled");

        p = findPreference(Constants.PREF_DISABLE_MAX_PLAYABLE_HEIGHT);
        enabled = prefs.getBoolean(Constants.PREF_DISABLE_MAX_PLAYABLE_HEIGHT, false);
        p.setSummary(enabled ? "Enabled" : "Disabled");

        p = findPreference(Constants.PREF_ENABLE_ABR_SENSITIVITY);
        p.setSummary(prefs.getString(Constants.PREF_ENABLE_ABR_SENSITIVITY, "Default"));
        p = findPreference(Constants.PREF_CODEC_TYPE);
        p.setSummary(prefs.getString(Constants.PREF_CODEC_TYPE, "DEFAULT"));

        p = findPreference(Constants.PREF_ENABLE_PLATFORM_AUDIO_DECODING);
        enabled = prefs.getBoolean(Constants.PREF_ENABLE_PLATFORM_AUDIO_DECODING, false);
        p.setSummary(enabled ? "Enabled" : "Disabled");

        p = findPreference(Constants.PREF_ENABLE_INT_SUBTITLE_RENDERING);
        enabled = prefs.getBoolean(Constants.PREF_ENABLE_INT_SUBTITLE_RENDERING, true);
        p.setSummary(enabled ? "Enabled" : "Disabled");

        p = findPreference(Constants.PREF_TEXT_SUBTITLE_COLOR);
        p.setSummary(prefs.getString(Constants.PREF_TEXT_SUBTITLE_COLOR, "Default"));

        p = findPreference(Constants.PREF_SET_AD_SEEK_TO_ALLOWED);
        enabled = prefs.getBoolean(Constants.PREF_SET_AD_SEEK_TO_ALLOWED, false);
        p.setSummary(enabled ? "Allowed" : "Not allowed");

        p = findPreference(Constants.PREF_FLAT_360_ZOOM);
        String zoomValue = prefs.getString(Constants.PREF_FLAT_360_ZOOM, Constants.FLAT_360_ZOOM_DEFAULT);
        p.setSummary(zoomValue);

        p = findPreference(Constants.PREF_FLAT_360_RATIO);
        String ratioValue = prefs.getString(Constants.PREF_FLAT_360_RATIO, Constants.FLAT_360_RATIO_DEFAULT);
        p.setSummary(ratioValue);

        p = findPreference(Constants.PREF_ENABLE_ABR_GRAPH);
        enabled = prefs.getBoolean(Constants.PREF_ENABLE_ABR_GRAPH, false);
        p.setSummary(enabled ? "Enabled" : "Disabled");

        p = findPreference(Constants.PREF_IP_RESOLVE_TYPE);
        p.setSummary(prefs.getString(Constants.PREF_IP_RESOLVE_TYPE, "Default"));

        p = findPreference(Constants.PREF_ENABLE_TEMPORARY_REDIRECT_AS_PERMANENT);
        enabled = prefs.getBoolean(Constants.PREF_ENABLE_TEMPORARY_REDIRECT_AS_PERMANENT, false);
        p.setSummary(enabled ? "Enabled" : "Disabled");
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Log.v(TAG, "shared preference changed: " + key);

        Preference p;
        boolean enabled = false;

        if(key.equalsIgnoreCase(Constants.PREF_PROXY_TYPE)) {

            if(sharedPreferences.getString(key, "No proxy").equals("No proxy")) {
                findPreference(Constants.PREF_PROXY_PORT).setEnabled(false);
                findPreference(Constants.PREF_PROXY_URL).setEnabled(false);
            } else {
                findPreference(Constants.PREF_PROXY_PORT).setEnabled(true);
                findPreference(Constants.PREF_PROXY_URL).setEnabled(true);
            }
            p = findPreference(Constants.PREF_PROXY_TYPE);
            p.setSummary(sharedPreferences.getString(Constants.PREF_PROXY_TYPE, "No proxy"));
        } else if(key.equalsIgnoreCase(Constants.PREF_PROXY_URL)) {
            p = findPreference(Constants.PREF_PROXY_URL);
            p.setSummary(sharedPreferences.getString(Constants.PREF_PROXY_URL, ""));
        } else if (key.equalsIgnoreCase(Constants.PREF_PROXY_PORT)) {
            p = findPreference(Constants.PREF_PROXY_PORT);
            p.setSummary(sharedPreferences.getString(Constants.PREF_PROXY_PORT, ""));
        } else if (key.equalsIgnoreCase(Constants.PREF_TYPICAL_BITRATE)) {
            p = findPreference(Constants.PREF_TYPICAL_BITRATE);
            p.setSummary(sharedPreferences.getString(Constants.PREF_TYPICAL_BITRATE, ""));
        } else if (key.equalsIgnoreCase(Constants.PREF_SURFACE_SECURE)) {
            p = findPreference(Constants.PREF_SURFACE_SECURE);
            enabled = sharedPreferences.getBoolean(Constants.PREF_SURFACE_SECURE, true);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_DL_START_DELAY)) {
            p = findPreference(Constants.PREF_DL_START_DELAY);
            p.setSummary(sharedPreferences.getString(Constants.PREF_DL_START_DELAY, "30 s"));
        } else if(key.equalsIgnoreCase(Constants.PREF_FORCE_EVENT_PLAYLIST)) {
            p = findPreference(Constants.PREF_FORCE_EVENT_PLAYLIST);
            enabled = sharedPreferences.getBoolean(Constants.PREF_FORCE_EVENT_PLAYLIST, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_ENABLE_LIVE_SEEKING)) {
            p = findPreference(Constants.PREF_ENABLE_LIVE_SEEKING);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_LIVE_SEEKING, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_ENABLE_SCRUBBING)) {
            p = findPreference(Constants.PREF_ENABLE_SCRUBBING);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_SCRUBBING, true);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_DISABLE_MAX_PLAYABLE_HEIGHT)) {
            p = findPreference(Constants.PREF_DISABLE_MAX_PLAYABLE_HEIGHT);
            enabled = sharedPreferences.getBoolean(Constants.PREF_DISABLE_MAX_PLAYABLE_HEIGHT, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_ENABLE_ABR_SENSITIVITY)) {
            p = findPreference(Constants.PREF_ENABLE_ABR_SENSITIVITY);
            p.setSummary(sharedPreferences.getString(Constants.PREF_ENABLE_ABR_SENSITIVITY, "Default"));
        } else if(key.equalsIgnoreCase(Constants.PREF_CODEC_TYPE)) {
            p = findPreference(Constants.PREF_CODEC_TYPE);
            p.setSummary(sharedPreferences.getString(Constants.PREF_CODEC_TYPE, "DEFAULT"));
        } else if (key.equalsIgnoreCase(Constants.PREF_ENABLE_PLATFORM_AUDIO_DECODING)) {
            p = findPreference(Constants.PREF_ENABLE_PLATFORM_AUDIO_DECODING);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_PLATFORM_AUDIO_DECODING, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_ENABLE_INT_SUBTITLE_RENDERING)) {
            p = findPreference(Constants.PREF_ENABLE_INT_SUBTITLE_RENDERING);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_INT_SUBTITLE_RENDERING, true);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_TEXT_SUBTITLE_COLOR)) {
            p = findPreference(Constants.PREF_TEXT_SUBTITLE_COLOR);
            p.setSummary(sharedPreferences.getString(Constants.PREF_TEXT_SUBTITLE_COLOR, "Default"));
        } else if (key.equalsIgnoreCase(Constants.PREF_SET_AD_SEEK_TO_ALLOWED)) {
            p = findPreference(Constants.PREF_SET_AD_SEEK_TO_ALLOWED);
            enabled = sharedPreferences.getBoolean(Constants.PREF_SET_AD_SEEK_TO_ALLOWED, false);
            p.setSummary(enabled ? "Allowed" : "Not allowed");
        } else if (key.equalsIgnoreCase(Constants.PREF_FLAT_360_ZOOM)) {
            p = findPreference(Constants.PREF_FLAT_360_ZOOM);
            p.setSummary(sharedPreferences.getString(Constants.PREF_FLAT_360_ZOOM, Constants.FLAT_360_ZOOM_DEFAULT));
        } else if (key.equalsIgnoreCase(Constants.PREF_FLAT_360_RATIO)) {
            p = findPreference(Constants.PREF_FLAT_360_RATIO);
            p.setSummary(sharedPreferences.getString(Constants.PREF_FLAT_360_RATIO, Constants.FLAT_360_RATIO_DEFAULT));
        } else if (key.equalsIgnoreCase(Constants.PREF_ENABLE_ABR_GRAPH)) {
            p = findPreference(Constants.PREF_ENABLE_ABR_GRAPH);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_ABR_GRAPH, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        } else if(key.equalsIgnoreCase(Constants.PREF_IP_RESOLVE_TYPE)) {
            p = findPreference(Constants.PREF_IP_RESOLVE_TYPE);
            p.setSummary(sharedPreferences.getString(Constants.PREF_IP_RESOLVE_TYPE, "Default"));
        } else if(key.equalsIgnoreCase(Constants.PREF_ENABLE_TEMPORARY_REDIRECT_AS_PERMANENT)) {
            p = findPreference(Constants.PREF_ENABLE_TEMPORARY_REDIRECT_AS_PERMANENT);
            enabled = sharedPreferences.getBoolean(Constants.PREF_ENABLE_TEMPORARY_REDIRECT_AS_PERMANENT, false);
            p.setSummary(enabled ? "Enabled" : "Disabled");
        }
    }
}
