///<summary>
    /// Loads the inventory and historyDatabases and populates the relevant binary search trees.
    ///</summary>
    private void LoadDatabases()
    {
        List <InventoryItemInstance> itemData;
        List <string> itemDataIDs = new List <string>();

        SavedDatabaseHandler.LoadDatabase <InventoryItemInstance>(out itemData, true);

        for (int i = 0; i < itemData.Count; i++)
        {
            itemDataIDs.Add(itemData[i].Item.Name.ToLower());
        }

        inventory = new BinaryST <InventoryItemInstance>(itemData, itemDataIDs);


        List <Order>  orderData;
        List <string> orderDataIDs = new List <string>();

        SavedDatabaseHandler.LoadDatabase <Order>(out orderData, true);

        for (int i = 0; i < orderData.Count; i++)
        {
            orderDataIDs.Add(orderData[i].ClientName.ToLower());
        }

        orderHistory = new BinaryST <Order>(orderData, orderDataIDs);

        SavedDatabaseHandler.LoadDatabase <Order>(out outgoingOrders, false);
    }
    ///<summary>
    /// Validates the stock modification and saves the modified item. Returns false on invalid entry for price or quantity.
    ///</summary>
    internal bool ConfirmStockEdit(string priceString, string ammountString, int discount)
    {
        float price = 0f;

        float.TryParse(priceString, out price);
        int ammount = -1;

        int.TryParse(ammountString, out ammount);

        if (price < 0 || ammount < 0)
        {
            return(false);
        }

        InventoryItemInstance item = inventory.GetNodeAt(changeAmmountTargetItem.Item.ID).NodeValue;

        item.Item.BasePrice = price;
        item.Quantity       = ammount;
        item.discount       = discount;

        //save the database
        SavedDatabaseHandler.SaveDatabase <InventoryItemInstance>(inventory.ToOrderedList(), true);
        SearchInventory(null);
        return(true);
    }
    ///<summary>
    /// Adds the finalized outgoing orders to history and deletes the sheet objects creared to show them.
    // Also triggeres save request for both stock values and order history.
    ///</summary>
    internal void ConfirmPlaceOutgoingOrders()
    {
        foreach (Order order in outgoingOrders)
        {
            orderHistory.Add(new Node <Order>(order, order.ClientName.ToLower()));
        }

        //add outgoing to history
        //clear the outgoing list
        outgoingOrders.Clear();
        view.UpdateOrderObjects(
            false,
            outgoingOrdersParent,
            outgoingOrders,
            spawnedOutgoingOrderObjects,
            true,
            false,
            OrderRemoveAction,
            ShowOrderItems
            );

        //destroy placed orders from placed panel
        foreach (OrderSheetObject sheet in spawnedOrderSheets)
        {
            Destroy(sheet.gameObject);
        }
        spawnedOrderSheets.Clear();

        //Save the updated inventory database and order history
        SavedDatabaseHandler.SaveDatabase <InventoryItemInstance>(inventory.ToOrderedList(), true);
        SavedDatabaseHandler.SaveDatabase <Order>(orderHistory.ToOrderedList(), true);
        SavedDatabaseHandler.SaveDatabase <Order>(outgoingOrders, false);
        SearchOrderHistory(null);
    }
    ///<summary>
    /// Returns item to add validity. On a succesfull validation the item will be added to the inventory, and the item database will be saved.
    ///</summary>
    internal bool ConfirmNewItemAdd(string name, string basePrice, string quantity, int discount)
    {
        if (inventory.ContainsId(name.ToLower()))
        {
            view.editPanel.OpenPanel(inventory.GetNodeAt(name.ToLower()).NodeValue);
            view.editPanel.DisplayErrorMessage(Constants.ERROR_ADD);
            return(false);
        }

        int quant = 0;

        int.TryParse(quantity, out quant);
        float price = 1.0f;

        float.TryParse(basePrice, out price);

        if (quant < 0 || price < 0)
        {
            view.editPanel.DisplayErrorMessage(Constants.ERROR_EDIT);
            return(false);
        }

        InventoryItem         itemBase     = new InventoryItem(name, price);
        InventoryItemInstance itemInstance = new InventoryItemInstance(quant, discount, itemBase);

        inventory.Add(new Node <InventoryItemInstance>(itemInstance, itemBase.ID));
        // view -> update inventory items
        SearchInventory(null);

        SavedDatabaseHandler.SaveDatabase <InventoryItemInstance>(inventory.ToOrderedList(), true);
        return(true);
    }
    ///<summary>
    /// Returns validity of the given <paramref name= "clientName"/> and if valid adds the curent order to the outgoing orders list.
    ///</summary>
    internal bool ConfirmFinalizeOrder(string clientName)
    {
        if (string.IsNullOrEmpty(clientName))
        {
            //return and toggle warning
            return(false);
        }

        if (outgoingOrders.Exists(order => order.ID == clientName.ToLower()))
        {
            Order existingOrder = outgoingOrders.Find(order => order.ID == clientName.ToLower());
            foreach (InventoryItemInstance item in itemsInCurentOrder)
            {
                //if an item in the curent order mathces one in the existing outgoing orders
                if (existingOrder.Items.Exists(orderItem => orderItem.Item.ID == item.Item.ID))
                {
                    //fow now the discount from the initial order is kept for new items
                    existingOrder.Items.Find(orderItem => orderItem.Item.ID == item.Item.ID).Quantity +=
                        item.Quantity;
                }
                //else whe add the item from the curent order to the existing order
                else
                {
                    existingOrder.Items.Add(new InventoryItemInstance(item));
                }
            }
            view.UpdateOrderObjects(
                true,
                outgoingOrdersParent,
                outgoingOrders,
                spawnedOutgoingOrderObjects,
                true,
                false,
                OrderRemoveAction,
                ShowOrderItems
                );
        }
        else
        {
            outgoingOrders.Add(new Order(clientName.ToLower(), clientName, new List <InventoryItemInstance>(itemsInCurentOrder)));
            view.UpdateOrderObjects(
                true,
                outgoingOrdersParent,
                outgoingOrders,
                spawnedOutgoingOrderObjects,
                true,
                false,
                OrderRemoveAction,
                ShowOrderItems
                );
        }

        //remove item quantities from stock
        foreach (InventoryItemInstance item in itemsInCurentOrder)
        {
            inventory.GetNodeAt(item.Item.ID).NodeValue.Quantity -= item.Quantity;
        }
        //update the inventory
        SearchInventory(null);

        //clear curent order
        itemsInCurentOrder.Clear();
        view.UpdateItemObjects(
            Constants.ItemInteraction.NoInteraction,
            currentOrderItemParent,
            itemsInCurentOrder,
            spawnedItemObjectsInOrder,
            true,
            ItemButtonAction,
            OpenStockEditPanel
            );

        SavedDatabaseHandler.SaveDatabase <InventoryItemInstance>(inventory.ToOrderedList(), true);
        SavedDatabaseHandler.SaveDatabase <Order>(outgoingOrders, false);
        return(true);
    }