package com.espritmobile.sim.restocheck;

import java.math.BigDecimal;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BillActivity extends Activity implements OnClickListener {

	Button un, deux, trois, quattre, cinq, six, sept, huit, neuf, zero, back;
	TextView bill;
	Button next;
	String montant = "0.00";
	String price;
	int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill);
		Intent i_table = getIntent();
		count = i_table.getIntExtra("count", 2);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		un = (Button) findViewById(R.id.un);
		deux = (Button) findViewById(R.id.deux);
		trois = (Button) findViewById(R.id.trois);
		quattre = (Button) findViewById(R.id.quattre);
		cinq = (Button) findViewById(R.id.cinq);
		six = (Button) findViewById(R.id.six);
		sept = (Button) findViewById(R.id.sept);
		huit = (Button) findViewById(R.id.huit);
		neuf = (Button) findViewById(R.id.neuf);
		zero = (Button) findViewById(R.id.zero);
		back = (Button) findViewById(R.id.back);

		bill = (TextView) findViewById(R.id.bill);

		next = (Button) findViewById(R.id.next);

		un.setOnClickListener(this);
		deux.setOnClickListener(this);
		trois.setOnClickListener(this);
		quattre.setOnClickListener(this);
		cinq.setOnClickListener(this);
		six.setOnClickListener(this);
		sept.setOnClickListener(this);
		huit.setOnClickListener(this);
		neuf.setOnClickListener(this);
		zero.setOnClickListener(this);
		back.setOnClickListener(this);
		next.setOnClickListener(this);

		bill.setText(montant);
		montant = "";
	}

	@Override
	public void onClick(View v) {

		if (montant.length() <= 5) {
			if (v == un) {
				montant += "1";
			}
			if (v == deux) {
				montant += "2";
			}
			if (v == trois) {
				montant += "3";
			}
			if (v == quattre) {
				montant += "4";
			}
			if (v == cinq) {
				montant += "5";
			}
			if (v == six) {
				montant += "6";
			}
			if (v == sept) {
				montant += "7";
			}
			if (v == huit) {
				montant += "8";
			}
			if (v == neuf) {
				montant += "9";
			}
			if (v == zero) {
				if (montant.length() != 0)
					if (montant.charAt(0) != '0')
						montant += "0";
			}
		}
		if (v == back && montant.length() > 0) {
			montant = montant.substring(0, montant.length() - 1);
		}
		update(montant);
		if (v == next) {
			Intent intent = new Intent(BillActivity.this, EqualActivity.class);
			intent.putExtra("count", count);
			intent.putExtra("bill", price);
			startActivity(intent);
			finish();
		}

	}

	private void update(String montant2) {

		String fbill = "";

		switch (montant2.length()) {
		case 0:
			fbill = "0.000";
			break;
		case 1:
			fbill = montant2 + ".000";
			break;
		case 2:
			fbill = montant2 + ".000";
			break;
		case 3:
			fbill = montant2 + ".000";
			break;
		case 4:
			fbill = montant2.substring(0, 3) + "." + montant2.substring(3, 4)
					+ "00";
			break;
		case 5:
			fbill = montant2.substring(0, 3) + "." + montant2.substring(3, 5)
					+ "0";
			break;
		case 6:
			fbill = montant2.substring(0, 3) + "." + montant2.substring(3, 6);
			break;
		default:
			fbill = montant2;
			break;
		}
		price = fbill;
		// price = new BigDecimal(fbill);
		bill.setText(fbill);

	}

}
