
package com.battlelancer.seriesguide.ui;

import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.util.AnalyticsUtils;
import com.battlelancer.seriesguide.util.ShareUtils.TraktCredentialsDialogFragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class WelcomeDialogFragment extends DialogFragment {

    private static final String TAG = "WelcomeDialogFragment";

    public static boolean hasSeenWelcomeDialog(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean("accepted_eula", false);
    }

    public void fireTrackerEvent(String label) {
        AnalyticsUtils.getInstance(getActivity()).trackEvent(TAG, "Click", label, 0);
    }

    public static WelcomeDialogFragment newInstance() {
        WelcomeDialogFragment f = new WelcomeDialogFragment();
        return f;
    }

    public static void showWelcomeDialog(FragmentActivity activity) {
        // show welcome dialog
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        Fragment prev = activity.getSupportFragmentManager().findFragmentByTag("welcome-dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        WelcomeDialogFragment newFragment = WelcomeDialogFragment.newInstance();
        newFragment.show(ft, "welcome-dialog");
    }

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

        v.findViewById(R.id.welcome_setuptrakt).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fireTrackerEvent("Setup trakt account");
                TraktCredentialsDialogFragment newFragment = TraktCredentialsDialogFragment
                        .newInstance();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                newFragment.show(ft, "traktdialog");
            }
        });

        final CheckBox cb = (CheckBox) v.findViewById(R.id.welcome_sendusagedata);

        return new AlertDialog.Builder(getActivity()).setIcon(R.drawable.icon)
                .setTitle(R.string.welcome_message).setView(v)
                .setPositiveButton(R.string.dismiss, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... voids) {
                                SharedPreferences sp = PreferenceManager
                                        .getDefaultSharedPreferences(getActivity());
                                sp.edit()
                                        .putBoolean("accepted_eula", true)
                                        .putBoolean(SeriesGuidePreferences.KEY_GOOGLEANALYTICS,
                                                cb.isChecked()).commit();
                                return null;
                            }
                        }.execute();
                    }
                }).setCancelable(false).create();
    }
}

@@ -2905,16 +2905,74 @@
 edata);%0D
+%0A        final FragmentActivity activity = getActivity();%0D
 %0A%0D%0A     
@@ -3005,29 +3005,24 @@
 Builder(
-getA
+a
 ctivity
-()
 ).setIco
@@ -3627,29 +3627,24 @@
 erences(
-getA
+a
 ctivity
-()
 );%0D%0A    

Save activity reference for welcome dialog.