private void onViewed(SearchType ss, findItemsAdvancedResponse.eBayItem eb)
 {
     if (ItemViewed != null)
     {
         ItemViewed(ss, eb);
     }
 }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                findItemsAdvancedResponse.eBayItem eb = (findItemsAdvancedResponse.eBayItem)dataGridView1.SelectedRows[0].Tag;


                Process.Start(eb.ViewItemURL);
            }
        }
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                findItemsAdvancedResponse.eBayItem eb = (findItemsAdvancedResponse.eBayItem)dataGridView1.SelectedRows[0].Tag;
                eBayItems.SetAsViewed(thisSearch, eb);

                //eBayItems.SaveItems();

                onViewed(thisSearch, eb);

                // Process.Start(eb.ViewItemURL);
            }
        }
        //public delegate void d_NewItem(findItemsAdvancedResponse.eBayItem ei, SearchType ss);

        //public static event d_NewItem NewItem;

        private static void onNewItem(findItemsAdvancedResponse.eBayItem ei, SearchType ss)
        {
            //add to the NewItems for later

            if (NewItems == null)
            {
                NewItems = new Dictionary <string, int>();
            }

            if (!NewItems.ContainsKey(ss.SearchName))
            {
                NewItems.Add(ss.SearchName, 0);
            }


            NewItems[ss.SearchName]++;// = (NewItems[ss.SearchName]+1);
        }
        public static void SetAllAsViewed(SearchType ss)
        {
            if (CurrentItems.ContainsKey(ss.SearchName))
            {
                if (CurrentItems[ss.SearchName] != null)
                {
                    findItemsAdvancedResponse.eBayItem[] a = new findItemsAdvancedResponse.eBayItem[CurrentItems[ss.SearchName].Count];
                    CurrentItems[ss.SearchName].CopyTo(a);
                    foreach (findItemsAdvancedResponse.eBayItem eb in a)
                    {
                        findItemsAdvancedResponse.eBayItem b = eb;
                        b.Viewed = true;
                        CurrentItems[ss.SearchName].Remove(eb);

                        CurrentItems[ss.SearchName].Add(b);
                    }
                }
            }
        }
        public static void AddItem(findItemsAdvancedResponse.eBayItem eBayItem, SearchType ss)
        {
            if (!CurrentItems.ContainsKey(ss.SearchName))
            {
                CurrentItems.Add(ss.SearchName, null);
            }

            if (CurrentItems[ss.SearchName] == null)
            {
                CurrentItems[ss.SearchName] = new List <findItemsAdvancedResponse.eBayItem>();
            }

            //if (CurrentItems.Count > 0)
            //{


            findItemsAdvancedResponse.eBayItem sty = (findItemsAdvancedResponse.eBayItem)CurrentItems[ss.SearchName].Find((findItemsAdvancedResponse.eBayItem ei) => ei.ItemId.Equals(eBayItem.ItemId));

            if (sty == null)
            {
                CurrentItems[ss.SearchName].Add(eBayItem);
                //Fire New Item event here
                onNewItem(eBayItem, ss);
            }
            else
            {
                //TODO: COuld make this update here

                //CurrentItems[ss.SearchName].Remove(sty);
                //CurrentItems[ss.SearchName].Add(eBayItem);
            }



            //}
            //else
            //{
            //    CurrentItems[ss.SearchName].Add(eBayItem);

            //    //Fire New Item event here
            //    onNewItem(eBayItem);
            //}
        }
        private void MarkAllAsRead()
        {
            //mark all items in this search as read...

            foreach (DataGridViewRow dvr in dataGridView1.Rows)
            {
                findItemsAdvancedResponse.eBayItem ei = (findItemsAdvancedResponse.eBayItem)dvr.Tag;
                if (ei != null)
                {
                    eBayItems.SetAsViewed(thisSearch, ei);
                }
            }


            eBayItems.SaveItems();


            onViewed(thisSearch, null);
        }
        public static void SetAsViewed(SearchType ss, findItemsAdvancedResponse.eBayItem eBayItem)
        {
            if (eBayItem == null)
            {
                return;
            }

            if (CurrentItems.ContainsKey(ss.SearchName))
            {
                if (CurrentItems[ss.SearchName] != null)
                {
                    findItemsAdvancedResponse.eBayItem sty = (findItemsAdvancedResponse.eBayItem)CurrentItems[ss.SearchName].Find((findItemsAdvancedResponse.eBayItem ei) => ei.ItemId.Equals(eBayItem.ItemId));



                    sty.Viewed = true;
                    CurrentItems[ss.SearchName].Remove(sty);
//
                    CurrentItems[ss.SearchName].Add(sty);
                }
            }
        }
        public static double GetBestPrice(SearchType Search)
        {
            //find best preice from currentITems
            double lowest = 10000000000;

            if (!CurrentItems.ContainsKey(Search.SearchName))
            {
                return(lowest);
            }


            if (CurrentItems[Search.SearchName] != null)
            {
                findItemsAdvancedResponse.eBayItem[] list = new findItemsAdvancedResponse.eBayItem[CurrentItems[Search.SearchName].Count];

                CurrentItems[Search.SearchName].CopyTo(list);

                if (list != null)
                {
                    foreach (findItemsAdvancedResponse.eBayItem item in list)
                    {
                        if (item.SellingStatus.CurrentPrice != null)
                        {
                            if (item.SellingStatus.CurrentPrice.Amount < lowest)
                            {
                                lowest = item.SellingStatus.CurrentPrice.Amount;
                            }
                        }
                        //if (item.ListingInfo.BuyItNowPrice != null)
                        //{
                        //    if (item.ListingInfo.BuyItNowPrice.Amount < lowest)
                        //        lowest = item.ListingInfo.BuyItNowPrice.Amount;
                        //}
                    }
                }
            }
            return(lowest);
        }
 private static bool Viewed(findItemsAdvancedResponse.eBayItem item)
 {
     return(item.Viewed);
 }