package com.espritmobile.sim.restocheck.activities;

import java.math.BigDecimal;

import com.espritmobile.sim.restocheck.MainActivity;
import com.espritmobile.sim.restocheck.R;
import com.espritmobile.sim.restocheck.entities.Person;
import com.espritmobile.sim.restocheck.utils.Utils;
import com.gc.materialdesign.views.ButtonRectangle;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PersoResultActivity extends Activity implements OnClickListener {

	Button next;
	TextView done;
	BigDecimal bill;
	Button plate_one, plate_two, plate_three, plate_four, plate_five,
			plate_six, plate_seven, plate_eight, plate_nine, plate_ten;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final String price = Utils.price;
		bill = new BigDecimal(price);
		switch (Utils.count) {
		case 2:
			setContentView(R.layout.activity_equal_two);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_one.setOnClickListener(this);
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_two.setOnClickListener(this);
			break;
		case 3:
			setContentView(R.layout.activity_equal_three);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);

			break;
		case 4:
			setContentView(R.layout.activity_equal_four);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			break;
		case 5:
			setContentView(R.layout.activity_equal_five);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			break;
		case 6:
			setContentView(R.layout.activity_equal_six);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());
			plate_six = (Button) findViewById(R.id.plate_six);
			plate_six.setText(Utils.persons.get(5).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			plate_six.setOnClickListener(this);
			break;
		case 7:
			setContentView(R.layout.activity_equal_seven);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());
			plate_six = (Button) findViewById(R.id.plate_six);
			plate_six.setText(Utils.persons.get(5).getName());
			plate_seven = (Button) findViewById(R.id.plate_seven);
			plate_seven.setText(Utils.persons.get(6).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			plate_six.setOnClickListener(this);
			plate_seven.setOnClickListener(this);
			break;
		case 8:
			setContentView(R.layout.activity_equal_eight);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());
			plate_six = (Button) findViewById(R.id.plate_six);
			plate_six.setText(Utils.persons.get(5).getName());
			plate_seven = (Button) findViewById(R.id.plate_seven);
			plate_seven.setText(Utils.persons.get(6).getName());
			plate_eight = (Button) findViewById(R.id.plate_eight);
			plate_eight.setText(Utils.persons.get(7).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			plate_six.setOnClickListener(this);
			plate_seven.setOnClickListener(this);
			plate_eight.setOnClickListener(this);
			break;
		case 9:
			setContentView(R.layout.activity_equal_nine);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());
			plate_six = (Button) findViewById(R.id.plate_six);
			plate_six.setText(Utils.persons.get(5).getName());
			plate_seven = (Button) findViewById(R.id.plate_seven);
			plate_seven.setText(Utils.persons.get(6).getName());
			plate_eight = (Button) findViewById(R.id.plate_eight);
			plate_eight.setText(Utils.persons.get(7).getName());
			plate_nine = (Button) findViewById(R.id.plate_nine);
			plate_nine.setText(Utils.persons.get(8).getName());

			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			plate_six.setOnClickListener(this);
			plate_seven.setOnClickListener(this);
			plate_eight.setOnClickListener(this);
			plate_nine.setOnClickListener(this);
			break;
		case 10:
			setContentView(R.layout.activity_equal_ten);
			plate_one = (Button) findViewById(R.id.plate_one);
			plate_one.setText(Utils.persons.get(0).getName());
			plate_two = (Button) findViewById(R.id.plate_two);
			plate_two.setText(Utils.persons.get(1).getName());
			plate_three = (Button) findViewById(R.id.plate_three);
			plate_three.setText(Utils.persons.get(2).getName());
			plate_four = (Button) findViewById(R.id.plate_four);
			plate_four.setText(Utils.persons.get(3).getName());
			plate_five = (Button) findViewById(R.id.plate_five);
			plate_five.setText(Utils.persons.get(4).getName());
			plate_six = (Button) findViewById(R.id.plate_six);
			plate_six.setText(Utils.persons.get(5).getName());
			plate_seven = (Button) findViewById(R.id.plate_seven);
			plate_seven.setText(Utils.persons.get(6).getName());
			plate_eight = (Button) findViewById(R.id.plate_eight);
			plate_eight.setText(Utils.persons.get(7).getName());
			plate_nine = (Button) findViewById(R.id.plate_nine);
			plate_nine.setText(Utils.persons.get(8).getName());
			plate_ten = (Button) findViewById(R.id.plate_ten);
			plate_ten.setText(Utils.persons.get(9).getName());
			plate_one.setOnClickListener(this);
			plate_two.setOnClickListener(this);
			plate_three.setOnClickListener(this);
			plate_four.setOnClickListener(this);
			plate_five.setOnClickListener(this);
			plate_six.setOnClickListener(this);
			plate_seven.setOnClickListener(this);
			plate_eight.setOnClickListener(this);
			plate_nine.setOnClickListener(this);
			plate_ten.setOnClickListener(this);
			break;
		default:
			break;
		}
		done = (TextView) findViewById(R.id.done);
		done.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(PersoResultActivity.this,
						MainActivity.class));
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				finish();

			}
		});
		next = (Button) findViewById(R.id.next);
		next.setText(price + Utils.currency);
		next.setClickable(false);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == plate_one)
			afficherResultDialog(0);
		if (v == plate_two)
			afficherResultDialog(1);
		if (v == plate_three)
			afficherResultDialog(2);
		if (v == plate_four)
			afficherResultDialog(3);
		if (v == plate_five)
			afficherResultDialog(4);
		if (v == plate_six)
			afficherResultDialog(5);
		if (v == plate_seven)
			afficherResultDialog(6);
		if (v == plate_eight)
			afficherResultDialog(7);
		if (v == plate_nine)
			afficherResultDialog(8);
		if (v == plate_ten)
			afficherResultDialog(9);
	}

	private void afficherResultDialog(int i) {
		final Dialog result_dialog = new Dialog(this);
		result_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		result_dialog.setContentView(R.layout.result_dialog);

		Person p = Utils.persons.get(i);

		TextView name, rest, part, payed;
		ButtonRectangle back = (ButtonRectangle) result_dialog
				.findViewById(R.id.cancel);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				result_dialog.dismiss();

			}
		});
		name = (TextView) result_dialog.findViewById(R.id.name);
		part = (TextView) result_dialog.findViewById(R.id.part);
		payed = (TextView) result_dialog.findViewById(R.id.payed);
		rest = (TextView) result_dialog.findViewById(R.id.reste);

		name.setText(p.getName());
		part.setText(p.getPart() + " " + Utils.currency);
		payed.setText(p.getPayed() + " " + Utils.currency);
		rest.setText(p.getRest() + " " + Utils.currency);

		result_dialog.setCanceledOnTouchOutside(true);
		result_dialog.show();

	}

}