package com.armandgray.taap.detail;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.armandgray.taap.R;
import com.armandgray.taap.models.SessionLog;
import com.armandgray.taap.utils.SessionLogRvAdapter;

import java.util.Date;

public class DetailSummaryDialog extends DialogFragment {

    public static final String DIALOG = "DIALOG";
    public static final String SESSION_LOG = "SESSION_LOG";
    Activity activity;
    private RecyclerView rvSummary;
    public DetailSummaryDialogListener listener;

    public static DetailSummaryDialog newInstance(SessionLog sessionLog) {
        return null;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listener = (DetailSummaryDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement DetailSummaryDialogListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        @SuppressLint("InflateParams") View dialogLayout = LayoutInflater.from(getActivity())
                .inflate(R.layout.detail_summary_dialog_layout, null);
        rvSummary = (RecyclerView) dialogLayout.findViewById(R.id.rvSummary);
        builder.setView(dialogLayout)
                .setNeutralButton("Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onDialogDismiss();
                    }
                });
        setupRvSummary();
        return builder.create();
    }

    private void setupRvSummary() {
        SessionLog sessionLog = new SessionLog.Builder()
                .sessionLength(new Date(0))
                .sessionGoal(new Date(0))
                .activeWork(new Date(0))
                .restTime(new Date(0))
                .setsCompleted(0)
                .repsCompleted(0)
                .successRate(0.24)
                .successRecord(0.0)
                .create();
        rvSummary.setAdapter(new SessionLogRvAdapter(sessionLog));
        GridLayoutManager layoutManager = getGridLayoutManager();
        rvSummary.setLayoutManager(layoutManager);
    }

    @NonNull
    private GridLayoutManager getGridLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(activity, 2);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == 0 ? 2 : 1;
            }
        });
        return layoutManager;
    }

    interface DetailSummaryDialogListener {
        void onDialogDismiss();
    }
}

@@ -1027,19 +1027,215 @@
     
-return null
+Bundle args = new Bundle();%0A        DetailSummaryDialog fragment = new DetailSummaryDialog();%0A        args.putParcelable(SESSION_LOG, sessionLog);%0A        fragment.setArguments(args);%0A        return fragment
 ;%0A  

put passing Parcelable on dialog fragment args