//main function to push list of items
        public void DoPush(Batch batch, List<DataItemPush> items)
        {
            Console.WriteLine("RemotePush.DoPush is started");
            int totalItems = items.Count;
            int count = 1;
            foreach (DataItemPush item in items)
            {
                if (DoPush(batch, item))
                {
                    DatabaseManager.Current.UpdateDataPushSent(item.PushId);
                }

                Console.WriteLine(count.ToString() + " of " + totalItems.ToString());
                count = count + 1;
            }
            Console.WriteLine("RemotePush.DoPush is finished");
        }
        //push a single item to remote server
        private bool DoPush(Batch batch, DataItemPush item)
        {
            try
            {
                string result = Post(_url_push, new NameValueCollection()
                                {
                                    {"secret_code",   ConfigManager.Current.RemoteSecretKey},
                                    {"sku",           item.SKU.ToString()},
                                    {"product_name",  item.ProductName.ToString()},
                                    {"description",   item.Description.ToString()},
                                    {"url",           item.Url.ToString()},
                                    {"original_url",  item.OriginalUrl.ToString()},
                                    {"image_url",     item.ImageUrl.ToString()},
                                    {"price",         item.Price.ToString("#.##")},
                                    {"delivery_cost", item.DeliveryCost.ToString()},
                                    {"currency_code", item.CurrencyCode.ToString()},
                                    {"brand",         item.Brand.ToString()},
                                    {"colour",        item.Colour.ToString()},
                                    {"gender",        item.Gender.ToString()},
                                    {"size",          item.Size.ToString()},
                                    {"mid",           batch.Merchant.MerchantID.ToString()}
                                });

                if (result == "OK"){
                    return true;
                }
                else{
                    int trimLength = 100;
                    if (result.Length < 100) { trimLength = result.Length; }
                    Console.WriteLine("RemotePush.DoPush seems good, SKU: " + item.SKU + ", but remote server return: " + result.Substring(0, trimLength));        //truncate to up to 100 characters error
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("RemotePush.DoPush has failed: " + ex.ToString());
                return false;
            }
        }
        /*
         * the is specific procedure for CommissionFactory(now used only for The Iconic)
         *  - it will either add or update the feed item to local database
         */
        private void CommissionFactoryAddFeedItem(Batch batch, DataItemRaw item)
        {
            int batchId = batch.BatchId;
            int MID     = batch.Merchant.MerchantID;

            SqlCommand cmd = new SqlCommand("ProcessCFRawData", sqlConn);
            cmd.CommandType = CommandType.StoredProcedure;

            try
            {
                //passing parameter
                cmd.Parameters.Add(new SqlParameter("@BatchId", batchId));
                cmd.Parameters.Add(new SqlParameter("@MID", MID));
                cmd.Parameters.Add(new SqlParameter("@SKU", MID.ToString() + "-" + item.SKU.Trim()));
                cmd.Parameters.Add(new SqlParameter("@Name", item.Name));
                cmd.Parameters.Add(new SqlParameter("@Category", item.Category));
                cmd.Parameters.Add(new SqlParameter("@Description", item.Description));
                cmd.Parameters.Add(new SqlParameter("@Url", item.Url));
                cmd.Parameters.Add(new SqlParameter("@OriginalUrl", item.OriginalUrl));
                cmd.Parameters.Add(new SqlParameter("@ImageUrl", item.ImageUrl));
                cmd.Parameters.Add(new SqlParameter("@Price", item.Price));
                cmd.Parameters.Add(new SqlParameter("@DeliveryCost", RemNull(item.DeliveryCost)));
                cmd.Parameters.Add(new SqlParameter("@CurrencyCode", RemNull(item.Currency)));
                cmd.Parameters.Add(new SqlParameter("@Brand", item.Brand));
                cmd.Parameters.Add(new SqlParameter("@Colour", RemNull(item.Colour)));
                cmd.Parameters.Add(new SqlParameter("@Gender", RemNull(item.Gender)));
                cmd.Parameters.Add(new SqlParameter("@Size", RemNull(item.Size)));

                //execute
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("DatabaseManager.CommissionFactoryAddFeedItem is failed at Item SKU: " + item.SKU + " - " + ex.ToString());
            }
        }
        /*
        public List<Merchant> GetMerchantList()
        {
            List<Merchant> result = new List<Merchant>();
            string sql = "select * from Merchant where IsActive = 1";
            SqlCommand cmd = new SqlCommand(sql, sqlConn);
            cmd.CommandType = CommandType.Text;
            try
            {
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        Merchant merchant = new Merchant();
                        merchant.MerchantID = (int)reader["MID"];
                        merchant.Name = reader["Name"].ToString();
                        merchant.Description = reader["Description"].ToString();
                        merchant.UrlFeed = reader["URLFeed"].ToString();

                        merchant.FileStoreLocal = reader["FileStoreLocal"].ToString();
                        merchant.FileStoreLocal = merchant.FileStoreLocal.Replace("YYYYMMDD", DateTime.Now.ToString("yyyyMMdd"));

                        result.Add(merchant);
                    }
                    reader.Close();
                }
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine("DatabaseManager.GetMerchantList is failed - " + ex.ToString());
                return null;
            }
        }
        */
        public List<Batch> GetPendingBatch()
        {
            Console.WriteLine("DatabaseManager.GetPendingBatch - getting pending batch to run");
            List<Batch> result = new List<Batch>();
            string sql = "Select m.MID, m.Name, m.URLFeed, m.FileStoreLocal, b.* From FeedBatch b inner join Merchant m on b.MID = m.MID " +
                         "Where m.IsActive = 1 and b.DatePushed is NULL";
            SqlCommand cmd = new SqlCommand(sql, sqlConn);
            cmd.CommandType = CommandType.Text;
            try
            {
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        Batch batch = new Batch();

                        batch.BatchId = (int)reader["BatchId"];
                          Merchant merchant = new Merchant();
                          merchant.MerchantID = (int)reader["MID"];
                          merchant.Name = reader["Name"].ToString();
                          merchant.UrlFeed = reader["URLFeed"].ToString();
                          merchant.FileStoreLocal = reader["FileStoreLocal"].ToString();
                          merchant.FileStoreLocal = merchant.FileStoreLocal.Replace("YYYYMMDD", DateTime.Now.ToString("yyyyMMdd"));
                        batch.Merchant = merchant;

                        //setup batch status depend on date stamp
                        if (reader.IsDBNull(reader.GetOrdinal("DateDownloaded")) &&
                            reader.IsDBNull(reader.GetOrdinal("DateProcessed")) &&
                            reader.IsDBNull(reader.GetOrdinal("DatePushed")))
                        {
                            batch.Status = BatchStatus.New;
                        }
                        else if (!reader.IsDBNull(reader.GetOrdinal("DateDownloaded")) &&
                                 reader.IsDBNull(reader.GetOrdinal("DateProcessed")) &&
                                 reader.IsDBNull(reader.GetOrdinal("DatePushed")))
                        {
                            batch.Status = BatchStatus.FileDownloaded;
                        }
                        else if (!reader.IsDBNull(reader.GetOrdinal("DateDownloaded")) &&
                                 !reader.IsDBNull(reader.GetOrdinal("DateProcessed")) &&
                                 reader.IsDBNull(reader.GetOrdinal("DatePushed")))
                        {
                            batch.Status = BatchStatus.FileProcessed;
                        }

                        result.Add(batch);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DatabaseManager.GetPendingBatch is failed - " + ex.ToString());
            }

            Console.WriteLine("DatabaseManager.GetPendingBatch - number of batch is: " + result.Count.ToString());
            return result;
        }
        public void CommissionFactoryAddFeedItem(Batch batch, List<DataItemRaw> itemsRaw)
        {
            int batchId = batch.BatchId;
            List<string> selectedBrand = GetSelectedBrandName(batch.Merchant.MerchantID);

            try
            {
                foreach (DataItemRaw itemRaw in itemsRaw)
                {
                    if (selectedBrand.Contains(itemRaw.Brand.Trim().ToLower()))      //do selected brand filter and compare lower case
                    {
                        DatabaseManager.Current.CommissionFactoryAddFeedItem(batch, itemRaw);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DatabaseManager.CommissionFactoryAddFeedItem failed - " + ex.ToString());
            }
        }