package au.com.pactera.pacterapulse.fragment;


import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Toast;

import au.com.pactera.pacterapulse.R;
import au.com.pactera.pacterapulse.chart.EmotionBarChartView;
import au.com.pactera.pacterapulse.core.BaseFragment;
import au.com.pactera.pacterapulse.helper.NetworkHelper;
import au.com.pactera.pacterapulse.model.Emotions;
import butterknife.InjectView;
import butterknife.OnCheckedChanged;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;


public class ResultFragment extends BaseFragment<Emotions> {

    private static final String TYPE = "_TYPE";
    @InjectView(R.id.progressBar)
    ProgressBar progressBar;
    @InjectView(R.id.chartView)
    EmotionBarChartView chartView;

    private String currentType = "24hours";


    @OnCheckedChanged({R.id.oneday, R.id.onemonth, R.id.oneweek})
    void onActionsChecked(RadioButton view) {
        if (!view.getTag().equals(currentType)) {
            currentType = (String) view.getTag();
            Bundle b = new Bundle();
            b.putString(TYPE, currentType);
            refresh(b);
        }
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_result;
    }


    @Override
    protected void setupUI(View view, Bundle bundle) throws Exception {
        if (!getArguments().getBoolean(EmotionFragment.SUCCESS, true)) {
            Crouton.makeText(getActivity(), R.string.voted_today, Style.ALERT).show();
        }
    }

    @Override
    public Emotions pendingData(Bundle arg) throws Exception {
        String type = arg.getString(TYPE, currentType);
        return NetworkHelper.getResult(type);
    }

    @Override
    protected int getLoaderOn() {
        return ONCREATE;
    }

    @Override
    protected void onStartLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onStopLoading() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void onLoaderDone(Emotions items) {
        setEmotionResult(items.getHappy(), items.getSoso(), items.getSad());
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(context, R.string.invalidNetwork, Toast.LENGTH_SHORT).show();
    }

    private void setEmotionResult(int happy, int neutral, int sad) {
        if (null != chartView) {
            chartView.setDataSet(happy, neutral, sad);
            chartView.invalidate();
        }
    }

}

@@ -87,20 +87,87 @@
 id.view.
-View
+LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup
 ;%0Aimport
@@ -226,24 +226,58 @@
 adioButton;%0A
+import android.widget.RadioGroup;%0A
 import andro
@@ -294,16 +294,40 @@
 Toast;%0A%0A
+import java.util.List;%0A%0A
 import a
@@ -599,26 +599,90 @@
 erknife.
-InjectView
+ButterKnife;%0Aimport butterknife.InjectView;%0Aimport butterknife.InjectViews
 ;%0Aimport
@@ -1067,16 +1067,99 @@
 tView;%0A%0A
+    @InjectViews(%7BR.id.oneweek,R.id.oneday,R.id.onemonth%7D)%0A    List%3CView%3E radios;%0A%0A
     priv
@@ -2259,105 +2259,522 @@
     
-%7D%0A%0A    @Override%0A    protected void onStopLoading() %7B%0A        progressBar.setVisibility(View.GONE
+    ButterKnife.apply(radios, new ButterKnife.Action%3CView%3E() %7B%0A            @Override%0A            public void apply(View view, int index) %7B%0A                view.setEnabled(false);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    @Override%0A    protected void onStopLoading() %7B%0A        progressBar.setVisibility(View.GONE);%0A        ButterKnife.apply(radios, new ButterKnife.Action%3CView%3E() %7B%0A            @Override%0A            public void apply(View view, int index) %7B%0A                view.setEnabled(true);%0A            %7D%0A        %7D
 );%0A 

disable action buttons while loader is working