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.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;


public class LogLevelsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    private static final String TAG = "LogLevelsActivity";

    SharedPreferences prefs;

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

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

        Preference reset_btn = findPreference(getString(R.string.loglevels_reset_key));
        reset_btn.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                    ListPreference lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_AUDIO_DECODER);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_AUDIO_PRESENTER);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_AUDIO_SERVICE);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_DEPACK);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_DRM);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_DRM_AGENT);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_HTTP_STACK);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_LVMC_AUDIO);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_LVMC_JNI);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_LVMC_VIDEO);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_LVVTB_VIDEO);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_M4TOOL_URI);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_MSH);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_NET_ENG);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_NET_ENG_AUCB);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_OFFLINE_VIEWING);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_PGDL_ENG);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_PLAYER_GENERIC);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_QP_PROXY);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_SSM);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_SUBTITLE_DECODER);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VIDEO_DECODER);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VIDEO_RENDERER);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VIDEO_SINK);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPS);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPS_API);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPS_SUB);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPSIL);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPSIL_CB);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPSIL_OTHERS);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VPSIL_UTILS);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VAST);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VMAP);
                    lp.setValue("None");
                    lp = (ListPreference)findPreference(Constants.PREF_LOGLEVEL_VMX);
                    lp.setValue("None");

                    return true;
                }
            });
    }

    private void updatePrefsDisplay(SharedPreferences prefs) {
        Preference p;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    }

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

        Preference p;

        p = findPreference(key);

        try {
            p.setSummary(sharedPreferences.getString(key, "Default"));
        } catch (ClassCastException e) {
            Log.e(TAG, "Preference " + key + " value is not an String");
        }
    }

}
