/// <summary>
 /// find location number with SKU wich is closest to exit
 /// </summary>
 /// <param name="_sku">sku to find</param>
 /// <param name="locations">locations to find in</param>
 /// <returns>location number with closest SKU</returns>
 private Int32 findClosestSKU(Sku _sku, List<Locazione> locations)
 {
     Int32 minPos = Int32.MaxValue;
     Int32 minLocation = -1;
     Int32 currentPos = 0;
     for(Int32 locationNumber = 0; locationNumber < locations.Count; locationNumber++)
     {
         for (Int32 positionNumber = locations[locationNumber].elementi.Count - 1; positionNumber >= 0 ; positionNumber--)
         {
             if (locations[locationNumber].elementi[positionNumber].sku == _sku.sku)
             {
                 currentPos = locations[locationNumber].elementi.Count - positionNumber;
                 if (currentPos < minPos)
                 {
                     currentPos = minPos;
                     minLocation = locationNumber;
                     break;
                 }
             }
         }
     }
     return minLocation;
 }
 /// <summary>
 /// Pushes SKU element to location stack
 /// </summary>
 /// <param name="_sku">SKU to push</param>
 public void pushSku(Sku _sku)
 {
     elementi.Add(_sku);
 }
        //Lettura della giacenza
        private void readGiacenza()
        {
            try
            {
                IDbConnection _conn = new SQLiteConnection(_connString);
                _conn.Open();//apre la connessione
                IDbCommand _com = _conn.CreateCommand();
                string queryText = "select LOCAZIONE,POSIZIONE,sku from giacenza4I";
                _com.CommandText = queryText;//proprietà del commandtext che si aspetta una stringa
                IDataReader reader = _com.ExecuteReader();//mi permette di leggere i risultati -> risultato dell'esecuzione del comando -> executereader serve per le select
                while (reader.Read())//leggo 1 per 1 i record -> reader mi permette di vedere un record alla volta
                {
                    Console.WriteLine(reader["LOCAZIONE"] + " " + reader["POSIZIONE"] + " " + reader["sku"] + " \n");
                    var locazione = reader["LOCAZIONE"].ToString();
                    var posizione = Convert.ToInt32(reader["POSIZIONE"]);
                    var sku = Convert.ToInt32(reader["sku"]);

                    foreach (var sel in _locazioni)
                    {
                        //Seleziono la locazione
                        if (sel.nome == locazione)
                        {
                            Sku s = null;
                            Boolean trovato = false;
                            //Ricerco se sku è già in sequenza di pickup perchè ha caricata la priorità
                            foreach (var sk in _pickupSeq)
                            {
                                if (sk.sku == sku)
                                {
                                    s = sk;
                                    trovato = true;
                                    break;
                                }
                                //Altrimenti lo carico assegnandoli una priorità max
                            }
                            if (!trovato)
                                s = new Sku() { sku = sku, priorità = _maxPriority, posizione = posizione };

                            sel.elementi.Add(s);

                        }
                    }

                }
                reader.Close();
                _conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("[readTable] Errore: " + ex.Message + Environment.NewLine);
            }

            foreach (var sel in _locazioni)
                checkLocazione(sel);
        }
 //Lettura della sequenza di Pickup
 private void readPickupSeq()
 {
     try
     {
         IDbConnection _conn = new SQLiteConnection(_connString);
         _conn.Open();//apre la connessione
         IDbCommand _com = _conn.CreateCommand();
         string queryText = "select Ordini from pickupSeq";
         _com.CommandText = queryText;//proprietà del commandtext che si aspetta una stringa
         IDataReader reader = _com.ExecuteReader();//mi permette di leggere i risultati -> risultato dell'esecuzione del comando -> executereader serve per le select
         Int32 count = 1;
         while (reader.Read())//leggo 1 per 1 i record -> reader mi permette di vedere un record alla volta
         {
             Console.WriteLine(reader["Ordini"] + " \n");
             Sku c = new Sku() { sku = Convert.ToInt32(reader["Ordini"]), priorità = count };
             _pickupSeq.Add(c);
             count++;
         }
         reader.Close();
         _conn.Close();
     }
     catch (Exception ex)
     {
         Console.WriteLine("[readTable] Errore: " + ex.Message + Environment.NewLine);
     }
 }
 /// <summary>
 /// gets SKU from location by moving previous SKUs to nearby locations
 /// </summary>
 /// <param name="_sku">SKU to find</param>
 /// <param name="locations">locations</param>
 /// <param name="locationNumber">location number to get SKU from</param>
 /// <returns>Number of steps needed to get SKU</returns>
 private Int32 getSKU(Sku _sku, List<Locazione> locations, Int32 locationNumber)
 {
     Int32 _ret = 0;
     Random myRandom = new Random();
     Sku skuGetted = locations[locationNumber].getSku();
     _ret++;
     while (skuGetted.sku != _sku.sku)
     {
         Int32 locationToPush = locationNumber + myRandom.Next(2) - 1; // move only to nearby locations
         locations[locationToPush].pushSku(skuGetted);
         skuGetted = locations[locationNumber].getSku();
         _ret++;
     }
     // here we assume that SKU is getted!
     return _ret;
 }