package dlei.forkme.gui.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;

import dlei.forkme.R;

/** Just need this to show
 * StarNotificationDialog s = new StarNotificationDialog();
 s.show(getFragmentManager(), "test");
 */

/**
 * Fragment to set a notification, allows the user to set the notification for some time within 24 hours.
 */
public class StarNotificationDialog extends DialogFragment {

    /**
     * Create a new instance of StarNotificationDialog, providing "repositoryFullName" as an argument.
     * This is called before OnCreateDialog().
     */
    public static StarNotificationDialog newInstance(String repositoryFullName) {
        Log.d("StarNotifiDialog: ", "newInstance() called, param: " + repositoryFullName);

        StarNotificationDialog f = new StarNotificationDialog();
        // Supply repositoryFullName input as an argument.
        Bundle args = new Bundle();
        args.putString("repositoryFullName", repositoryFullName);
        f.setArguments(args);
        return f;
    }

    /**
     * Creates new StarNotificationDialog.
     * This is called after newInstance() so it has access to args.
     * @param savedInstanceState bundle passed in from newInstance containing args.
     * @return built Dialog.
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String repositoryFullName = getArguments().getString("repositoryFullName");
        Log.d("StarNotifiDialog: ", "onCreateDialog() Got Arg: " + repositoryFullName);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogSpinnerView = inflater.inflate(R.layout.diaglog_spinner, null);
        AppCompatTextView t = (AppCompatTextView) dialogSpinnerView.findViewById(R.id.messageText);
        // Concatenate new string with repo name.
        t.setText(getResources().getText(R.string.dialog_star_notification_message_first_half) +
                repositoryFullName +
                getResources().getText(R.string.dialog_star_notification_message_second_half));
        final Spinner s = (Spinner) dialogSpinnerView.findViewById(R.id.dialogTimeSpinner);

        builder
                .setView(dialogSpinnerView)
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String selected = (String) s.getAdapter().getItem(s.getSelectedItemPosition());
                        Log.wtf("Selected: ", selected);
                        Log.wtf("index selected: ", "" +s.getSelectedItemPosition());
                        Log.wtf("Selected: ", (String) s.getSelectedItem());

                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog.
                    }
                });
        // Create the AlertDialog object and return it.
        return builder.create();
    }
}

@@ -142,57 +142,244 @@
 oid.
-content.DialogInterface;%0Aimport android.os.Bundle
+app.NotificationManager;%0Aimport android.app.PendingIntent;%0Aimport android.content.Context;%0Aimport android.content.DialogInterface;%0Aimport android.content.Intent;%0Aimport android.os.Bundle;%0Aimport android.support.v4.app.NotificationCompat
 ;%0Aim
@@ -568,16 +568,139 @@
 orkme.R;
+%0Aimport dlei.forkme.gui.activities.SettingsActivity;%0Aimport dlei.forkme.gui.activities.github.TrendingRepositoriesActivity;
 %0A%0A/** Ju
@@ -1941,24 +1941,30 @@
 ) %7B%0A        
+final 
 String repos
@@ -3358,16 +3358,77 @@
 Item());
+%0A                        AddNotification(repositoryFullName);
 %0A%0A      
@@ -3807,11 +3807,972 @@
 );%0A    %7D
+%0A%0A    private void AddNotification(String repositoryFullName) %7B%0A        Context context = getContext();%0A        NotificationCompat.Builder builder =%0A                new NotificationCompat.Builder(context)%0A                        .setSmallIcon(R.drawable.ic_fork)%0A                        .setContentTitle(%22ForkMe Reminder!%22)%0A                        .setContentText(%22Reminder to look at %22 + repositoryFullName);%0A        // This is the activity started from the notification.%0A        //Intent notificationIntent = new Intent(this, SettingsActivity.class);%0A//        PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, notificationIntent,%0A//                PendingIntent.FLAG_UPDATE_CURRENT);%0A//        builder.setContentIntent(contentIntent);%0A%0A        // Add as notification%0A        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);%0A        manager.notify(0, builder.build());%0A    %7D%0A%0A%0A
 %0A%7D%0A

Add notification base code