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 com.androidsx.libraryrateme.R;

public class DialogRateMe extends DialogFragment {
	private static final String EXTRA_PACKAGE_NAME = "package-name";

	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 packageName) {
		DialogRateMe dialogo = new DialogRateMe();
		Bundle args = new Bundle();
		args.putString(EXTRA_PACKAGE_NAME, packageName);
		dialogo.setArguments(args);
		return dialogo;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		appPackageName = getArguments().getString(EXTRA_PACKAGE_NAME);
		initializeUiFields();

		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) {
						if (rating >= 4.0) {
							rateMe.setVisibility(View.VISIBLE);
							noThanks.setVisibility(View.GONE);
						} else {
							noThanks.setVisibility(View.VISIBLE);
							rateMe.setVisibility(View.GONE);
						}
					}
				});
		configureButtons();
		close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();

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

	private void initializeUiFields() {
		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 configureButtons() {
		rateMe.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				rateApp();
			}
		});
		
		noThanks.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO: fill with real strings
				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();
				}
			}
		});
	}
}

@@ -2149,17 +2149,21 @@
 library,
- 
+%0A%09%09%09%09
 null);%0A%09
@@ -2883,16 +2883,443 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09private void goToMail() %7B%0A%09%09Intent intent = new Intent(Intent.ACTION_SEND);%0A%09%09intent.setType(%22plain/text%22);%0A%09%09intent.putExtra(Intent.EXTRA_EMAIL, new String%5B%5D %7B %22some@email.com%22 %7D);%0A%09%09intent.putExtra(Intent.EXTRA_SUBJECT, %22subject%22);%0A%09%09intent.putExtra(Intent.EXTRA_TEXT, %22mail body%22);%0A%09%09try %7B%0A%09%09%09startActivity(Intent.createChooser(intent, %22%22));%0A%09%09%7D catch (android.content.ActivityNotFoundException ex) %7B%0A%09%09%09rateApp();%0A%09%09%7D%0A%09%7D%0A%0A
 %09private
@@ -3345,16 +3345,16 @@
 ons() %7B%0A
-
 %09%09rateMe
@@ -3473,11 +3473,9 @@
 %7D);%0A
-%09%09
 %0A
+
 %09%09no
@@ -3578,461 +3578,19 @@
 %09%09%09%09
-// TODO: fill with real strings%0A%09%09%09%09Intent intent = new Intent(Intent.ACTION_SEND);%0A%09%09%09%09intent.setType(%22plain/text%22);%0A%09%09%09%09intent.putExtra(Intent.EXTRA_EMAIL,%0A%09%09%09%09%09%09new String%5B%5D %7B %22some@email.com%22 %7D);%0A%09%09%09%09intent.putExtra(Intent.EXTRA_SUBJECT, %22subject%22);%0A%09%09%09%09intent.putExtra(Intent.EXTRA_TEXT, %22mail body%22);%0A%09%09%09%09try %7B%0A%09%09%09%09%09startActivity(Intent.createChooser(intent, %22%22));%0A%09%09%09%09%7D catch (android.content.ActivityNotFoundException ex) %7B%0A%09%09%09%09%09rateApp();%0A%09%09%09%09%7D
+goToMail();
 %0A%09%09%09

create Method