/**
 * @author Fabricio Pontes Harsich fabriph@gmail.com
 */
package com.fabricioph.piteacher;

import android.app.ActivityOptions;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

import com.fabricioph.piteacher.adapters.DBAdapter;

public class HomeSettingsFragment extends Fragment {


    public static final String USE_DIALOGS = "use_dialogs";
    public static final boolean USE_DIALOGS_DEFAULT_VALUE = true;
    private SharedPreferences.Editor editor;

    public HomeSettingsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_settings,
                container, false);
        getActivity().setTitle(getString(R.string.settings));

        SharedPreferences sharedPref = getActivity().getSharedPreferences(
                getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        useWifiDirect(rootView, sharedPref);
        useDialogs(rootView, sharedPref);
        editInfo(rootView, sharedPref);

        return rootView;
    }

    private void useWifiDirect(View rootView, SharedPreferences sharedPref) {
        Switch useWifiDirect = (Switch) rootView
                .findViewById(R.id.settings_wifi_direct);
        useWifiDirect.setChecked(sharedPref.getBoolean(DBAdapter.USE_WIFI_DIRECT,
                DBAdapter.USE_WIFI_DIRECT_DEFAULT_VALUE));
        useWifiDirect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                HomeSettingsFragment.this.editor.putBoolean(DBAdapter.USE_WIFI_DIRECT,
                        isChecked);
                HomeSettingsFragment.this.editor.commit();
            }
        });
    }

    private void useDialogs(View rootView, SharedPreferences sharedPref) {
        Switch useDialogs = (Switch) rootView
                .findViewById(R.id.settings_rh_dialog);
        useDialogs.setChecked(sharedPref.getBoolean(USE_DIALOGS,
                USE_DIALOGS_DEFAULT_VALUE));
        useDialogs.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                HomeSettingsFragment.this.editor.putBoolean(USE_DIALOGS,
                        isChecked);
                HomeSettingsFragment.this.editor.commit();
            }
        });
    }

    private void editInfo(View rootView, SharedPreferences sharedPref) {
        Button editInfo = (Button) rootView
                .findViewById(R.id.settings_edit_info_button);
        editInfo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(
                        new Intent(getActivity(), com.fabricioph.piteacher.SetupActivity.class),
                        ActivityOptions.makeCustomAnimation(
                                getActivity().getApplicationContext(),
                                R.anim.enter_1, R.anim.enter_2).toBundle());
            }
        });
    }
}
