public ObjectMakeCDResponse setOrder(OrderInfo order)
 {
     var request = new ObjectCDRequest();
     request.WSCallback = "xxxxxxx";
     request.DeliveryAdress = order.morada;
     request.Distance = order.distance;
     request.encomendaID = order.encomendaid;
     request.fabrica = "fabrica a";
     request.userid = Convert.ToInt32(order.userID);
     var arrayOfMusic = new Music[order.orderedTracks.Count];
     var x = 0;
     foreach (var t in order.orderedTracks)
     {
         var m = new Music();
         m.nome = t.TrackName;
         m.price = t.Price;
         m.duracao = getMusicDuration(m.nome);
         arrayOfMusic[x] = m;
         x++;
     }
     request.ListaMusicas = arrayOfMusic;
     
     AFabricanteC.MakeCd(request);
     return new ObjectMakeCDResponse();
 }
        public ObjectMakeCDResponse setOrder(OrderInfo order)
        {
            var request = new ObjectCDRequest {WSCallback = "xxxxxxx"};
            request.WSCallback = "xxxxxxx";
            request.DeliveryAdress = order.morada;
            request.Distance = order.distance;
            //request.Distance = requestA.Distance;
            request.encomendaID = order.encomendaid;
            request.fabrica = "fabrica b";
            request.userid = Convert.ToInt32(order.userID);
            var arrayOfMusic = new Music[order.orderedTracks.Count];
            var x = 0;

            foreach (var m in order.orderedTracks.Select(t => new Music
            {
                TrackName = t.TrackName,
                Price = t.Price,
                ArtisName = t.ArtisName,
                PriceFormatted = t.PriceFormatted
            }))
            {
                arrayOfMusic[x] = m;
                x++;
            }

            request.ListaMusicas = arrayOfMusic;
            AFabricanteB.MakeCD(request);

            return new ObjectMakeCDResponse();
        }
 public string GetTransporterPrice(TransportJobPriceResponse response)
 {
     lock (_sync)
     {
         OrderInfo order = null;
         System.Console.WriteLine(response.fabricante + " " + response.Price);
         if (orderList.Count > 0)
         {
             foreach (var item in orderList)
             {
                 if (item.encomendaid == response.encomendaID)
                 {
                     order = item;
                     break;
                 }
             }
         }
         if (order == null)
         {
             order = new OrderInfo(response.userID, response.encomendaID);
             order.addpriceTransp(response.fabricante, response.Price);
         }
         else
         {
             order.addpriceTransp(response.fabricante, response.Price);
         }
     }
     return "ack";
 }
        public FabricantePriceResponse getPrice(OrderInfo order)
        {
            var request = new ObjectQuoteRequest
            {
                encomendaID = order.encomendaid,
                fabricante = "fabrica b",
                userID = order.userID,
                WSCallback = "qwerty"
            };
            var x = 0;
            var arrayOfMusic = new Music[order.orderedTracks.Count];
            foreach (var t in order.orderedTracks)
            {
                var m = new Music
                {
                    TrackName = t.TrackName,
                    Price = t.Price,
                    ArtisName = t.ArtisName,
                    PriceFormatted = t.PriceFormatted
                };
                arrayOfMusic[x] = m;
                x++;
            }
            request.ListaMusicas = arrayOfMusic;

            AFabricanteB.getQuote(request);
            return new FabricantePriceResponse();
        }
        public ObjectMakeCDResponse setOrder(OrderInfo order)
        {
            var request = new ObjectCDRequest
            {
                WSCallback = "xxxxxxx",
                DeliveryAdress = order.morada,
                Distance = order.distance,
                encomendaID = order.encomendaid,
                fabrica = "fabrica a",
                userid = Convert.ToInt32(order.userID)
            };
            var arrayOfMusic = new Music[order.orderedTracks.Count];
            var x = 0;

            foreach (var m in order.orderedTracks.Select(t => new Music {nome = t.TrackName, price = t.Price}))
            {
                m.duracao = getMusicDuration(m.nome);
                arrayOfMusic[x] = m;
                x++;
            }

            request.ListaMusicas = arrayOfMusic;

            AFabricanteC.MakeCd(request);
            return new ObjectMakeCDResponse();
        }
        public string GetFabricantePrice(FabricantePriceResponse response)
        {
            lock (_sync)
            {
                var order = orderList.FirstOrDefault(item => item.encomendaid == response.encomendaID);

                if (order == null)
                {
                    order = new OrderInfo(response.userID, response.encomendaID);
                    order.addpriceFabric(response.fabricante, response.Price);
                }
                else
                {
                    order.addpriceFabric(response.fabricante, response.Price);
                }
            }
            return "ack";
        }
        public FabricantePriceResponse getPrice(OrderInfo order)
        {
            var request = new ObjectQuoteRequest();
            request.encomendaID=order.encomendaid;
            request.fabricante = "fabrica a";
            request.userID = order.userID;
            request.WSCallback = "qwerty";
            var x = 0;
            var arrayOfMusic = new Music[order.orderedTracks.Count];
            foreach (var t in order.orderedTracks)
            {
                var m = new Music();
                m.nome = t.TrackName;
                m.price = t.Price;
                arrayOfMusic[x] = m;
                x++;
            }
            request.ListaMusicas = arrayOfMusic;

            AFabricanteA.getQuote(request);
            return new FabricantePriceResponse();
        }
        public FabricantePriceResponse getPrice(OrderInfo order)
        {
            var request = new ObjectQuoteRequest
            {
                encomendaID = order.encomendaid,
                fabricante = "fabrica a",
                userID = order.userID,
                WSCallback = "qwerty"
            };

            var x = 0;
            var arrayOfMusic = new Music[order.orderedTracks.Count];

            foreach (var m in order.orderedTracks.Select(t => new Music {nome = t.TrackName, price = t.Price}))
            {
                arrayOfMusic[x] = m;
                x++;
            }
            request.ListaMusicas = arrayOfMusic;

            AFabricanteA.getQuote(request);
            return new FabricantePriceResponse();
        }
        public OrderStatus RequestOrder(OrderInfo order)
        {
            var thread = new Thread(() =>
            {
                BackOfficeCallBackService.orderList.Add(order);

                //add to cdfactory
                var inboxMessage = new CDFactory.SongsByOrder
                {
                    OrderId = BackOfficeCallBackService.orderList.IndexOf(order).ToString(),
                    TrackList = new List<CDFactory.Track>()
                };

                foreach (var track in order.orderedTracks)
                {
                    var newTrack = new CDFactory.Track
                    {
                        ArtisName = track.ArtisName,
                        Price = track.Price,
                        PriceFormatted = track.PriceFormatted,
                        TrackName = track.TrackName
                    };

                    inboxMessage.TrackList.Add(newTrack);
                }

                MessageQueueHelper.SendMessage(InboxQueuePath, inboxMessage, "Order to Process");


                //get quotes from fabricantes
                IAdapterFabricantes adapterA = new AdapterFabricanteA(new FabricanteAServiceClient());
                adapterA.getPrice(order);
                IAdapterFabricantes adapterB = new AdapterFabricanteB(new FabricanteBServiceClient());
                adapterB.getPrice(order);
                IAdapterFabricantes adapterC = new AdapterFabricanteC(new FabricanteCServiceClient());
                adapterC.getPrice(order);

                //get quotes from transportadora
                TransportadoraServiceClient transp = new TransportadoraServiceClient();

                TransportJobPriceRequest requestA =new TransportJobPriceRequest();
                requestA.DeliveryAdress = order.morada;
                requestA.Distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaA);
                requestA.fabrica = "fabrica a";
                requestA.userID = order.userID;
                transp.TransportJobPrice(requestA);

                TransportJobPriceRequest requestB = new TransportJobPriceRequest();
                requestB.DeliveryAdress = order.morada;
                requestB.Distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaB);
                requestB.fabrica = "fabrica b";
                requestB.userID = order.userID;
                transp.TransportJobPrice(requestB);

                TransportJobPriceRequest requestC = new TransportJobPriceRequest();
                requestC.DeliveryAdress = order.morada;
                requestC.Distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaC);
                requestC.fabrica = "fabrica c";
                requestC.userID = order.userID;
                transp.TransportJobPrice(requestC);

                //select fabricante and tell transportadora qhere to get cd and where to deliver
                while (order.all3Received()==false)
                {
                    System.Console.WriteLine("Waiting for CallBacks to Select the best Manufacturer");
                    Thread.Sleep(1000);
                }

                //TransportJobRequest request = new TransportJobRequest();
                switch (order.getbestdeal())
                {
                    case("fabrica a"):
                        order.distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaA);
                        order.userID = UserDB.GetLoggedInUsers().First().UserId; // "1";
                        adapterA.setOrder(order);
                        //request.DeliveryAdress = order.morada;
                        //request.Distance= requestA.Distance;
                        //request.encomendaID = order.encomendaid;
                        //request.fabrica="fabrica a";
                        //request.userID = order.userID;
                        //transp.TransportJob(request);
                        break;
                    case ("fabrica b"):
                        order.distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaC);
                        order.userID = UserDB.GetLoggedInUsers().First().UserId;
                        adapterB.setOrder(order);
                        //request.DeliveryAdress = order.morada;
                        //request.Distance= requestA.Distance;
                        //request.encomendaID = order.encomendaid;
                        //request.fabrica="fabrica b";
                        //request.userID = order.userID;
                        //transp.TransportJob(request);
                        break;
                    case ("fabrica c"):
                        order.distance = GeocodingHelper.GetDistanceBetweenPlaces(order.morada, fabricaB);
                        order.userID = UserDB.GetLoggedInUsers().First().UserId;
                        adapterC.setOrder(order);
                        //request.DeliveryAdress = order.morada;
                        //request.Distance= requestA.Distance;
                        //request.encomendaID = order.encomendaid;
                        //request.fabrica="fabrica c";
                        //request.userID = order.userID;
                        //transp.TransportJob(request);
                        break;
                    default:
                        break;
                }
            });
            
            thread.Start();

            return new OrderStatus
            {
                status = "Encomenda Recebida"
            };
        }
 public string GetFabricantePrice(FabricantePriceResponse response)
 {
     lock (_sync)
     {
         OrderInfo order = null;
         foreach (var item in orderList)
         {
             if (item.encomendaid == response.encomendaID)
             {
                 order = item;
                 break;
             }
         }
         if (order == null)
         {
             order = new OrderInfo(response.userID, response.encomendaID);
             order.addpriceFabric(response.fabricante, response.Price);
         }
         else
         {
             order.addpriceFabric(response.fabricante, response.Price);
         }
     }
     return "ack";
 }
        private void btnConfirmOrder_Click(object sender, EventArgs e)
        {
            var request = new RestRequest("/RequestOrder/", Method.POST);

            var orderInfo = new OrderInfo
            {

                orderedTracks = ShoppingCartItems,
                morada = txtAddress.Text
            };

            var json = JsonConvert.SerializeObject(orderInfo);
            request.AddParameter("text/json", json, ParameterType.RequestBody);

            var response = Client.Execute(request);

            var res = JObject.Parse(response.Content);

            var sucesso = (string) res["status"];

            lbResultado.Text = sucesso;

        }