/*
 * Gochino "alto/basso":
 * Estrae un numero casuale tra 5 e 15 (modificare)
 * Presenta un campo nel quale l'utente scrive il suo tentativo
 * Se il numero è sbagliato, un "toast" che dice "alto" oppure "basso"
 * Se è giusto visualizza un dialog con il numero dei tentativi.
 * Alla chiusura del dialog, il gioco ricomincia
 */
package it.docsalvi.altobasso_fragment;


import it.docsalvi.altobasso_fragment.R;

import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	static final int MINVALUE = 5;
	static final int MAXVALUE = 15;
	static int numProve;
	static int daIndovinare;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			TextView prompt = (TextView)rootView.findViewById(R.id.prompt);
			String p = getResources().getString(R.string.messaggio) + " " + String.valueOf(MINVALUE) + " " + 
					getResources().getString(R.string.messaggio_b) + " " + String.valueOf(MAXVALUE);
			prompt.setText(p);
			EditText t = (EditText)rootView.findViewById(R.id.numero);
			t.requestFocus();
			resetGioco ();
			
			Button b = (Button)rootView.findViewById(R.id.bottone);
			b.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					EditText t = (EditText)getActivity().findViewById(R.id.numero);
					String tent = t.getText().toString();
					try {
						int tentativo = Integer.parseInt(tent);
						if (tentativo == daIndovinare) {
							vincita ();
						} else if (tentativo < daIndovinare) {
							Toast.makeText(getActivity().getApplicationContext(),getResources().getString(R.string.toast_cresci), 
					                Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(getActivity().getApplicationContext(),getResources().getString(R.string.toast_cala), 
					                Toast.LENGTH_SHORT).show();
						}
					} catch (Exception e) {
					}
					numProve ++;
					t.setText("");
				}
			});
			return rootView;
		}
		
		void vincita () {
			AlertDialog alertDialog = new AlertDialog.Builder(
					getActivity()).create();

			// Setting Dialog Title
			alertDialog.setTitle(getResources().getString(R.string.alert_titolo));

			// Setting Dialog Message
			alertDialog.setMessage(getResources().getString(R.string.alert_messaggio) + " " + String.valueOf(numProve) + " " +
					getResources().getString(R.string.alert_fine_messaggio));

			// Setting Icon to Dialog
			// alertDialog.setIcon(R.drawable.);

	        // Setting OK Button
	        alertDialog.setButton( DialogInterface.BUTTON_POSITIVE, getResources().getString(R.string.alert_nuovo), new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int which) {
	                	resetGioco ();
	                }
	        });
	        alertDialog.setButton( DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.alert_termina), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                	getActivity().finish();
                }
	        });
        

			// Showing Alert Message
			alertDialog.show();
			
		}
		
		void resetGioco () {
			Random r = new Random();
			numProve = 1;
			daIndovinare = r.nextInt(MAXVALUE - MINVALUE) + MINVALUE;
		}
	}

}
