package com.expidev.gcmapp.support.v4.content;

import static com.expidev.gcmapp.Constants.ARG_MINISTRY_ID;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.expidev.gcmapp.db.Contract;
import com.expidev.gcmapp.db.MinistriesDao;
import com.expidev.gcmapp.model.Church;
import com.expidev.gcmapp.utils.BroadcastUtils;

import org.ccci.gto.android.common.support.v4.content.AsyncTaskBroadcastReceiverLoader;

import java.util.List;

public class ChurchesLoader extends AsyncTaskBroadcastReceiverLoader<List<Church>> {
    private final MinistriesDao mDao;
    @Nullable
    private final String mMinistryId;

    public ChurchesLoader(@NonNull final Context context, @Nullable final Bundle args,
                          @NonNull final IntentFilter... filters) {
        this(context, args != null ? args.getString(ARG_MINISTRY_ID) : null, filters);
    }

    public ChurchesLoader(@NonNull final Context context, @Nullable final String ministryId,
                          @NonNull final IntentFilter... filters) {
        super(context, filters);
        if(ministryId != null) {
            setBroadcastReceiver(new ChurchLoaderBroadcastReceiver(this, ministryId));
            addIntentFilter(BroadcastUtils.updateChurchesFilter());
        }
        mDao = MinistriesDao.getInstance(context);
        mMinistryId = ministryId;
    }

    @Override
    public List<Church> loadInBackground() {
        if (mMinistryId != null) {
            return mDao.get(Church.class, Contract.Church.SQL_WHERE_MINISTRY_ID, new String[] {mMinistryId});
        }
        return null;
    }
}

@@ -408,16 +408,58 @@
 Church;%0A
+import com.expidev.gcmapp.model.Ministry;%0A
 import c
@@ -741,23 +741,22 @@
 ;%0A    @N
+onN
 ull
-able
 %0A    pri
@@ -1511,16 +1511,59 @@
 nistryId
+ != null ? ministryId : Ministry.INVALID_ID
 ;%0A    %7D%0A
@@ -1633,16 +1633,44 @@
      if 
+(!Ministry.INVALID_ID.equals
 (mMinist
@@ -1669,32 +1669,25 @@
 (mMinistryId
- != null
+)
 ) %7B%0A        

update ChurchesLoader to understand Ministry.INVALID_ID