/*
 * Copyright 2012 Artur Dryomov
 *
 * 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 ru.ming13.gambit.ui.task;


import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import ru.ming13.gambit.provider.GambitContract;
import ru.ming13.gambit.ui.bus.BusEvent;
import ru.ming13.gambit.ui.bus.BusProvider;
import ru.ming13.gambit.ui.bus.DeckCardsOrderQueriedEvent;


public class DeckCardsOrderQueryingTask extends AsyncTask<Void, Void, BusEvent>
{
	private final ContentResolver contentResolver;
	private final Uri cardsUri;

	public static void execute(ContentResolver contentResolver, Uri cardsUri) {
		new DeckCardsOrderQueryingTask(contentResolver, cardsUri).execute();
	}

	private DeckCardsOrderQueryingTask(ContentResolver contentResolver, Uri cardsUri) {
		this.contentResolver = contentResolver;
		this.cardsUri = cardsUri;
	}

	@Override
	protected BusEvent doInBackground(Void... parameters) {
		if (areCardsShuffled(queryCards())) {
			return new DeckCardsOrderQueriedEvent(DeckCardsOrderQueriedEvent.CardsOrder.SHUFFLE);
		}
		else {
			return new DeckCardsOrderQueriedEvent(DeckCardsOrderQueriedEvent.CardsOrder.ORIGINAL);
		}
	}

	private Cursor queryCards() {
		String[] projection = {GambitContract.Cards.ORDER_INDEX};

		return contentResolver.query(cardsUri, projection, null, null, null);
	}

	private boolean areCardsShuffled(Cursor cardsCursor) {
		while (cardsCursor.moveToNext()) {
			int cardOrderIndex = cardsCursor.getInt(
				cardsCursor.getColumnIndex(GambitContract.Cards.ORDER_INDEX));

			if (cardOrderIndex != GambitContract.Cards.DEFAULT_ORDER_INDEX) {
				return true;
			}
		}

		return false;
	}

	@Override
	protected void onPostExecute(BusEvent busEvent) {
		super.onPostExecute(busEvent);

		BusProvider.getInstance().post(busEvent);
	}
}

@@ -1509,28 +1509,16 @@
 huffled(
-queryCards()
 )) %7B%0A%09%09%09
@@ -1727,184 +1727,47 @@
 ate 
-Cursor queryCards() %7B%0A%09%09String%5B%5D projection = %7BGambitContract.Cards.ORDER_INDEX%7D;%0A%0A%09%09return contentResolver.query(cardsUri, projection, null, null, null);%0A%09%7D%0A%0A%09private 
+boolean areCardsShuffled() %7B%0A%09%09
 boolean 
 areC
@@ -1754,36 +1754,33 @@
 d() %7B%0A%09%09boolean 
-areC
+c
 ardsShuffled(Cur
@@ -1775,17 +1775,29 @@
 Shuffled
-(
+ = false;%0A%0A%09%09
 Cursor c
@@ -1806,19 +1806,33 @@
 dsCursor
-) %7B
+ = queryCards();%0A
 %0A%09%09while
@@ -2046,22 +2046,31 @@
 ) %7B%0A%09%09%09%09
-return
+cardsShuffled =
  true;%0A%09
@@ -2084,20 +2084,220 @@
 %0A%0A%09%09
-return false
+cardsCursor.close();%0A%0A%09%09return cardsShuffled;%0A%09%7D%0A%0A%09private Cursor queryCards() %7B%0A%09%09String%5B%5D projection = %7BGambitContract.Cards.ORDER_INDEX%7D;%0A%0A%09%09return contentResolver.query(cardsUri, projection, null, null, null)
 ;%0A%09%7D

Change cards order querying to close cursor.