package com.example.tictactoe;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.view.View.*;


/**
 * @author Alexis
 * activité
 */
/**
 * @author martin
 *
 */
public class MainActivity extends Activity{

	public Boolean playerOneTurn;
	public String[] xoChar = {"X","O"};
	public ThreeStateButton buttons[] = new ThreeStateButton[9];
	public Button apply;
	public Button restart;
	public boolean gameFinished, dansOption;
	public RelativeLayout main;
	public RelativeLayout option;
	public RadioGroup radioOption;
	public Menu menu;
	
	protected void onCreate(Bundle savedInstanceState) {
		//preparer le jeux
		super.onCreate(savedInstanceState);
		playerOneTurn = true;
		gameFinished = false;
		dansOption = false;
		
		//Option page View
		setContentView(R.layout.option);
		option = (RelativeLayout) findViewById(R.id.optionLayout);
		
		apply = (Button)findViewById(R.id.buttonApply);
		apply.setOnClickListener(new OnClickListener(){
	        public void onClick(View v) {
				if(optionsAreOk())
				{
					if(gameFinished == false)
					{
						updateGraphique();
					}
		    		setContentView(main);
		    		dansOption = false;
				}
	        }});
		
		radioOption = (RadioGroup)findViewById(R.id.radioEffect);
		radioOption.setOnCheckedChangeListener(new OnCheckedChangeListener() 
	    {
	        public void onCheckedChanged(RadioGroup group, int checkedId) {
	        	int radioButtonID = radioOption.getCheckedRadioButtonId();
	        	
	        	switch (radioButtonID) {
				case R.id.radioNone:
					((EditText)findViewById(R.id.editText1)).setEnabled(true);
					((EditText)findViewById(R.id.editText2)).setEnabled(true);
					break;
				case R.id.radioSame:
					((EditText)findViewById(R.id.editText1)).setEnabled(true);
					((EditText)findViewById(R.id.editText2)).setEnabled(false);
					break;
				case R.id.radioInvisible:
					((EditText)findViewById(R.id.editText1)).setEnabled(false);
					((EditText)findViewById(R.id.editText2)).setEnabled(false);
					break;
				default:
					break;
				}

	        }
	    });
		
		//Main view
		setContentView(R.layout.activity_main);
		main = (RelativeLayout) findViewById(R.id.mainLayout);
		
		restart = (Button)findViewById(R.id.restart);
		restart.setVisibility(100);
		restart.setOnClickListener(new OnClickListener(){
	        public void onClick(View v) {
	        	recommencer();
	        }});
		
		for(int i = 0; i < 9; i++)
        {
        	String buttonID = "threeStateButton" + (i+1);
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
        	buttons[i] = ((ThreeStateButton)findViewById(resID));
   	 		buttons[i].setTextColor(Color.BLACK);
        }
		
	
		//meteo
		SensorManager sensorMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		//temperature
		Censurer.initThermometer(new Censurer.Value() {
			public void eval(float val) {
				if(!dansOption){
					if (buttons[0].state == State.nothing)
						buttons[0].setText(Integer.toString((int)val));
					if (buttons[1].state == State.nothing)
						buttons[1].setText("deg");
					if (buttons[2].state == State.nothing)
						buttons[2].setText("Cel");
				}
			}
		}, sensorMan);
		buttons[1].setText("Cel");
		
		//humidité
		Censurer.initHygrometer(new Censurer.Value() {
			public void eval(float val) {
				if(!dansOption){
					if (buttons[3].state == State.nothing)
						buttons[3].setText(Integer.toString((int)Math.ceil(val)));
					if (buttons[4].state == State.nothing)
						buttons[4].setText("%");
					if (buttons[5].state == State.nothing)
						buttons[5].setText("h");
				}
			}
		}, sensorMan);
		buttons[4].setText("Bar");
		
		//luminosité
		Censurer.initLumens(new Censurer.Value() {
			public void eval(float val) {
				if(!dansOption){
					if (val < 10){
						if (buttons[6].state == State.nothing)
							buttons[6].setText("zZ");
						if (buttons[7].state == State.nothing)
							buttons[7].setText("zZ");
						if (buttons[8].state == State.nothing)
							buttons[8].setText("zZ");
					}
					else if (val < 100){
						if (buttons[6].state == State.nothing)
							buttons[6].setText("M");
						if (buttons[7].state == State.nothing)
							buttons[7].setText("OO");
						if (buttons[8].state == State.nothing)
							buttons[8].setText("DY");
					}
					else if (val < 500){
						if (buttons[6].state == State.nothing)
							buttons[6].setText("In");
						if (buttons[7].state == State.nothing)
							buttons[7].setText("do");
						if (buttons[8].state == State.nothing)
							buttons[8].setText("or");
					}
					else{
						if (buttons[6].state == State.nothing)
							buttons[6].setText("Su");
						if (buttons[7].state == State.nothing)
							buttons[7].setText("nn");
						if (buttons[8].state == State.nothing)
							buttons[8].setText("Y!");
					}
	
				}
			}}, sensorMan);
		buttons[4].setText("Bar");
		
	}
	
	/**
	 * verify winning condition
	 * position in tictactoe like
	 * [0] [1] [2]
	 * [3] [4] [5]
	 * [6] [7] [8]
	 * @param position as int
	 */
	public void verify(int position){
		String content;
		int colonne = position%3;
		int ligne = position/3;
		ligne = ligne*3;
		
		//player turn
		if(playerOneTurn)
			content = xoChar[0];
		else
			content = xoChar[1];
		
		//verifie colonne
		if(buttons[colonne].getText().equals(content) && buttons[colonne+3].getText().equals(content) && buttons[colonne+6].getText().equals(content))
		{
			buttons[colonne].setTextColor(Color.RED);
			buttons[colonne+3].setTextColor(Color.RED);
			buttons[colonne+6].setTextColor(Color.RED);
			gameFinished = true;
		}
		
		//verifie ligne
		if(buttons[ligne].getText().equals(content) && buttons[ligne+1].getText().equals(content) && buttons[ligne+2].getText().equals(content))
		{
			buttons[ligne].setTextColor(Color.RED);
			buttons[ligne+1].setTextColor(Color.RED);
			buttons[ligne+2].setTextColor(Color.RED);
			gameFinished = true;
		}
		
		if(position % 2 == 0)//coin et milieu((Button)findViewById(R.id.restart))
		{
			if(buttons[0].getText().equals(content) && buttons[4].getText().equals(content) && buttons[8].getText().equals(content))
			{
				buttons[0].setTextColor(Color.RED);
				buttons[4].setTextColor(Color.RED);
				buttons[8].setTextColor(Color.RED);
				gameFinished = true;
			}
			if(buttons[2].getText().equals(content) && buttons[4].getText().equals(content) && buttons[6].getText().equals(content))
			{
				buttons[2].setTextColor(Color.RED);
				buttons[4].setTextColor(Color.RED);
				buttons[6].setTextColor(Color.RED);
				gameFinished = true;
			}
		}
		
		if(gameFinished)
		{
			if(playerOneTurn)
				((TextView)findViewById(R.id.textView1)).setText("Joueur 1 a gagner");
			else
				((TextView)findViewById(R.id.textView1)).setText("Joueur 2 a gagner");
			((Button)findViewById(R.id.restart)).setVisibility(0);
		}
		else
		{
			if(playerOneTurn)
				((TextView)findViewById(R.id.textView1)).setText("Au tour du Joueur 1");
			else
				((TextView)findViewById(R.id.textView1)).setText("Au tour du Joueur 2");
		}
	}

	/**
	 * test d'option
	 * @return
	 */
	private boolean optionsAreOk(){
		if(!((TextView)findViewById(R.id.editText1)).getText().toString().equals(((TextView)findViewById(R.id.editText2)).getText().toString()))
			return true;
		
		return false;
	}
	
	/**
	 * choose symbol for button
	 */
	private void updateGraphique(){
		int radioButtonID = radioOption.getCheckedRadioButtonId();
		xoChar[0] = ((EditText)findViewById(R.id.editText1)).getText().toString();
		
    	if(radioButtonID == R.id.radioSame)
		{
			xoChar[1] = " " + xoChar[0] + " ";
		}
    	else
    		xoChar[1] = ((EditText)findViewById(R.id.editText2)).getText().toString();
		if(radioButtonID == R.id.radioInvisible)
		{
    		xoChar[1] = ((EditText)findViewById(R.id.editText2)).getText().toString();
    		for(int i = 0; i < 9; i++)
    			buttons[i].setTextColor(Color.TRANSPARENT);
		}
		
		for(int i = 0; i < 9; i++)
			buttons[i].updateState();
	}
	
	private void recommencer(){
		playerOneTurn = true;
		gameFinished = false;
		for(int i = 0; i < 9; i++)
        {
   	 		buttons[i].setTextColor(Color.BLACK);
   	 		buttons[i].reset();
        }
		((TextView)findViewById(R.id.textView1)).setText("TicTacToeMeteo");
		restart.setVisibility(100);
	}
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	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);
		menu = _menu;
		return true;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) 
		{
		case R.id.option:
			if(!gameFinished)
			{
				setContentView(option);
				dansOption = true;
			}
			break;
		case R.id.recommencer:
			recommencer();
		    return true;
		case R.id.quitter:
		     finish();
		     return true;
		}
		return false;}
}
