package com.example.examenmartinrobinson;

import Application.CalculTaxe;
import Application.Constant;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.TableRow.LayoutParams;

public class ExamenMartinRobinson extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mainScreen();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.examen_martin_robinson, menu);
		
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.restart:
	        	mainScreen();
	            return true;
	        case R.id.setting:
	            settingScreen();
	            return true;
	        case R.id.about:
	        	aboutScreen();
	        	return true;
	        case R.id.quit:
	        	finish();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private void mainScreen(){
		//toast
		final Toast tstVide = Toast.makeText(this, "Il faut enter un montant dans [Montant initial]", 8),
			        tstInvalide = Toast.makeText(this, "[Montant initial] n'accepte QUE les nombres entier",8),
			        tstRecommencer = Toast.makeText(this, "recommencer", 8);
		
		//tableau
		TableLayout tbl = new TableLayout(this);
		TableRow [] rows = new TableRow[5];
		for (int i=0;i<5;i++){
			rows[i] = new TableRow(this);
			tbl.addView(rows[i]);
		}
		
		//les fields editables
		final EditText MontantInitial = new EditText(this),
					   Tps = new EditText(this), 
					   MontantAvecTps = new EditText(this), 
					   MontantAvecTps2 = new EditText(this), 
					   Tvq = new EditText(this), 
					   MontantTotal = new EditText(this);
		MontantInitial.setInputType(InputType.TYPE_CLASS_NUMBER);
		Tps.setInputType(InputType.TYPE_CLASS_NUMBER);
		MontantAvecTps.setInputType(InputType.TYPE_CLASS_NUMBER);
		MontantAvecTps2.setInputType(InputType.TYPE_CLASS_NUMBER);
		Tvq.setInputType(InputType.TYPE_CLASS_NUMBER);
		MontantTotal.setInputType(InputType.TYPE_CLASS_NUMBER);
		MontantInitial.setFocusable(true);
		Tps.setFocusable(false);
		MontantAvecTps.setFocusable(false);
		MontantAvecTps2.setFocusable(false);
		Tvq.setFocusable(false);
		MontantTotal.setFocusable(false);
		
		//les labels
		TextView lblMontantInitial = new TextView(this),
				 lblTps = new TextView(this),
				 lblMontantAvecTps = new TextView(this),
				 lblMontantAvecTps2 = new TextView(this),
				 lblTvq = new TextView(this),
				 lblMontantTotal = new TextView(this),
				 lblPlus = new TextView(this),
				 lblEqual = new TextView(this),
				 lblPlus2 = new TextView(this),
				 lblEqual2 = new TextView(this),
				 lblSpacer = new TextView(this),
				 lblSpacer2 = new TextView(this),
				 lblSpacer3 = new TextView(this),
				 lblSpacer4 = new TextView(this),
				 lblSpacer5 = new TextView(this);
		lblMontantInitial.setText("Montant Initial");
		lblTps.setText(String.format("TPS (%.2f %%)",Constant.TPS));
		lblMontantAvecTps.setText("Montant avec TPS");
		lblMontantAvecTps2.setText(lblMontantAvecTps.getText());
		lblTvq.setText(String.format("TVQ (%.2f %%)",Constant.TVQ));
		lblMontantTotal.setText("Montant total");
		lblPlus.setText("+");
		lblEqual.setText("=");
		lblPlus2.setText("+");
		lblEqual2.setText("=");
		
		//bouton
		LinearLayout btnll = new LinearLayout(this);
		Button calculer = new Button(this),
			   recommencer = new Button(this);
		calculer.setText("Calculer");
		recommencer.setText("Recommencer");
		btnll.addView(calculer);
		btnll.addView(recommencer);
		//methode de calculation
		calculer.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				int cash;
				//est vide ?
				if(MontantInitial.getText().length()==0){
					tstVide.show();
					MontantInitial.setText("");
					return;
				}
				//est valide ?
				try{
					cash = Integer.parseInt(MontantInitial.getText().toString());
				}catch(Exception e){
					tstInvalide.show();
					return;
				}
				//ok on y vas
				Tps.setText(String.format("%.2f", CalculTaxe.calculTps(cash)));
				MontantAvecTps.setText(String.format("%.2f", CalculTaxe.MontantTps(cash)));
				MontantAvecTps2.setText(MontantAvecTps.getText());
				Tvq.setText(String.format("%.2f", CalculTaxe.calculTvq(cash)));
				MontantTotal.setText(String.format("%.2f", CalculTaxe.MontantTpsTvq(cash)));
			}
		});
		recommencer.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				MontantInitial.setText("");
				Tps.setText("");
				MontantAvecTps.setText("");
				MontantAvecTps2.setText("");
				Tvq.setText("");
				MontantTotal.setText("");
				MontantInitial.requestFocus();
				tstRecommencer.show();
			}
		});
		
		//remplire la table
		rows[0].addView(lblMontantInitial);
		rows[0].addView(lblSpacer);
		rows[0].addView(lblTps);
		rows[0].addView(lblSpacer2);
		rows[0].addView(lblMontantAvecTps);
		rows[1].addView(MontantInitial);
		rows[1].addView(lblPlus);
		rows[1].addView(Tps);
		rows[1].addView(lblEqual);
		rows[1].addView(MontantAvecTps);
		rows[2].addView(lblMontantAvecTps2);
		rows[2].addView(lblSpacer3);
		rows[2].addView(lblTvq);
		rows[2].addView(lblSpacer4);
		rows[2].addView(lblMontantTotal);
		rows[3].addView(MontantAvecTps2);
		rows[3].addView(lblPlus2);
		rows[3].addView(Tvq);
		rows[3].addView(lblEqual2);
		rows[3].addView(MontantTotal);
		rows[4].addView(btnll);
		TableRow.LayoutParams params = (TableRow.LayoutParams)btnll.getLayoutParams();
		params.span = 5;
		params.width = LayoutParams.MATCH_PARENT;
		btnll.setLayoutParams(params);
		
		//focus sur edittext
		MontantInitial.requestFocus();
		
		
		//finalement
		setContentView(tbl);
	}
	
	private void aboutScreen(){
		Dialog about = new Dialog(this);
		about.setTitle("C'est un examen");
		about.show();
	}
	
	private void settingScreen(){
		final Toast error = Toast.makeText(this, "error", 7);
		
		TableLayout tbl = new TableLayout(this);
		
		TextView lblTPS = new TextView(this),
				 lblTVQ = new TextView(this);
		lblTPS.setText("TPS");
		lblTVQ.setText("TVQ");
		
		final EditText edtTPS = new EditText(this),
				 edtTVQ = new EditText(this);
		edtTPS.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		edtTVQ.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		edtTPS.setText(String.format("%.2f", Constant.TPS));
		edtTVQ.setText(String.format("%.2f", Constant.TVQ));
		
		Button btn = new Button(this);
		btn.setText("save");
		btn.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				float tps,
				      tvq;
				//est vide ?
				if(edtTPS.getText().length()==0){
					error.show();
					edtTPS.setText("");
					return;
				}
				//est valide ?
				try{
					tps = Float.parseFloat(edtTPS.getText().toString());
				}catch(Exception e){
					error.show();
					return;
				}
				//est vide ?
				if(edtTVQ.getText().length()==0){
					error.show();
					edtTVQ.setText("");
					return;
				}
				//est valide ?
				try{
					tvq = Float.parseFloat(edtTVQ.getText().toString());
				}catch(Exception e){
					error.show();
					return;
				}
				
				Constant.TPS = tps;
				Constant.TVQ = tvq;
				
				mainScreen();
			}
		});
		
		TableRow row0 = new TableRow(this),
				 row1 = new TableRow(this),
				 row2 = new TableRow(this);
		row0.addView(lblTPS);
		row0.addView(edtTPS);
		row1.addView(lblTVQ);
		row1.addView(edtTVQ);
		row2.addView(btn);
		tbl.addView(row0);
		tbl.addView(row1);
		tbl.addView(row2);
		
		setContentView(tbl);
	}
	
}
