package com.espritmobile.sim.restocheck;

import java.util.ArrayList;

import android.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.espritmobile.sim.restocheck.R;
import com.espritmobile.sim.restocheck.activities.BillActivity;
import com.espritmobile.sim.restocheck.activities.PersoActivity;
import com.espritmobile.sim.restocheck.entities.Person;
import com.espritmobile.sim.restocheck.fragments.BillFragment;
import com.espritmobile.sim.restocheck.fragments.TableFragment;
import com.espritmobile.sim.restocheck.utils.Utils;
import com.gc.materialdesign.views.ButtonRectangle;

public class CountFragment extends Fragment {
	ButtonRectangle plus, minus;
	ButtonRectangle next;

	TextView count;
	RadioGroup type;
	RadioButton equal;
	RadioButton perso;
	boolean mDualPane;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		View tabFrame = getActivity().findViewById(R.id.table);
		mDualPane = tabFrame != null
				&& tabFrame.getVisibility() == View.VISIBLE;

		Utils.count = 2;
		Utils.current = 0;
		Utils.totale = 0.0;
		Utils.price = "";

	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_count, container,
				false);
		plus = (ButtonRectangle) rootView.findViewById(R.id.plus);
		minus = (ButtonRectangle) rootView.findViewById(R.id.minus);
		count = (TextView) rootView.findViewById(R.id.count);
		next = (ButtonRectangle) rootView.findViewById(R.id.next);
		type = (RadioGroup) rootView.findViewById(R.id.type);
		equal = (RadioButton) rootView.findViewById(R.id.equal);
		perso = (RadioButton) rootView.findViewById(R.id.perso);

		plus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (Utils.count < 10) {

					Utils.count++;
					if (mDualPane) {
						FragmentManager fm = getActivity().getFragmentManager();
						TableFragment newF = new TableFragment();
						Log.i("TAG", Utils.count + "");
						FragmentTransaction ft = fm.beginTransaction();
						ft.replace(R.id.table, newF);
						ft.addToBackStack(null);
						ft.commit();
					}

				}
				count.setText(Utils.count + "");
			}
		});
		minus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Utils.count > 2) {
					Utils.count--;

					if (mDualPane) {
						FragmentManager fm = getActivity().getFragmentManager();
						TableFragment newF = new TableFragment();
						FragmentTransaction ft = fm.beginTransaction();
						ft.replace(R.id.table, newF);
						ft.addToBackStack(null);
						ft.commit();
					}
				}
				count.setText(Utils.count + "");
			}
		});
		count.setText(Utils.count + "");
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mDualPane) {
					FragmentManager fm = getActivity().getFragmentManager();
					BillFragment newF = new BillFragment();
					FragmentTransaction ft = fm.beginTransaction();
					ft.replace(R.id.menu, newF);
					ft.addToBackStack(null);
					ft.commit();
				} else {
					if (equal.isChecked()) {
						Intent i = new Intent(getActivity(), BillActivity.class);
						getActivity().startActivity(i);
						/*
						 * getActivity().overridePendingTransition(
						 * R.anim.slide_in_right, R.anim.slide_out_left);
						 */
					} else {
						Utils.persons = new ArrayList<Person>();
						Intent i = new Intent(getActivity(),
								PersoActivity.class);
						getActivity().startActivity(i);
						/*
						 * getActivity().overridePendingTransition(
						 * R.anim.slide_in_right, R.anim.slide_out_left);
						 */
					}

				}
			}
		});

		return rootView;
	}

}
