package com.espritmobile.sim.restocheck.fragments;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

import com.espritmobile.sim.restocheck.R;
import com.espritmobile.sim.restocheck.entities.Plate;
import com.espritmobile.sim.restocheck.utils.Utils;

public class TableFragment extends Fragment {
	LinearLayout top, right, left, bottom;
	Plate plate_top, plate_top2, plate_top3, plate_left, plate_left2,
			plate_right, plate_right2, plate_bottom, plate_bottom2,
			plate_bottom3;
	Button next;
	boolean mDualPane;
	int count = 2;

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

		Toast.makeText(getActivity(), "mDualPane " + mDualPane,
				Toast.LENGTH_SHORT).show();
	}

	public TableFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_table, container,
				false);

		ActionBar actionBar = getActivity().getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		top = (LinearLayout) rootView.findViewById(R.id.top);
		left = (LinearLayout) rootView.findViewById(R.id.left);
		right = (LinearLayout) rootView.findViewById(R.id.right);
		bottom = (LinearLayout) rootView.findViewById(R.id.bottom);
		next = (Button) rootView.findViewById(R.id.next);
		next.setVisibility(View.INVISIBLE);
		/*
		 * next.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) {
		 * 
		 * Toast.makeText(getActivity(), "Count " + count,
		 * Toast.LENGTH_SHORT).show(); // startActivity(new
		 * Intent(getActivity(), // BillActivity.class).putExtra("count",
		 * count));
		 * 
		 * } });
		 */
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		switch (Utils.count) {
		case 2:
			plate_top = createButton(params, "top");
			top.addView(plate_top);
			plate_bottom = createButton(params, "bottom");
			bottom.addView(plate_bottom);
			break;
		case 3:
			plate_top = createButton(params, "top");
			top.addView(plate_top);
			plate_bottom = createButton(params, "bottom");
			bottom.addView(plate_bottom);
			plate_left = createButton(params, "left");
			left.addView(plate_left);

			break;
		case 4:
			plate_bottom = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_right = createButton(params, "right");
			top.addView(plate_top);
			bottom.addView(plate_bottom);
			left.addView(plate_left);
			right.addView(plate_right);
			break;
		case 5:
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_right = createButton(params, "right");
			top.addView(plate_top);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			left.addView(plate_left);
			right.addView(plate_right);
			break;
		case 6:
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_top2 = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_right = createButton(params, "right");
			top.addView(plate_top);
			top.addView(plate_top2);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			left.addView(plate_left);
			right.addView(plate_right);
			break;
		case 7:
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_top2 = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_left2 = createButton(params, "left");
			plate_right = createButton(params, "right");
			top.addView(plate_top);
			top.addView(plate_top2);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			left.addView(plate_left);
			left.addView(plate_left2);
			right.addView(plate_right);
			break;
		case 8:
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_top2 = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_left2 = createButton(params, "left");
			plate_right = createButton(params, "right");
			plate_right2 = createButton(params, "right");
			top.addView(plate_top);
			top.addView(plate_top2);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			left.addView(plate_left);
			left.addView(plate_left2);
			right.addView(plate_right);
			right.addView(plate_right2);
			break;
		case 9:
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_bottom3 = createButton(params, "bottom");
			plate_top = createButton(params, "top");
			plate_top2 = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_left2 = createButton(params, "left");
			plate_right = createButton(params, "right");
			plate_right2 = createButton(params, "right");
			top.addView(plate_top);
			top.addView(plate_top2);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			bottom.addView(plate_bottom3);
			left.addView(plate_left);
			left.addView(plate_left2);
			right.addView(plate_right);
			right.addView(plate_right2);
			break;
		case 10:
			plate_top = createButton(params, "top");
			plate_top2 = createButton(params, "top");
			plate_top3 = createButton(params, "top");
			plate_left = createButton(params, "left");
			plate_left2 = createButton(params, "left");
			plate_right = createButton(params, "right");
			plate_right2 = createButton(params, "right");
			plate_bottom = createButton(params, "bottom");
			plate_bottom2 = createButton(params, "bottom");
			plate_bottom3 = createButton(params, "bottom");
			top.addView(plate_top);
			top.addView(plate_top2);
			top.addView(plate_top3);
			left.addView(plate_left);
			left.addView(plate_left2);
			right.addView(plate_right);
			right.addView(plate_right2);
			bottom.addView(plate_bottom);
			bottom.addView(plate_bottom2);
			bottom.addView(plate_bottom3);
			break;

		default:
			plate_top = createButton(params, "top");
			top.addView(plate_top);
			plate_bottom = createButton(params, "bottom");
			bottom.addView(plate_bottom);
			break;
		}
		return rootView;
	}

	public Plate createButton(android.widget.RelativeLayout.LayoutParams lp,
			String pos) {
	
		switch (pos) {
		case "top":
			Plate top = new Plate(getActivity(), R.drawable.plate_top, "0 DT",
					lp);
			return top;
		case "left":
			Plate left = new Plate(getActivity(), R.drawable.plate_left,
					"0 DT", new LayoutParams(LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
			return left;
		case "right":
			Plate right = new Plate(getActivity(), R.drawable.plate_right,
					"0 DT", new LayoutParams(LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
			return right;
		case "bottom":
			Plate bot = new Plate(getActivity(), R.drawable.plate_bottom,
					"0 DT", lp);
			return bot;
		default:
			return null;
		}
	}
}
