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

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

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.expidev.gcmapp.R;
import com.expidev.gcmapp.model.Assignment;
import com.expidev.gcmapp.model.Ministry;
import com.expidev.gcmapp.service.GmaSyncService;
import com.expidev.gcmapp.support.v4.content.MinistryLoader;
import com.expidev.gcmapp.tasks.CreateAssignmentTask;

import org.ccci.gto.android.common.support.v4.app.SimpleLoaderCallbacks;
import org.ccci.gto.android.common.support.v4.fragment.AbstractDialogFragment;
import org.ccci.gto.android.common.support.v4.util.FragmentUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.Optional;

public class JoinMinistryDialogFragment extends AbstractDialogFragment {
    private static final int LOADER_MINISTRY = 1;

    /* Loader callback objects */
    private final MinistryLoaderCallbacks mLoaderCallbacksMinistry = new MinistryLoaderCallbacks();

    @Optional
    @Nullable
    @InjectView(R.id.name)
    TextView mNameView;
    @Optional
    @Nullable
    @InjectView(R.id.join)
    Button mJoinButton;
    @Optional
    @Nullable
    @InjectView(R.id.progressBar)
    View mProgressView;

    @Nullable
    private String mGuid = null;
    @NonNull
    private String mMinistryId = Ministry.INVALID_ID;

    @Nullable
    private Ministry mMinistry = null;

    public static JoinMinistryDialogFragment newInstance(@NonNull final String guid, @NonNull final String ministryId) {
        final JoinMinistryDialogFragment fragment = new JoinMinistryDialogFragment();

        final Bundle args = new Bundle(2);
        args.putString(ARG_GUID, guid);
        args.putString(ARG_MINISTRY_ID, ministryId);
        fragment.setArguments(args);

        return fragment;
    }

    /* BEGIN lifecycle */

    @Override
    public void onCreate(@Nullable final Bundle savedState) {
        super.onCreate(savedState);

        final Bundle args = this.getArguments();
        mGuid = args != null ? args.getString(ARG_GUID) : null;
        final String ministryId = args != null ? args.getString(ARG_MINISTRY_ID) : null;
        mMinistryId = ministryId != null ? ministryId : Ministry.INVALID_ID;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable final Bundle savedState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setView(R.layout.fragment_dialog_join_ministry);
        } else {
            builder.setView(LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialog_join_ministry, null));
        }
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();
        startLoaders();
        ButterKnife.inject(this, getDialog());
        updateViews();
    }

    void onLoadMinistry(@Nullable final Ministry ministry) {
        mMinistry = ministry;
        updateViews();
    }

    @Optional
    @OnClick(R.id.join)
    void onJoinMinistry() {
        if (mGuid != null) {
            if (mJoinButton != null) {
                mJoinButton.setEnabled(false);
            }
            if (mProgressView != null) {
                mProgressView.setVisibility(View.VISIBLE);
            }

            new CreateAssignmentTask(getActivity(), mMinistryId, Assignment.Role.SELF_ASSIGNED, mGuid) {
                @Override
                protected void onPostExecute(final Assignment assignment) {
                    super.onPostExecute(assignment);

                    if (assignment != null) {
                        // trigger a forced background sync of all assignments
                        GmaSyncService.syncAssignments(getActivity(), assignment.getGuid(), true);
                    }

                    // TODO: we need to handle failed requests

                    onJoinedMinistry();
                }
            }.execute();
        }
    }

    void onJoinedMinistry() {
        dismiss();

        // let parent fragment/activity know we joined the ministry
        final OnJoinMinistryListener listener = FragmentUtils.getListener(this, OnJoinMinistryListener.class);
        if (listener != null) {
            listener.onJoinedMinistry(mMinistryId);
        }
    }

    @Optional
    @OnClick(R.id.cancel)
    void onCancelJoin() {
        dismiss();
    }

    @Override
    public void onStop() {
        super.onStop();
        ButterKnife.reset(this);
    }

    /* END lifecycle */

    private void updateViews() {
        if (mNameView != null) {
            mNameView.setText(mMinistry != null ? mMinistry.getName() : null);
        }
    }

    private void startLoaders() {
        final LoaderManager manager = this.getLoaderManager();

        final Bundle args = new Bundle(1);
        args.putString(ARG_MINISTRY_ID, mMinistryId);

        manager.initLoader(LOADER_MINISTRY, args, mLoaderCallbacksMinistry);
    }

    public interface OnJoinMinistryListener {
        void onJoinedMinistry(@NonNull String ministryId);
    }

    private class MinistryLoaderCallbacks extends SimpleLoaderCallbacks<Ministry> {
        @Nullable
        @Override
        public Loader<Ministry> onCreateLoader(final int id, @Nullable final Bundle args) {
            switch (id) {
                case LOADER_MINISTRY:
                    return new MinistryLoader(getActivity(), args);
                default:
                    return null;
            }
        }

        @Override
        public void onLoadFinished(@NonNull final Loader<Ministry> loader, @Nullable final Ministry ministry) {
            switch (loader.getId()) {
                case LOADER_MINISTRY:
                    onLoadMinistry(ministry);
                    break;
            }
        }
    }
}

@@ -156,16 +156,56 @@
 RY_ID;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -2517,31 +2517,16 @@
  mGuid =
- args != null ?
  args.ge
@@ -2542,23 +2542,16 @@
 RG_GUID)
- : null
 ;%0A      
@@ -2552,30 +2552,18 @@
         
-final String 
 m
+M
 inistryI
@@ -2569,23 +2569,8 @@
 Id =
- args != null ?
  arg
@@ -2601,92 +2601,8 @@
 _ID)
- : null;%0A        mMinistryId = ministryId != null ? ministryId : Ministry.INVALID_ID
 ;%0A  
@@ -2624,32 +2624,67 @@
 ll%0A    @Override
+%0A    @SuppressLint(%22InflateParams%22)
 %0A    public Dial

simplify argument processing in onCreate