package com.androidsx.rateme;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

import com.androidsx.libraryrateme.R;

public class DialogRateMe extends DialogFragment {

	private String appPackageName;
	private View mView;
	private View tView;
	private Button close;
	private RatingBar ratingBar;
	private LayerDrawable stars;
	private Button RateMe;
	private Button NoThanks;

	public static DialogRateMe newInstance(String appName) {
		DialogRateMe dialogo = new DialogRateMe();
		Bundle args = new Bundle();
		args.putString("name", appName);
		dialogo.setArguments(args);
		return dialogo;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		appPackageName = getArguments().getString("name");
		setupUI();

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		stars.getDrawable(2).setColorFilter(Color.YELLOW,
				PorterDuff.Mode.SRC_ATOP);
		ratingBar
				.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						Toast.makeText(getActivity(),
								"Puntuacion: " + String.valueOf(rating),
								Toast.LENGTH_SHORT).show();
						if (rating >= 4.0) {
							RateMe.setVisibility(View.VISIBLE);
							NoThanks.setVisibility(View.GONE);
							goToPlayStore();

						} else {
							NoThanks.setVisibility(View.VISIBLE);
							RateMe.setVisibility(View.GONE);
							goToEmail();
						}
					}
				});

		close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();

			}
		});
		return builder.setView(mView).setCustomTitle(tView)
				.setCancelable(false).create();

	}

	private void setupUI() {
		mView = getActivity().getLayoutInflater().inflate(R.layout.library,
				null);
		tView = getActivity().getLayoutInflater().inflate(R.layout.title, null);
		close = (Button) tView.findViewById(R.id.cerrar);
		RateMe = (Button) mView.findViewById(R.id.buttonRateMe);
		NoThanks = (Button) mView.findViewById(R.id.buttonThanks);

		ratingBar = (RatingBar) mView.findViewById(R.id.ratingBar);
		stars = (LayerDrawable) ratingBar.getProgressDrawable();

	}

	private void rateApp() {
		try {
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("market://details?id=" + appPackageName)));
		} catch (android.content.ActivityNotFoundException anfe) {
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://play.google.com/store/apps/details?id="
							+ appPackageName)));
		}
	}

	private void goToPlayStore() {

			RateMe.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					rateApp();
				}
			});
	}
	
	private void goToEmail(){
		NoThanks.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("plain/text");
				intent.putExtra(Intent.EXTRA_EMAIL,
						new String[] { "some@email.com" });
				intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
				intent.putExtra(Intent.EXTRA_TEXT, "mail body");
				try {
					startActivity(Intent.createChooser(intent, ""));
				} catch (android.content.ActivityNotFoundException ex) {
					rateApp();
				}
			}
		});
	}
}

@@ -739,17 +739,17 @@
  Button 
-R
+r
 ateMe;%0A%09
@@ -763,17 +763,17 @@
  Button 
-N
+n
 oThanks;
@@ -1636,33 +1636,33 @@
 = 4.0) %7B%0A%09%09%09%09%09%09%09
-R
+r
 ateMe.setVisibil
@@ -1679,33 +1679,33 @@
 ISIBLE);%0A%09%09%09%09%09%09%09
-N
+n
 oThanks.setVisib
@@ -1769,17 +1769,17 @@
 %0A%09%09%09%09%09%09%09
-N
+n
 oThanks.
@@ -1814,17 +1814,17 @@
 %0A%09%09%09%09%09%09%09
-R
+r
 ateMe.se
@@ -2346,17 +2346,17 @@
 rar);%0A%09%09
-R
+r
 ateMe = 
@@ -2405,17 +2405,17 @@
 eMe);%0A%09%09
-N
+n
 oThanks 
@@ -2976,17 +2976,17 @@
 ) %7B%0A%0A%09%09%09
-R
+r
 ateMe.se
@@ -3137,17 +3137,17 @@
 il()%7B%0A%09%09
-N
+n
 oThanks.
@@ -3231,24 +3231,60 @@
 k(View v) %7B%0A
+%09%09%09%09// TODO: fill with real strings%0A
 %09%09%09%09Intent i

Rename Button variables