// Path: RapidFTR-Android/src/main/java/com/rapidftr/adapter/HighlightedFieldsViewAdapter.java
// public class HighlightedFieldsViewAdapter<T extends BaseModel> extends BaseModelViewAdapter<T> {
// 
//     protected Map<Integer, FormField> highlightedFields;
//     private FormService formService;
//     private Class<CollectionActivity> activityToLaunch;
//     private final List<FormField> titleFields;
// 
//     public HighlightedFieldsViewAdapter(Context context, List<T> baseModels, String formName, Class<CollectionActivity> activityToLaunch) {
//         super(context, R.layout.row_highlighted_fields, baseModels);
// 
//         formService = RapidFtrApplication.getApplicationInstance().getBean(FormService.class);
// 
//         List<FormField> fields = formService.getHighlightedFields(formName);
//         titleFields = formService.getTitleFields(formName);
// 
//         highlightedFields = new TreeMap<Integer, FormField>();
//         this.activityToLaunch = activityToLaunch;
// 
//         int counter = 0;
//         for (FormField formField : fields) {
//             int id = ++counter;
//             highlightedFields.put(id, formField);
//         }
//     }
// 
//     @Override
//     public View getView(int position, View convertView, ViewGroup viewGroup){
//         View view = convertView;
//         if (view == null) {
//             LayoutInflater vi = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
//             view = vi.inflate(textViewResourceId, null);
//         }
// 
//         final T baseModel = objects.get(position) ;
//         if (baseModel != null) {
//             TextView uniqueIdView = (TextView) view.findViewById(R.id.row_child_unique_id);
// 
//             HighlightedFieldViewGroup highlightedFieldViewGroup = (HighlightedFieldViewGroup) view.findViewById(R.id.child_field_group);
//             highlightedFieldViewGroup.prepare(baseModel, highlightedFields);
// 
//             ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
//             try {
//                 setFields(String.valueOf(buildTitle(baseModel)), uniqueIdView);
//                 assignThumbnail(baseModel, imageView);
// 
//                 view.setOnClickListener(createClickListener(baseModel, activityToLaunch));
//             } catch (JSONException e) {
//                 throw new RuntimeException(e);
//             }
//         }
//         return view;
//     }
// 
//     private String buildTitle(T baseModel) throws JSONException {
//         if(titleFields.size() == 0) {
//             return baseModel.getShortId();
//         }
//         StringBuilder titleBuilder = new StringBuilder();
//         for (int i = 0; i < titleFields.size(); i++) {
//             FormField titleField = titleFields.get(i);
//             titleBuilder.append(baseModel.optString(titleField.getId()));
//             if((i + 1) < titleFields.size()) {
//                 titleBuilder.append(" ");
//             }
//         }
//         return String.format("%s (%s)", titleBuilder.toString(), baseModel.getShortId());
//     }
// }
// 
// Path: RapidFTR-Android/src/main/java/com/rapidftr/adapter/pagination/ViewAllChildrenPaginatedScrollListener.java
// public class ViewAllChildrenPaginatedScrollListener implements AbsListView.OnScrollListener{
// 
//     public static final int DEFAULT_PAGE_SIZE = 30;
//     public static final int FIRST_PAGE = 30;
//     private ViewAllChildScroller scroller;
// 
//     public ViewAllChildrenPaginatedScrollListener(ChildRepository repository,
//                                                   HighlightedFieldsViewAdapter<Child> adapter) {
//         scroller = new ViewAllChildScroller(repository, adapter);
//     }
// 
//     @Override
//     public void onScroll(AbsListView absListView, int firstVisibleItem,
//                          int numberOfVisibleItems, int numberOfItemsInAdapter) {
//         scroller.updateRecordNumbers(firstVisibleItem, numberOfVisibleItems, numberOfItemsInAdapter);
// 
//         try {
//             scroller.loadRecordsForNextPage();
//         } catch (JSONException e) {
//             throw new RuntimeException(e);
//         }
//     }
// 
//     @Override
//     public void onScrollStateChanged(AbsListView absListView, int i) {}
// }


import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.rapidftr.R;
import com.rapidftr.RapidFtrApplication;
import com.rapidftr.adapter.HighlightedFieldsViewAdapter;
import com.rapidftr.adapter.pagination.ViewAllChildrenPaginatedScrollListener;
import com.rapidftr.model.Child;
import com.rapidftr.repository.ChildRepository;
import lombok.Cleanup;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_children);
        try {
            hideEnquiriesTabIfRapidReg();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        listView(getChildren());
    }

    private List<Child> getChildren() {
        List<Child> children = new ArrayList<Child>();
        @Cleanup ChildRepository childRepository = inject(ChildRepository.class);
        try {
            children = childRepository.getRecordsForFirstPage();
        } catch (JSONException e) {
            Log.e("ViewAllChildrenActivity", "Error while displaying children list");
            makeToast(R.string.fetch_child_error);
        }
        return children;
    }

    private void listView(List<Child> children) {
        HighlightedFieldsViewAdapter highlightedFieldsViewAdapter = new HighlightedFieldsViewAdapter(this, children, Child.CHILD_FORM_NAME, ViewChildActivity.class);
        ListView childListView = (ListView) findViewById(R.id.child_list);
        if (children.isEmpty()) {
            childListView.setEmptyView(findViewById(R.id.no_child_view));
        }
        childListView.setAdapter(highlightedFieldsViewAdapter);
// Path: RapidFTR-Android/src/main/java/com/rapidftr/adapter/HighlightedFieldsViewAdapter.java
// public class HighlightedFieldsViewAdapter<T extends BaseModel> extends BaseModelViewAdapter<T> {
// 
//     protected Map<Integer, FormField> highlightedFields;
//     private FormService formService;
//     private Class<CollectionActivity> activityToLaunch;
//     private final List<FormField> titleFields;
// 
//     public HighlightedFieldsViewAdapter(Context context, List<T> baseModels, String formName, Class<CollectionActivity> activityToLaunch) {
//         super(context, R.layout.row_highlighted_fields, baseModels);
// 
//         formService = RapidFtrApplication.getApplicationInstance().getBean(FormService.class);
// 
//         List<FormField> fields = formService.getHighlightedFields(formName);
//         titleFields = formService.getTitleFields(formName);
// 
//         highlightedFields = new TreeMap<Integer, FormField>();
//         this.activityToLaunch = activityToLaunch;
// 
//         int counter = 0;
//         for (FormField formField : fields) {
//             int id = ++counter;
//             highlightedFields.put(id, formField);
//         }
//     }
// 
//     @Override
//     public View getView(int position, View convertView, ViewGroup viewGroup){
//         View view = convertView;
//         if (view == null) {
//             LayoutInflater vi = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
//             view = vi.inflate(textViewResourceId, null);
//         }
// 
//         final T baseModel = objects.get(position) ;
//         if (baseModel != null) {
//             TextView uniqueIdView = (TextView) view.findViewById(R.id.row_child_unique_id);
// 
//             HighlightedFieldViewGroup highlightedFieldViewGroup = (HighlightedFieldViewGroup) view.findViewById(R.id.child_field_group);
//             highlightedFieldViewGroup.prepare(baseModel, highlightedFields);
// 
//             ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
//             try {
//                 setFields(String.valueOf(buildTitle(baseModel)), uniqueIdView);
//                 assignThumbnail(baseModel, imageView);
// 
//                 view.setOnClickListener(createClickListener(baseModel, activityToLaunch));
//             } catch (JSONException e) {
//                 throw new RuntimeException(e);
//             }
//         }
//         return view;
//     }
// 
//     private String buildTitle(T baseModel) throws JSONException {
//         if(titleFields.size() == 0) {
//             return baseModel.getShortId();
//         }
//         StringBuilder titleBuilder = new StringBuilder();
//         for (int i = 0; i < titleFields.size(); i++) {
//             FormField titleField = titleFields.get(i);
//             titleBuilder.append(baseModel.optString(titleField.getId()));
//             if((i + 1) < titleFields.size()) {
//                 titleBuilder.append(" ");
//             }
//         }
//         return String.format("%s (%s)", titleBuilder.toString(), baseModel.getShortId());
//     }
// }
// 
// Path: RapidFTR-Android/src/main/java/com/rapidftr/adapter/pagination/ViewAllChildrenPaginatedScrollListener.java
// public class ViewAllChildrenPaginatedScrollListener implements AbsListView.OnScrollListener{
// 
//     public static final int DEFAULT_PAGE_SIZE = 30;
//     public static final int FIRST_PAGE = 30;
//     private ViewAllChildScroller scroller;
// 
//     public ViewAllChildrenPaginatedScrollListener(ChildRepository repository,
//                                                   HighlightedFieldsViewAdapter<Child> adapter) {
//         scroller = new ViewAllChildScroller(repository, adapter);
//     }
// 
//     @Override
//     public void onScroll(AbsListView absListView, int firstVisibleItem,
//                          int numberOfVisibleItems, int numberOfItemsInAdapter) {
//         scroller.updateRecordNumbers(firstVisibleItem, numberOfVisibleItems, numberOfItemsInAdapter);
// 
//         try {
//             scroller.loadRecordsForNextPage();
//         } catch (JSONException e) {
//             throw new RuntimeException(e);
//         }
//     }
// 
//     @Override
//     public void onScrollStateChanged(AbsListView absListView, int i) {}
// }


// Path: RapidFTR-Android/src/main/java/com/rapidftr/activity/ViewAllChildrenActivity.java
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.rapidftr.R;
import com.rapidftr.RapidFtrApplication;
import com.rapidftr.adapter.HighlightedFieldsViewAdapter;
import com.rapidftr.adapter.pagination.ViewAllChildrenPaginatedScrollListener;
import com.rapidftr.model.Child;
import com.rapidftr.repository.ChildRepository;
import lombok.Cleanup;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_children);
        try {
            hideEnquiriesTabIfRapidReg();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        listView(getChildren());
    }

    private List<Child> getChildren() {
        List<Child> children = new ArrayList<Child>();
        @Cleanup ChildRepository childRepository = inject(ChildRepository.class);
        try {
            children = childRepository.getRecordsForFirstPage();
        } catch (JSONException e) {
            Log.e("ViewAllChildrenActivity", "Error while displaying children list");
            makeToast(R.string.fetch_child_error);
        }
        return children;
    }

    private void listView(List<Child> children) {
        HighlightedFieldsViewAdapter highlightedFieldsViewAdapter = new HighlightedFieldsViewAdapter(this, children, Child.CHILD_FORM_NAME, ViewChildActivity.class);
        ListView childListView = (ListView) findViewById(R.id.child_list);
        if (children.isEmpty()) {
            childListView.setEmptyView(findViewById(R.id.no_child_view));
        }
        childListView.setAdapter(highlightedFieldsViewAdapter);
        ViewAllChildrenPaginatedScrollListener scrollListener = new ViewAllChildrenPaginatedScrollListener(inject(ChildRepository.class), highlightedFieldsViewAdapter);