/*
 * Copyright 2017 Nafundi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.odk.collect.android.activities;

import android.app.ListActivity;
import android.support.annotation.NonNull;
import android.widget.Button;
import android.widget.ListView;

import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.database.ActivityLogger;

abstract class AppListActivity extends ListActivity {
    protected final ActivityLogger logger = Collect.getInstance().getActivityLogger();

    protected boolean areCheckedItems() {
        return getCheckedCount() > 0;
    }

    /** The positions and IDs of the checked items */
    class CheckedItemInfo {
        final int[] positions;
        final long[] ids;

        CheckedItemInfo(int[] positions, long[] ids) {
            this.positions = positions;
            this.ids = ids;
        }
    }

    /** Returns the positions and IDs of the checked items */
    protected CheckedItemInfo getCheckedItemInfo() {
        return getCheckedItemInfo(getListView());
    }

    /** Returns the positions and IDs of the checked items */
    protected CheckedItemInfo getCheckedItemInfo(ListView lv) {
        int itemCount = lv.getCount();
        int checkedItemCount = lv.getCheckedItemCount();
        int[] checkedPositions = new int[checkedItemCount];
        long[] checkedIds = new long[checkedItemCount];
        int resultIndex = 0;
        for (int posIdx = 0; posIdx < itemCount; ++posIdx) {
            if (lv.isItemChecked(posIdx)) {
                checkedPositions[resultIndex] = posIdx;
                checkedIds      [resultIndex] = lv.getItemIdAtPosition(posIdx);
                resultIndex++;
            }
        }
        return new CheckedItemInfo(checkedPositions, checkedIds);
    }

    /** Returns the IDs of the checked items, as an array of Long */
    @NonNull
    protected Long[] getCheckedIdObjects() {
        long[] checkedIds = getCheckedItemInfo().ids;
        Long[] checkedIdObjects = new Long[checkedIds.length];
        for (int i = 0; i < checkedIds.length; ++i) {
            checkedIdObjects[i] = checkedIds[i];
        }
        return checkedIdObjects;
    }

    protected int getCheckedCount() {
        return getListView().getCheckedItemCount();
    }

    // toggles to all checked or all unchecked
    // returns:
    // true if result is all checked
    // false if result is all unchecked
    //
    // Toggle behavior is as follows:
    // if ANY items are unchecked, check them all
    // if ALL items are checked, uncheck them all
    public static boolean toggleChecked(ListView lv) {
        // shortcut null case
        if (lv == null) return false;

        boolean newCheckState = lv.getCount() > lv.getCheckedItemCount();
        setAllToCheckedState(lv, newCheckState);
        return newCheckState;
    }

    public static void setAllToCheckedState(ListView lv, boolean check) {
        // no-op if ListView null
        if (lv == null) return;

        for (int x = 0; x < lv.getCount(); x++) {
            lv.setItemChecked(x, check);
        }
    }

    // Function to toggle button label
    public static void toggleButtonLabel(Button mToggleButton, ListView lv) {
        if (lv.getCheckedItemCount() != lv.getCount()) {
            mToggleButton.setText(R.string.select_all);
        } else {
            mToggleButton.setText(R.string.clear_all);
        }
    }
}

@@ -1151,25 +1151,19 @@
 /** 
-The positions and
+Returns the
  IDs
@@ -1187,149 +1187,85 @@
 tems
- */%0A    class CheckedItemInfo %7B%0A        final int%5B%5D positions;%0A        final long%5B%5D ids;%0A%0A        CheckedItemInfo(int%5B%5D positions, long%5B%5D i
+, using the ListView of this activity. */%0A    protected long%5B%5D getCheckedI
 ds
+(
 ) %7B%0A
@@ -1276,77 +1276,44 @@
     
-    this.positions = positions;%0A            this.ids = ids;%0A        %7D
+return getCheckedIds(getListView());
 %0A   
@@ -1332,38 +1332,24 @@
 Returns the 
-positions and 
 IDs of the c
@@ -1364,247 +1364,243 @@
 tems
- */%0A    protected CheckedItemInfo getCheckedItemInfo() %7B%0A        return getCheckedItemInfo(getListView());
+, using the ListView provided */%0A    protected long%5B%5D getCheckedIds(ListView lv) %7B%0A        // This method could be simplified by using getCheckedItemIds, if one ensured that
 %0A    
-%7D%0A%0A
     /
-** Returns the positions and IDs of the checked items */%0A    protected CheckedItemInfo getCheckedItemInfo(ListView lv) %7B
+/ IDs were %E2%80%9Cstable%E2%80%9D (see the getCheckedItemIds doc).
 %0A   
@@ -1696,68 +1696,8 @@
 ();%0A
-        int%5B%5D checkedPositions = new int%5BcheckedItemCount%5D;%0A
     
@@ -1886,64 +1886,8 @@
 ) %7B%0A
-                checkedPositions%5BresultIndex%5D = posIdx;%0A
     
@@ -2035,46 +2035,8 @@
 turn
- new CheckedItemInfo(checkedPositions,
  che
@@ -2042,17 +2042,16 @@
 eckedIds
-)
 ;%0A    %7D%0A
@@ -2221,21 +2221,12 @@
 kedI
-temInfo().i
 ds
+()
 ;%0A  

Simplify AppListActivity getCheckedItemInfo, now that we no longer need item positions.