package org.durka.hallmonitor;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

/**
 * Created by ladmin on 07.01.14.
 */
public class PreferenceSwitchable extends Preference {
    private final String LOG_TAG = "PreferenceSwitchable";

    private TextView mTitle;
    private TextView mSummary;
    private ImageView mIcon;
    private Switch mSwitch;
    private boolean mSwitchState = false;

    public PreferenceSwitchable(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        //Log.d(LOG_TAG, "onCreateView: " + getKey());

        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.preference_switchable_layout, parent, false);

        mTitle = (TextView)view.findViewById(android.R.id.title);
        mSummary = (TextView)view.findViewById(android.R.id.summary);
        mIcon = (ImageView)view.findViewById(android.R.id.icon);
        mSwitch = (Switch)view.findViewById(R.id.prefEnableSwitch);

        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                setChecked(b);
                persistBoolean(b);
            }
        });

        setChecked(mSwitchState);

        return view;
    }

    @Override
    public void setTitle(int titleResId) {
        mTitle.setText(getContext().getResources().getString(titleResId));
    }

    @Override
    public void setTitle(CharSequence title) {
        mSummary.setText(title);
    }

    @Override
    public void setSummary(int summaryResId) {
        mSummary.setText(getContext().getResources().getString(summaryResId));
    }

    @Override
    public void setSummary(CharSequence summary) {
        mSummary.setText(summary);
    }

    @Override
    public void setIcon(int iconResId) {
        mIcon.setImageDrawable(getContext().getResources().getDrawable(iconResId));
    }

    @Override
    public void setIcon(Drawable icon) {
        mIcon.setImageDrawable(icon);
    }

    @Override
    public void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        //Log.d(LOG_TAG, "onSetInitialValue: " + restorePersistedValue + ", " + defaultValue);
        boolean value = false;

        if (!restorePersistedValue && (defaultValue instanceof Boolean)) {
            value = ((Boolean)defaultValue).booleanValue();
        }

        if (restorePersistedValue)
            value = getSharedPreferences().getBoolean(getKey(), false);

        mSwitchState = value;
        //Log.d(LOG_TAG, "onSetInitialValue: value = " + value);
    }

    @Override
    protected Object onGetDefaultValue (TypedArray a, int index) {
        //Log.d(LOG_TAG, "onGetDefaultValue: " + index + ", " + a);
        return a.getBoolean(index, false);
    }

    protected void setChecked(boolean checked) {
        //Log.d(LOG_TAG, "setChecked: " + checked);
        mSwitchState = checked;
        setSelectable(mSwitchState);
        mSwitch.setChecked(mSwitchState);
    }

}

@@ -1769,16 +1769,23 @@
 hecked(b
+, false
 );%0A     
@@ -1872,16 +1872,23 @@
 tchState
+, false
 );%0A%0A    
@@ -3479,24 +3479,118 @@
 n checked) %7B
+ setChecked(checked, true); %7D%0A    protected void setChecked(boolean checked, boolean notify) %7B
 %0A        //L
@@ -3603,17 +3603,31 @@
 OG_TAG, 
-%22
+getTitle() + %22 
 setCheck
@@ -3644,32 +3644,83 @@
 ecked);%0A        
+//new Exception().printStackTrace();%0A        %0A    %09
 mSwitchState = c
@@ -3764,32 +3764,62 @@
 State);%0A        
+%0A        if (mSwitch != null) 
 mSwitch.setCheck
@@ -3832,21 +3832,486 @@
 itchState);%0A
+        %09%0A        if (notify) notifyDependencyChange(shouldDisableDependents());%0A    %7D%0A    %0A    @Override%0A    public boolean shouldDisableDependents() %7B%0A    %09return !mSwitchState;%0A    %7D%0A    %0A    @Override%0A    public void onDependencyChanged(Preference dependency, boolean disableDependent) %7B%0A    %09Log.d(LOG_TAG, getTitle() + %22 oDP from %22 + dependency.getTitle() + %22, dD=%22 + disableDependent);%0A    %09setChecked(!disableDependent);%0A    %09setEnabled(!disableDependent);%0A
     %7D%0A%0A%7D%0A

implement dependencies in PreferenceSwitchable