package org.msf.records.ui.tentselection;

import java.util.List;

import org.msf.records.R;
import org.msf.records.location.LocationTree.LocationSubtree;
import org.msf.records.ui.PatientListActivity;
import org.msf.records.ui.ProgressFragment;
import org.msf.records.utils.PatientCountDisplay;
import org.msf.records.widget.SubtitledButtonView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Displays a list of all tents.
 */
public final class TentSelectionFragment extends ProgressFragment {
    @InjectView(R.id.tent_selection_tents) GridView mTentGrid;
    @InjectView(R.id.tent_selection_all_patients) SubtitledButtonView mAllPatientsButton;
    @InjectView(R.id.tent_selection_triage) SubtitledButtonView mTriageButton;
    @InjectView(R.id.tent_selection_discharged) SubtitledButtonView mDischargedButton;

    private TentSelectionController mController;
    private MyUi mMyUi = new MyUi();
    private TentListAdapter mAdapter;

    public TentSelectionFragment() {
        // Required empty public constructor
    }

    @OnItemClick(R.id.tent_selection_tents) void onTentGridClicked(int position) {
    	mController.onTentSelected(mAdapter.getItem(position));
    }

    @OnClick(R.id.tent_selection_all_patients) void onAllPatientsClicked(View v) {
    	Intent listIntent = new Intent(getActivity(), PatientListActivity.class);
        startActivity(listIntent);
    }

    @OnClick(R.id.tent_selection_discharged) void onDischargedClicked(View v) {
    	mController.onDischargedPressed();
    }

    @OnClick(R.id.tent_selection_triage) void onTriageClicked(View v) {
    	mController.onTriagePressed();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tent_selection);
        mController = ((TentSelectionActivity) getActivity()).getController();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mController.attachFragmentUi(mMyUi);
    }

    @Override
    public void onPause() {
    	mController.detachFragmentUi(mMyUi);
        super.onPause();
    }

    private final class MyUi implements TentSelectionController.TentFragmentUi {
    	@Override
    	public void setDischargedPatientCount(int patientCount) {
            mDischargedButton.setSubtitle(
                    PatientCountDisplay.getPatientCountSubtitle(
                            getActivity(), patientCount));
    	}

    	@Override
    	public void setTriagePatientCount(int patientCount) {
            mTriageButton.setSubtitle(
                    PatientCountDisplay.getPatientCountSubtitle(
                            getActivity(), patientCount));
    	}

    	@Override
    	public void setPatientCount(int patientCount) {
    		mAllPatientsButton.setSubtitle(
                    PatientCountDisplay.getPatientCountSubtitle(
                            getActivity(), patientCount, true));
    	}

    	@Override
    	public void setTents(List<LocationSubtree> tents) {
    		mAdapter = new TentListAdapter(getActivity(), tents);
    		mTentGrid.setAdapter(mAdapter);
		}

    	@Override
    	public void showSpinner(boolean show) {
    		changeState(show ? State.LOADING : State.LOADED);
    	}
    }
}

@@ -341,16 +341,45 @@
 nView;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -548,24 +548,25 @@
 t.GridView;%0A
+%0A
 import butte
@@ -2091,87 +2091,8 @@
 n);%0A
-        mController = ((TentSelectionActivity) getActivity()).getController();%0A
     
@@ -2426,23 +2426,59 @@
 d on
-Resume(
+ViewStateRestored(Bundle savedInstanceState
 ) %7B%0A    
     
@@ -2473,28 +2473,25 @@
 ) %7B%0A    
-    
+%09
 super.on
 Resume()
@@ -2486,26 +2486,131 @@
 r.on
-Resume();%0A        
+ViewStateRestored(savedInstanceState);%0A        mController = ((TentSelectionActivity) getActivity()).getController();%0A    %09
 mCon
@@ -2641,36 +2641,33 @@
 (mMyUi);%0A    %7D%0A%0A
-    
+%09
 @Override%0A    pu
@@ -2652,36 +2652,33 @@
   %7D%0A%0A%09@Override%0A
-    
+%09
 public void onPa
@@ -2679,22 +2679,25 @@
 d on
-Pause
+DestroyView
 () %7B%0A
-    
+%09
 %09mCo
@@ -2734,37 +2734,34 @@
 i);%0A
-        super.onPause();%0A    
+%09%09super.onDestroyView();%0A%09
 %7D%0A%0A 

Fix crash in TentSelectionFragment if activity was destroyed.