package com.kozen.printerhelper.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;

import com.kozen.printerhelper.R;

public class ActivitySettings extends PreferenceActivity implements OnPreferenceChangeListener {
    public static final String SETTING_DEMO_DATA        = "demo_data";
    public static final String PREF_CUTPAPER_KEY        = "cut_paper";

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        SwitchPreference switchPref = (SwitchPreference)findPreference(PREF_CUTPAPER_KEY);
        switchPref.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        final String key = preference.getKey();
        SharedPreferences.Editor editor = getSharedPreferences(SETTING_DEMO_DATA, MODE_PRIVATE).edit();
        if(PREF_CUTPAPER_KEY.equals(key)){
            editor.putBoolean(PREF_CUTPAPER_KEY, Boolean.valueOf(newValue.toString()));
            editor.commit();
            return true;
        } else {
            editor.commit();
            return false;
        }
    }

    public static boolean doCheckIsCutPaperSupport(Context context){
        return context.getSharedPreferences(SETTING_DEMO_DATA, Context.MODE_PRIVATE)
            .getBoolean(PREF_CUTPAPER_KEY, context.getResources().getBoolean(R.bool.config_cutpaper_support));
    }
}
