package com.lloydtorres.stately.login;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.lloydtorres.stately.R;
import com.lloydtorres.stately.dto.UserLogin;
import com.lloydtorres.stately.helpers.SparkleHelper;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Lloyd on 2016-02-03.
 * This dialog is shown for switching active nations.
 */
public class SwitchNationDialog extends DialogFragment {
    public static final String DIALOG_TAG = "fragment_switch_dialog";
    public static final String LOGINS_KEY = "logins";

    // RecyclerView variables
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mRecyclerAdapter;

    private ArrayList<UserLogin> logins;

    public void setLogins(ArrayList<UserLogin> l)
    {
        logins = l;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            setStyle(DialogFragment.STYLE_NORMAL, R.style.AlertDialogCustom);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_paddedrecycler, null);

        // Restore saved state
        if (savedInstanceState != null)
        {
            logins = savedInstanceState.getParcelableArrayList(LOGINS_KEY);
        }
        initRecycler(view);

        DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SparkleHelper.startAddNation(getContext());
            }
        };

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
        dialogBuilder.setTitle(R.string.menu_switch)
                .setView(view)
                .setPositiveButton(R.string.add_nation, dialogListener);

        return dialogBuilder.create();
    }

    private void initRecycler(View view)
    {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_padded);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        Collections.sort(logins);
        mRecyclerAdapter = new SwitchNationRecyclerAdapter(getContext(), this, logins);
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Save state
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(LOGINS_KEY, logins);
    }
}

@@ -330,16 +330,73 @@
 erView;%0A
+import android.support.v7.widget.helper.ItemTouchHelper;%0A
 import a
@@ -419,24 +419,24 @@
 utInflater;%0A
-
 import andro
@@ -1893,32 +1893,33 @@
 KEY);%0A        %7D%0A
+%0A
         initRecy
@@ -1927,24 +1927,55 @@
 ler(view);%0A%0A
+        // Build actual dialog%0A
         Dial
@@ -2562,32 +2562,63 @@
 iew view)%0A    %7B%0A
+        // Base recycler stuff%0A
         mRecycle
@@ -2961,32 +2961,32 @@
  this, logins);%0A
-
         mRecycle
@@ -3016,24 +3016,1087 @@
 lerAdapter);
+%0A%0A        // Add swipe to delete%0A        ItemTouchHelper.SimpleCallback deleteCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) %7B%0A            @Override%0A            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) %7B%0A                // not needed%0A                return false;%0A            %7D%0A%0A            @Override%0A            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) %7B%0A                // remove on swipe%0A                int pos = viewHolder.getAdapterPosition();%0A%0A                if (pos != RecyclerView.NO_POSITION)%0A                %7B%0A                    logins.get(pos).delete();%0A                    logins.remove(pos);%0A                    mRecyclerAdapter.notifyItemRemoved(pos);%0A                    mRecyclerAdapter.notifyItemRangeChanged(pos, logins.size());%0A                %7D%0A            %7D%0A        %7D;%0A        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(deleteCallback);%0A        itemTouchHelper.attachToRecyclerView(mRecyclerView);
 %0A    %7D%0A%0A    

Add swipe to remove for nation switch