/*
 * $URL$
 * $Revision$
 * $LastChangedBy$
 * $LastChangedDate$
 *
 * Copyright (c) 2005 syseca AG, Switzerland
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of syseca AG. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you
 * entered into with syseca AG.
 */
package ch.spherIC.settings;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;
import ch.spherIC.EasyBracketMain;
import ch.spherIC.R;


/**
 * Settings / Preferences Dialog
 */
public class SettingsDlg extends Dialog {

    public static final String PREFS_NAME = "SettingsFile";
    private Context prnt;

    /**
     * Creates a new SettingsDlg object.
     */
    public SettingsDlg(Context context) {
        super(context, R.style.GlassThemeDialog);
        this.prnt = context;
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings);
        setTitle(this.prnt.getResources().getText(R.string.menuSettings));

        ((Button) findViewById(R.id.btn_Stg_Save)).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    saveSettings();
                    SettingsDlg.this.dismiss();
                    ((EasyBracketMain) SettingsDlg.this.prnt).loadSettings();
                    ((EasyBracketMain) SettingsDlg.this.prnt).setDefaults();
                }
            });

        ((Button) findViewById(R.id.btn_Stg_Cancel)).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    SettingsDlg.this.dismiss();
                }
            });

    }


    /**
     * {@inheritDoc}
     */
    @Override
    protected void onStart() {
        super.onStart();
        applySettings();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * Reads and applies the settings.
     */
    private void applySettings() {

        boolean fixBP;
        float bpCorrection;
        float dpCorrection;

        // Restore settings
        SharedPreferences settings = getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

        fixBP = settings.getBoolean("fixBrightPoint", true);
        ((RadioButton) findViewById(R.id.rdbtn_KeepBP)).setChecked(fixBP);
        ((RadioButton) findViewById(R.id.rdbtn_KeepDP)).setChecked(!fixBP);

        bpCorrection = settings.getFloat("brightPointCorrection", 0f);
        dpCorrection = settings.getFloat("darkPointCorrection", 0f);

        ((Spinner) findViewById(R.id.spn_BP_Corr)).setSelection((int) bpCorrection);
        ((Spinner) findViewById(R.id.spn_DP_Corr)).setSelection((int) dpCorrection);
    }

    /**
     * Saves the settings
     */
    private void saveSettings() {

        // Save settings
        SharedPreferences settings = getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();

        editor.putBoolean("fixBrightPoint", ((RadioButton) findViewById(R.id.rdbtn_KeepBP)).isChecked());
        editor.putFloat("brightPointCorrection", ((Spinner) findViewById(R.id.spn_BP_Corr)).getSelectedItemPosition());
        editor.putFloat("darkPointCorrection", ((Spinner) findViewById(R.id.spn_DP_Corr)).getSelectedItemPosition());

        // Commit the edits!
        editor.commit();
    }

}
