internal Response updateOrderState(Order order, params Input[] inputs)
        {
            Response response = new Response();
            Input state = inputs.Where(input => input.Name == "state").ElementAt(0);

            if (state == null || state.Value == "")
                response.Errors.Add(new Error("Order state can't be empty."));

            if (!order.isAllowableState(state.Value))
                response.Errors.Add(new Error("Order state " + state.Value + " isn't acceptable."));

            if (response.Errors.Count > 0)
                response.State = ResponseState.FAIL;
            else
            {
                order.State = state.Value;
                bool orderUpdated = order.update();

                if (orderUpdated)
                    response.State = ResponseState.SUCCESS;
                else
                {
                    response.Errors.Add(new Error("Unknown Error Happened While Updating Order State."));
                    response.State = ResponseState.FAIL;
                }
            }

            return response;
        }
 /// <summary>
 /// Login user into server using his information
 /// </summary>
 /// <param name="inputs">List of inputs : username, password</param>
 /// <returns>Result of runing the function</returns>
 public Response login(params Input[] inputs)
 {
     Response response = new Response();
     
     response.State = ResponseState.SUCCESS;
     return response;
 }
        public Response showOrders(OrdersListingPage orderListingPage)
        {
            Response response = new Response();

            _orderListingPage = orderListingPage;

            List<Order> ordersWaiting = Market.getInstance().Orders.FindAll((Order order) => order.State == Order.WAITING);
            _orderListingPage.changeLinks(ordersWaiting);
            Thread T = new Thread(Refresh);
            T.Start();

            response.State = ResponseState.SUCCESS;
            return response;
        }
        /// <summary>
        /// Create new product in the system
        /// </summary>
        /// <param name="inputs">List of inputs : name, barcode, price, categoryId, weight and description</param>
        /// <returns>Result of runing the function</returns>
        public Response createProduct(params Input[] inputs)
        {
            Response response = new Response();
            Input name, barcode, price, categoryId, weight, description;

            name = inputs.Where(input => input.Name == "name").ElementAt(0);
            barcode = inputs.Where(input => input.Name == "barcode").ElementAt(0);
            price = inputs.Where(input => input.Name == "price").ElementAt(0);
            categoryId = inputs.Where(input => input.Name == "categoryId").ElementAt(0);
            weight = inputs.Where(input => input.Name == "weight").ElementAt(0);
            description = inputs.Where(input => input.Name == "description").ElementAt(0);

            if (name.Value == "")
                response.Errors.Add(new Error("Product Name can't be empty."));
            if (barcode.Value == "")
                response.Errors.Add(new Error("Product Barcode can't be empty."));
            if (price.Value == "")
                response.Errors.Add(new Error("Product Price can't be empty."));
            if (categoryId.Value == "")
                response.Errors.Add(new Error("Product Category can't be empty."));
            if (weight.Value == "")
                response.Errors.Add(new Error("Product Weight can't be empty."));
            if (description.Value == "")
                response.Errors.Add(new Error("Product Description can't be empty."));


            if (response.Errors.Count > 0)
                response.State = ResponseState.FAIL;
            else
            {
                Product product = new Product(
                    name.Value, barcode.Value, float.Parse(price.Value), categoryId.Value, weight.Value, description.Value, Market.getInstance()
                );

                Product productSaved = Market.getInstance().addProduct(product);

                if (productSaved == null)
                {
                    response.Errors.Add(new Error("Unknown error happend while saving product, please try again later"));
                    response.State = ResponseState.FAIL;
                }
                else
                    response.State = ResponseState.SUCCESS;

            }

            return response;
        }
        public Response updateProduct(Product product,params Input[] inputs)
        {
            Response response = new Response();
            Input name, barcode, price;
            name = inputs.Where(input => input.Name == "name").ElementAt(0);
            barcode = inputs.Where(input => input.Name == "barcode").ElementAt(0);
            price = inputs.Where(input => input.Name == "price").ElementAt(0);
            if(name.Value=="")
            {
                response.Errors.Add(new Error("Please add the product name"));
            }
            if(barcode.Value=="")
            {
                response.Errors.Add(new Error("Please add the product barcode"));
            }
            if(price.Value=="")
            {
                response.Errors.Add(new Error("Please add the product price"));
            }
            if(response.Errors.Count>0)
            {
                response.State = ResponseState.FAIL;
            }
            else
            {
                product.Barcode = barcode.Value;
                product.Name = name.Value;
                product.Price = float.Parse(price.Value);

                bool check_productedit =  Market.getInstance().editProduct(product);  
                if(check_productedit==false)
                {
                    response.Errors.Add(new Error("Error happen while editing a product please check later"));
                    response.State = ResponseState.FAIL;
                }
                else
                {
                    response.State = ResponseState.SUCCESS;
                }
            }
            return response;
        }
        /// <summary>
        /// Create the controller offer constructed in the system
        /// </summary>
        /// <param name="inputs">List of inputs : name, price and teaser</param>
        /// <returns>Result of runing the function</returns>
        public Response createOffer(params Input[] inputs)
        {
            Response respone = new Response();

            Input name, price, teaser;

            name = inputs.Where(input => input.Name == "name").ElementAt(0);
            price = inputs.Where(input => input.Name == "price").ElementAt(0);
            teaser = inputs.Where(input => input.Name == "teaser").ElementAt(0);

            if (name == null || name.Value == "")
                respone.Errors.Add(new Error("Offer name can't be empty."));
            if (price == null || price.Value == "")
                respone.Errors.Add(new Error("Offer price can't be empty."));
            if(teaser == null || teaser.Value == "")
                respone.Errors.Add(new Error("Offer teaser can't be empty."));

            if (respone.Errors.Count > 0)
                respone.State = ResponseState.FAIL;
            else
            {
                _offer.Name = name.Value;
                _offer.Price = (price.Value);
                _offer.Teaser = teaser.Value;

                bool offerSaved = Market.getInstance().addOffer(_offer);

                if (!offerSaved)
                {
                    respone.Errors.Add(new Error("Unknown error happend while saving Offer, please try again later."));
                    respone.State = ResponseState.FAIL;
                }
                else
                {
                    respone.State = ResponseState.SUCCESS;
                }
            }

            return respone;
        }
        public Response openOrder(params Input[] inputs)
        {
            Response response = new Response();

            Input id = inputs.Where(input => input.Name == "id").ElementAt(0);
            Input confirmationCode = inputs.Where(input => input.Name == "confirmationCode").ElementAt(0);

            if (id == null || id.Value == "")
                response.Errors.Add(new Error("Order id can't be empty."));

            if (confirmationCode == null || confirmationCode.Value == "")
                response.Errors.Add(new Error("Confirmation Code can't be empty."));

            if (response.Errors.Count > 0)
                response.State = ResponseState.FAIL;
            else
            {
                //search order in the market
                Order order = Market.getInstance().Orders.Find((Order o) => (o.Id == id.Value && o.Confirmation_code == confirmationCode.Value));

                if (order != null)
                {
                    response.State = ResponseState.SUCCESS;
                    OrderDetailsWindow orderPopup = new OrderDetailsWindow(order);
                    orderPopup.Show();
                }
                else
                {
                    response.Errors.Add(new Error("Wrong Order ID/Confirmation Code Combination."));
                    response.State = ResponseState.FAIL;
                }
            }


            return response;
        }
        /// <summary>
        /// Add new product with it's quantity to controller of the offer
        /// </summary>
        /// <param name="inputs">List of inputs : productID and productQuantity</param>
        /// <returns>Result of runing the function</returns>
        public Response addProductToOffer(params Input[] inputs)
        {
            Response respone = new Response();
            Input productId, productQuantity;

            productId = inputs.Where(input => input.Name == "productId").ElementAt(0);
            productQuantity = inputs.Where(input => input.Name == "productQuantity").ElementAt(0);

            if(productId == null || productId.Value == "")
                respone.Errors.Add(new Error("Product ID can't be empty."));
            if(productQuantity == null || productQuantity.Value == "")
                respone.Errors.Add(new Error("Product quantity can't be empty."));
            
            if (respone.Errors.Count > 0)
                respone.State = ResponseState.FAIL;
            else
            {

                Product product = new Product(Market.getInstance());

                product.Id = productId.Value;

                int productQuantityInt;
                if (int.TryParse(productQuantity.Value, out productQuantityInt) == true)
                    product.Quantity = productQuantityInt;
                else
                {
                    respone.Errors.Add(new Error("Quantity do not represent integer value."));
                    respone.State = ResponseState.FAIL;
                }

                _offer.Products.Add(product);
            }

            return respone;
        }
        public Response showOffers(ShowOffers showOffersPage)
        {
            Response response = new Response();

            showOffersPage.show(Market.getInstance().Offers);

            response.State = ResponseState.SUCCESS;
            return response;
        }
        /// <summary>
        /// Delete the offer currently held by the offer controller
        /// </summary>
        /// <returns>Result of runing the function</returns>
        public Response deleteOffer()
        {
            Response respone = new Response();
            if (_offer == null)
            {
                respone.Errors.Add(new Error("Offer does not exist"));
                respone.State = ResponseState.FAIL;
            }

            if (Market.getInstance().deleteOffer(_offer))
                respone.State = ResponseState.SUCCESS;
            else
            {
                respone.Errors.Add(new Error("Unknown error happend while Deleting offer, please try again later"));
                respone.State = ResponseState.FAIL;
            }
            return respone;
        }
        public Response showProducts(ShowProducts showProducts)
        {
            Response response = new Response();

            showProducts.show(Market.getInstance().Products);

            response.State = ResponseState.SUCCESS;
            return response;
        }
 public Response deleteProduct(Product product)
 {
     Response response = new Response();
     if(product!=null)
     {
      bool check_productdeleted =  Market.getInstance().deleteProduct(product);
      if(check_productdeleted==true)
      {
          response.State = ResponseState.SUCCESS;
      }
      else
      {
          response.Errors.Add(new Error("Error Happen in the server please try later on"));
          response.State = ResponseState.FAIL; 
      }
     }
     else
     {
         response.Errors.Add(new Error("There Is No Product Selected To Delete"));
         response.State = ResponseState.FAIL;
     }
     return response;
 }