package net.teerapap.whatnext.view;

import android.app.Activity;
import android.widget.ToggleButton;

import net.teerapap.whatnext.R;
import net.teerapap.whatnext.model.WhenCondition;

/**
 * It wraps the behaviors of when condition views to be reused in many activities and fragments.
 * <p/>
 * Current views must be 4 ToggleButtons with these id.
 * * R.id.when_home_toggle
 * * R.id.when_free_toggle
 * * R.id.when_work_toggle
 * * R.id.when_shopping_toggle
 * <p/>
 * Created by teerapap on 10/29/13.
 */
public class WhenConditionViewGroup {

    private ToggleButton mWhenHomeToggleBtn;
    private ToggleButton mWhenFreeToggleBtn;
    private ToggleButton mWhenWorkToggleBtn;
    private ToggleButton mWhenShoppingToggleBtn;

    public WhenConditionViewGroup(Activity activity) {
        mWhenHomeToggleBtn = (ToggleButton) activity.findViewById(R.id.when_home_toggle);
        mWhenFreeToggleBtn = (ToggleButton) activity.findViewById(R.id.when_free_toggle);
        mWhenWorkToggleBtn = (ToggleButton) activity.findViewById(R.id.when_work_toggle);
        mWhenShoppingToggleBtn = (ToggleButton) activity.findViewById(R.id.when_shopping_toggle);
    }

    /**
     * Reset toggle buttons
     */
    public void resetToggleButtons() {
        mWhenHomeToggleBtn.setChecked(false);
        mWhenFreeToggleBtn.setChecked(false);
        mWhenWorkToggleBtn.setChecked(false);
        mWhenShoppingToggleBtn.setChecked(false);
    }

    /**
     * Get current when condition from the toggle buttons
     *
     * @return
     */
    public WhenCondition getCondition() {
        return new WhenCondition()
                .atHome(mWhenHomeToggleBtn.isChecked())
                .freeTime(mWhenFreeToggleBtn.isChecked())
                .atWork(mWhenWorkToggleBtn.isChecked())
                .shopping(mWhenShoppingToggleBtn.isChecked());
    }
}

@@ -59,16 +59,54 @@
 tivity;%0A
+import android.widget.CompoundButton;%0A
 import a
@@ -133,16 +133,16 @@
 Button;%0A
-
 %0Aimport 
@@ -581,16 +581,144 @@
 roup %7B%0A%0A
+    public static interface OnConditionChangeListener %7B%0A        void onConditionChanged(WhenCondition currentCondition);%0A    %7D%0A%0A
     priv
@@ -1537,32 +1537,32 @@
 Checked(false);%0A
-
         mWhenSho
@@ -1594,32 +1594,828 @@
 (false);%0A    %7D%0A%0A
+    /**%0A     * Register a callback to be invoked when the condition changed.%0A     * @param listener%0A     */%0A    public void onConditionChanged(final OnConditionChangeListener listener) %7B%0A        CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                listener.onConditionChanged(getCondition());%0A            %7D%0A        %7D;%0A%0A        mWhenHomeToggleBtn.setOnCheckedChangeListener(checkedListener);%0A        mWhenFreeToggleBtn.setOnCheckedChangeListener(checkedListener);%0A        mWhenWorkToggleBtn.setOnCheckedChangeListener(checkedListener);%0A        mWhenShoppingToggleBtn.setOnCheckedChangeListener(checkedListener);%0A    %7D%0A%0A
     /**%0A     * G

Add OnConditionChangeListener in WhenConditionViewGroup (WIP)