<commit_before>﻿// Copyright (c) 2007-2017 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Configuration;
using osu.Framework.Input;
using OpenTK.Input;
using osu.Framework.MathUtils;
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Threading;
using osu.Framework.Configuration;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Cursor;

namespace osu.Game.Screens.Select
{
    public class BeatmapCarousel : OsuScrollContainer
    {
        public BeatmapInfo SelectedBeatmap => selectedPanel?.Beatmap;

        public override bool HandleInput => AllowSelection;

        public Action BeatmapsChanged;

        public IEnumerable<BeatmapSetInfo> Beatmaps
        {
            get { return groups.Select(g => g.BeatmapSet); }

            set
            {
                scrollableContent.Clear(false);
                panels.Clear();
                groups.Clear();

                List<BeatmapGroup> newGroups = null;

                Task.Run(() =>
                {
                    newGroups = value.Select(createGroup).Where(g => g != null).ToList();
                    criteria.Filter(newGroups);
                }).ContinueWith(t =>
                {
                    Schedule(() =>
                    {
                        foreach (var g in newGroups)
                            if (g != null) addGroup(g);

                        computeYPositions();
                        BeatmapsChanged?.Invoke();
                    });
                });
            }
        }

        private readonly List<float> yPositions = new List<float>();

        /// <summary>
        /// Required for now unfortunately.
        /// </summary>
        private BeatmapManager manager;

        private readonly Container<Panel> scrollableContent;

        private readonly List<BeatmapGroup> groups = new List<BeatmapGroup>();

        private Bindable<SelectionRandomType> randomType;
        private readonly List<BeatmapGroup> seenGroups = new List<BeatmapGroup>();

        private readonly List<Panel> panels = new List<Panel>();

        private readonly Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>> randomSelectedBeatmaps = new Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>>();

        private BeatmapGroup selectedGroup;
        private BeatmapPanel selectedPanel;

        public BeatmapCarousel()
        {
            Add(new OsuContextMenuContainer
            {
                RelativeSizeAxes = Axes.X,
                AutoSizeAxes = Axes.Y,
                Child = scrollableContent = new Container<Panel>
                {
                    RelativeSizeAxes = Axes.X,
                }
            });
        }

        public void AddBeatmap(BeatmapSetInfo beatmapSet)
        {
            Schedule(() =>
            {
                var group = createGroup(beatmapSet);

                if (group == null)
                    return;

                addGroup(group);
                computeYPositions();
                if (selectedGroup == null)
                    selectGroup(group);
            });
        }

        public void RemoveBeatmap(BeatmapSetInfo beatmapSet)
        {
            Schedule(() => removeGroup(groups.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
        }

        public void UpdateBeatmap(BeatmapInfo beatmap)
        {
            // todo: this method should not run more than once for the same BeatmapSetInfo.
            var set = manager.QueryBeatmapSet(s => s.ID == beatmap.BeatmapSetInfoID);

            // todo: this method should be smarter as to not recreate panels that haven't changed, etc.
            var group = groups.Find(b => b.BeatmapSet.ID == set.ID);

            BeatmapGroup newGroup;
            if (group == null)
            {
                newGroup = createGroup(set);

                if (newGroup != null)
                    groups.Add(newGroup);
            }
            else
            {
                int i = groups.IndexOf(group);
                groups.RemoveAt(i);

                newGroup = createGroup(set);

                if (newGroup != null)
                    groups.Insert(i, newGroup);
            }

            bool hadSelection = selectedGroup == group;

            if (hadSelection && newGroup == null)
                selectedGroup = null;

            Filter(null, false);

            //check if we can/need to maintain our current selection.
            if (hadSelection && newGroup != null)
            {
                var newSelection =
                    newGroup.BeatmapPanels.Find(p => p.Beatmap.ID == selectedPanel?.Beatmap.ID) ??
                    newGroup.BeatmapPanels[Math.Min(newGroup.BeatmapPanels.Count - 1, group.BeatmapPanels.IndexOf(selectedPanel))];

                selectGroup(newGroup, newSelection);
            }
        }

        public void SelectBeatmap(BeatmapInfo beatmap, bool animated = true)
        {
            if (beatmap == null || beatmap.Hidden)
            {
                SelectNext();
                return;
            }
<commit_msg>refactor newly added code to be less redundant<commit_after>﻿// Copyright (c) 2007-2017 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Configuration;
using osu.Framework.Input;
using OpenTK.Input;
using osu.Framework.MathUtils;
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Threading;
using osu.Framework.Configuration;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Cursor;

namespace osu.Game.Screens.Select
{
    public class BeatmapCarousel : OsuScrollContainer
    {
        public BeatmapInfo SelectedBeatmap => selectedPanel?.Beatmap;

        public override bool HandleInput => AllowSelection;

        public Action BeatmapsChanged;

        public IEnumerable<BeatmapSetInfo> Beatmaps
        {
            get { return groups.Select(g => g.BeatmapSet); }

            set
            {
                scrollableContent.Clear(false);
                panels.Clear();
                groups.Clear();

                List<BeatmapGroup> newGroups = null;

                Task.Run(() =>
                {
                    newGroups = value.Select(createGroup).Where(g => g != null).ToList();
                    criteria.Filter(newGroups);
                }).ContinueWith(t =>
                {
                    Schedule(() =>
                    {
                        foreach (var g in newGroups)
                            if (g != null) addGroup(g);

                        computeYPositions();
                        BeatmapsChanged?.Invoke();
                    });
                });
            }
        }

        private readonly List<float> yPositions = new List<float>();

        /// <summary>
        /// Required for now unfortunately.
        /// </summary>
        private BeatmapManager manager;

        private readonly Container<Panel> scrollableContent;

        private readonly List<BeatmapGroup> groups = new List<BeatmapGroup>();

        private Bindable<SelectionRandomType> randomType;
        private readonly List<BeatmapGroup> seenGroups = new List<BeatmapGroup>();

        private readonly List<Panel> panels = new List<Panel>();

        private readonly Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>> randomSelectedBeatmaps = new Stack<KeyValuePair<BeatmapGroup, BeatmapPanel>>();

        private BeatmapGroup selectedGroup;
        private BeatmapPanel selectedPanel;

        public BeatmapCarousel()
        {
            Add(new OsuContextMenuContainer
            {
                RelativeSizeAxes = Axes.X,
                AutoSizeAxes = Axes.Y,
                Child = scrollableContent = new Container<Panel>
                {
                    RelativeSizeAxes = Axes.X,
                }
            });
        }

        public void AddBeatmap(BeatmapSetInfo beatmapSet)
        {
            Schedule(() =>
            {
                var group = createGroup(beatmapSet);

                if (group == null)
                    return;

                addGroup(group);
                computeYPositions();
                if (selectedGroup == null)
                    selectGroup(group);
            });
        }

        public void RemoveBeatmap(BeatmapSetInfo beatmapSet)
        {
            Schedule(() => removeGroup(groups.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
        }

        public void UpdateBeatmap(BeatmapInfo beatmap)
        {
            // todo: this method should not run more than once for the same BeatmapSetInfo.
            var set = manager.QueryBeatmapSet(s => s.ID == beatmap.BeatmapSetInfoID);

            // todo: this method should be smarter as to not recreate panels that haven't changed, etc.
            var group = groups.Find(b => b.BeatmapSet.ID == set.ID);

            int i = groups.IndexOf(group);
            if (i >= 0)
                groups.RemoveAt(i);

            var newGroup = createGroup(set);

            if (newGroup != null)
            {
                if (i >= 0)
                    groups.Insert(i, newGroup);
                else
                    groups.Add(newGroup);
            }

            bool hadSelection = selectedGroup == group;

            if (hadSelection && newGroup == null)
                selectedGroup = null;

            Filter(null, false);

            //check if we can/need to maintain our current selection.
            if (hadSelection && newGroup != null)
            {
                var newSelection =
                    newGroup.BeatmapPanels.Find(p => p.Beatmap.ID == selectedPanel?.Beatmap.ID) ??
                    newGroup.BeatmapPanels[Math.Min(newGroup.BeatmapPanels.Count - 1, group.BeatmapPanels.IndexOf(selectedPanel))];

                selectGroup(newGroup, newSelection);
            }
        }

        public void SelectBeatmap(BeatmapInfo beatmap, bool animated = true)
        {
            if (beatmap == null || beatmap.Hidden)
            {
                SelectNext();
                return;
            }

{'fraction_non_alphanumeric': 0.0931386314705607, 'fraction_numerical': 0.0018721332958906674, 'mean_word_length': 1.6596962907642518, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1072698', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2622, 'n_words': 624}