using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Collections;

namespace GuestAdmin
{
    public partial class TabTableFoodsPage : Form
    {
        private BusinessLogic bLogic;
        private int tableID;

        private ArrayList tabDataList;
        private FoodOrderItem[] tableFoodOrderList;
        private FoodItemDetails[] foodItemList;

        const int BUTTON_WIDTH = 145;
        const int BUTTON_HEIGHT = 60;
        const int BUTTON_START_X = 50;
        const int BUTTON_START_Y = 40;
        const int BUTTONS_PER_ROW = 2;
        const int BUTTON_GAP_X = 60;
        const int BUTTON_GAP_Y = 20;

        public TabTableFoodsPage(BusinessLogic bLogic,int tableID)
        {
            this.bLogic = bLogic;
            this.tableID = tableID;

            tabDataList = new ArrayList();        

            InitializeComponent();

            try
            {
                foodItemList = bLogic.getAllOrganizedFoodItems();
                tableFoodOrderList = new FoodOrderItem[foodItemList.Length];

                int customerCount=bLogic.getCustomerCountOfFoodTable(tableID);
                if (customerCount == 0) // new table and new customer
                {
                    addCustomer(0); // add empty customer
                }
                else // table has food records
                {
                    for (int i = 0; i < customerCount; i++)
                    {
                        addCustomer(i);

                        // get ordered food items of this customer and mark buttons in here.....

                    }
                }

            }
            catch (Exception exc)
            {
                MessageBox.Show("Error occured while reading data from the system.\n\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        void addCustomer(int customerIndex)
        {
            if (customerIndex > 20)
                return;

            TabData tabData = new TabData();
            tabDataList.Add(tabData);

            generateTabPage(tabData, "Customer " + (char)((((int)'A') + customerIndex)));
            generateButtons(tabData);

            tabControl1.Controls.Add(tabData.tabPage);

            TableFoodOrder tableFoodOrder = new TableFoodOrder();
            tableFoodOrder.tableID = tableID;
            tableFoodOrder.customerID = customerIndex;

        }

        void generateTabPage(TabData tabData, String title)
        {
            tabData.tabPage = new System.Windows.Forms.TabPage();
            tabData.tabPage.Location = new System.Drawing.Point(0, 0);
            tabData.tabPage.Name = title;
            tabData.tabPage.Text = title;
            tabData.tabPage.UseVisualStyleBackColor = true;
            tabData.tabPage.AutoScroll = true;
        }

        void generateButtons(TabData tabData)
        {
            int foodCount = foodItemList.Length;

            tabData.buttons = new FoodButton[foodCount];

            for (int i = 0; i < foodCount; i++)
            {
                tabData.buttons[i] = new FoodButton();
                tabData.buttons[i].Location = new System.Drawing.Point(BUTTON_START_X + ((i % BUTTONS_PER_ROW) * (BUTTON_WIDTH + BUTTON_GAP_X)), BUTTON_START_Y + ((i / BUTTONS_PER_ROW) * (BUTTON_HEIGHT + BUTTON_GAP_Y)));

                tabData.buttons[i].foodName = foodItemList[i].foodName;
                tabData.buttons[i].menuID = foodItemList[i].menuID.ToString();
                tabData.buttons[i].orderCount = "0";
                tabData.buttons[i].isOrdered = false;

                tabData.tabPage.Controls.Add(tabData.buttons[i]);

                tabData.buttons[i].Click += new System.EventHandler(table_button_Click);
            }
           
        }

        private void table_button_Click(object sender, EventArgs e)
        {
            TabData tabData = (TabData)tabDataList[tabControl1.SelectedIndex];
            int buttonIndex = Array.FindIndex(tabData.buttons, b => b == sender); // get button index from object

            if(tableFoodOrderList[buttonIndex]==null) // no previous records, lets allocate food record!
            {
                FoodOrderItem foodOrderItem = new FoodOrderItem();
                foodOrderItem.foodID = foodItemList[buttonIndex].foodID;
                foodOrderItem.foodName = foodItemList[buttonIndex].foodName;
                foodOrderItem.count = 1;
                foodOrderItem.total = foodItemList[buttonIndex].price;

                tableFoodOrderList[buttonIndex] = foodOrderItem;
            }
            else // we have previous record, just increase order count
            {
                tableFoodOrderList[buttonIndex].count++;
                tableFoodOrderList[buttonIndex].total += foodItemList[buttonIndex].price;
            }

            // update order count & repaint button
            tabData.buttons[buttonIndex].orderCount = tableFoodOrderList[buttonIndex].count.ToString();
            tabData.buttons[buttonIndex].isOrdered = true; // make it yellow
            tabData.buttons[buttonIndex].Refresh();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            addCustomer(tabDataList.Count);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bLogic.releaseTableLock(tableID);
                TabEmuHostForm.setMainContent(new TabHomePage(bLogic));
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error occured while connecting to the system.\n\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}