package it.unimi.di.laren.smartwash.Controller;

import java.util.Random;

import it.unimi.di.laren.smartwash.R;
import it.unimi.di.laren.smartwash.Model.Database;
import it.unimi.di.laren.smartwash.Model.SpinnerItemAdapter;
import it.unimi.di.laren.smartwash.View.ElaborationActivity;
import it.unimi.di.laren.smartwash.View.PlayActivity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class Play_Controller implements OnClickListener{
	private PlayActivity playActivity = null;
	private int spin;
	private int spin1;
	private int spin2;
	private int spin3;
	private Random r = new Random();
	private int a = 0;
	private Database database = new Database();
	public String sc;
	

	public Play_Controller(PlayActivity activity) {
		// TODO Auto-generated constructor stub
		playActivity = activity;
	}
	public Play_Controller(){
		return;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.GoBtn:
			if(playActivity.selezione[0] == 0){
				Toast.makeText(playActivity, "Select all the values", Toast.LENGTH_SHORT).show();
			}
			else if(playActivity.selezione[1] == 0){
				Toast.makeText(playActivity, "Select all the values", Toast.LENGTH_SHORT).show();
			}
			else if(playActivity.selezione[2] == 0){
				Toast.makeText(playActivity, "Select all the values", Toast.LENGTH_SHORT).show();
			}
			else if(playActivity.selezione[3] == 0){
				Toast.makeText(playActivity, "Select all the values", Toast.LENGTH_SHORT).show();
			}
			else{
				sc = String.valueOf(playActivity.selezione[0])  + String.valueOf(playActivity.selezione[1]) + String.valueOf(playActivity.selezione[2]) + String.valueOf(playActivity.selezione[3]);
				for(int i = 0; i<playActivity.selezione.length; i++){
					Log.i("Selex item",": "+playActivity.selezione[i]);
				}
				Log.i("scelta", ": " + sc);
				int scel = Integer.parseInt(sc);
				
			Intent showGo = new Intent(playActivity.getApplicationContext(),
					ElaborationActivity.class);
			showGo.putExtra("SCELTA", scel);
			Log.i("scelta", ": " + sc);
			playActivity.startActivity(showGo);
			}
			break;
			
		case R.id.RandBtn:
			spin = r.nextInt(4-1) + 1;
			playActivity.spinner.setSelection(spin);
			playActivity.selezione[0] = spin;
			spin1 = r.nextInt(4-1) + 1;
			playActivity.spinner1.setSelection(spin1);
			playActivity.selezione[1] = spin1;
			spin2 = r.nextInt(4-1) + 1;
			playActivity.spinner2.setSelection(spin2);
			playActivity.selezione[2] = spin2;
			spin3 = r.nextInt(4-1) + 1;
			playActivity.spinner3.setSelection(spin3);
			playActivity.selezione[3] = spin3;
			for(int i = 0; i<playActivity.selezione.length; i++){
				Log.i("Selex item",": "+playActivity.selezione[i]);
			}
			break;
		default:
			break;
		}

	}

}
