public SortingAlgorithmInfo GetSortingAlgorithmForTab(Tab tab)
 {
     var s = SortingAlgorithmInfos.FirstOrDefault(x => x.League == tab.League.Name && x.TabIndex == tab.Index);
     return s ?? new SortingAlgorithmInfo()
     {
         Name = PoeSorter.SortingAlgorithms.FirstOrDefault().Name,
         Option = PoeSorter.SortingAlgorithms.FirstOrDefault().SortOption.Options.FirstOrDefault()
     };
 }
 internal void SetSortingAlgorithmForTab(string name, string option, Tab SelectedTab)
 {
     SortingAlgorithmInfo s = SortingAlgorithmInfos.FirstOrDefault(x => x.League == SelectedTab.League.Name && x.TabIndex == SelectedTab.Index);
     if (s == null)
     {
         s = new SortingAlgorithmInfo()
         {
             League = SelectedTab.League.Name,
             TabIndex = SelectedTab.Index
         };
         SortingAlgorithmInfos.Add(s);
     }
     s.Name = name;
     s.Option = option;
     SaveChanges();
 }
        public Tab SortTab(Tab tab)
        {
            Tab sortedTab = new Tab();
            sortedTab.Index = tab.Index;
            sortedTab.League = tab.League;
            sortedTab.Name = tab.Name;
            sortedTab.srcC = tab.srcC;
            sortedTab.srcL = tab.srcL;
            sortedTab.srcR = tab.srcR;
            sortedTab.Items = tab.Items.Select(item => item.CloneItem()).ToList();


            Sort(sortedTab, SortOption);

            foreach (var item in sortedTab.Items)
            {
                double offsetX = 47.4 * 13;
                item.Image.Margin = new Thickness(item.X * 47.4f + 2.2f + offsetX, item.Y * 47.4f + 2.2f, 0, 0);
            }

            return sortedTab;
        }
        public static async void SetSelectedTab(Tab tab)
        {
            if (tab != null)
            {
                // Unselect current tab
                if (SelectedTab != null)
                {
                    SelectedTab.IsSelected       = false;
                    SelectedTabSorted.IsSelected = false;

                    //if (SelectedTab.Items != null)
                    //{
                    //    SelectedTab.Items.ForEach(x => x.Image.Visibility = Visibility.Hidden);
                    //}
                    ////Remove old sorted tab preview
                    //if (SelectedTabSorted != null)
                    //SelectedTabSorted.Items.ForEach(x => ItemCanvas.Children.Remove(x.Image));
                }
                // Set new selected tab
                tab.IsSelected = true;
                SelectedTab    = tab;

                // Download selected tab;
                if (tab.Items == null)
                {
                    tab.Items = (await PoeConnector.FetchTabAsync(tab.Index, tab.League)).Items;
                }
                tab.Items.ForEach(x => x.Tab = tab);

                // If the tab still is selected after download is complete.
                if (tab.IsSelected)
                {
                    tab.Items.ForEach(x => x.Image.Visibility = Visibility.Visible);
                    SelectSortingAlgoritm(null);
                }
            }
        }
        public void StartSorting(Tab unsortedTab, Tab sortedTab)
        {
            ApplicationHelper.OpenPathOfExile();
            List<Item> unsortedItems = unsortedTab.Items.Where(x => sortedTab.Items.Any(c => c.Id == x.Id && c.X == x.X && x.Y == c.Y) == false).ToList();
            if (isSorting == false)
            {
                GetStashDimentions();
                isSorting = true;

                Item unsortedItem = unsortedItems.FirstOrDefault();

                if (unsortedItem != null)
                {
                    MouseTools.MoveCursor(MouseTools.GetMousePosition(), new Vector2(startPos.X + unsortedItem.X * cellWidth, startPos.Y + unsortedItem.Y * cellHeight), 20);

                    bool selectGem = true;


                    while (unsortedItem != null)
                    {
                        if (interrupt == true)
                        {
                            interrupt = false;
                            break;
                        }
                        Item sortedItem = sortedTab.Items.FirstOrDefault(x => x.Id == unsortedItem.Id);

                        Vector2 unsortedPos = new Vector2(startPos.X + unsortedItem.X * cellWidth, startPos.Y + unsortedItem.Y * cellHeight);

                        if (selectGem)
                        {
                            //Move to item
                            MouseTools.MoveCursor(MouseTools.GetMousePosition(), unsortedPos, 10);
                            //select item
                            MouseTools.MouseClickEvent();
                            //wait a little (internet delay)
                            Thread.Sleep((int)(80f / Settings.Instance.Speed));
                        }

                        Vector2 sortedPos = new Vector2(startPos.X + sortedItem.X * cellWidth, startPos.Y + sortedItem.Y * cellHeight);

                        //Log.Message("Moving " + unsortedItem.Name + " from " + unsortedItem.X + "," + unsortedItem.Y + " to " + sortedItem.X + "," + sortedItem.Y);

                        //move to correct position
                        MouseTools.MoveCursor(MouseTools.GetMousePosition(), sortedPos, 10);
                        //place item
                        MouseTools.MouseClickEvent();
                        //wait a little (internet delay)
                        Thread.Sleep((int)(80f / Settings.Instance.Speed));

                        Item newGem = unsortedItems.FirstOrDefault(x => x.X == sortedItem.X && x.Y == sortedItem.Y);

                        //remove unsorted now that it is sorted
                        unsortedItems.Remove(unsortedItem);

                        //if there wassent a item where the item was placed
                        if (newGem == null)
                        {
                            //selected a new to sort
                            unsortedItem = unsortedItems.FirstOrDefault();
                            selectGem = true;
                        }
                        else
                        {
                            unsortedItem = newGem;
                            selectGem = false;
                        }

                    }
                }
                //Log.Message("Sorting Complete");

            }
            isSorting = false;

        }
 protected abstract void Sort(Tab tab, SortOption options);