<commit_before>﻿using ARKBreedingStats.species;
using ARKBreedingStats.values;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ARKBreedingStats
{
    public partial class SpeciesSelector : UserControl
    {
        /// <summary>
        /// Is invoked if a species was selected. The parameter is true if the species was changed.
        /// </summary>
        public event Action<bool> OnSpeciesSelected;

        /// <summary>
        /// The currently selected species
        /// </summary>
        public Species SelectedSpecies { get; private set; }

        /// <summary>
        /// Items for the species list.
        /// </summary>
        private List<SpeciesListEntry> _entryList;

        /// <summary>
        /// The TextBox control for the species searching which is outside of this control.
        /// </summary>
        private uiControls.TextBoxSuggest _textBox;

        /// <summary>
        /// List of species-blueprintPaths last used by the user
        /// </summary>
        private List<string> _lastSpeciesBPs;
        private List<string> _iconIndices;
        private CancellationTokenSource _cancelSource;

        public SpeciesSelector()
        {
            InitializeComponent();
            _lastSpeciesBPs = new List<string>();
            _iconIndices = new List<string>();
            SplitterDistance = Properties.Settings.Default.SpeciesSelectorVerticalSplitterDistance;
        }

        /// <summary>
        /// Initializes the species lists.
        /// </summary>
        /// <param name="species"></param>
        /// <param name="aliases"></param>
        public void SetSpeciesLists(List<Species> species, Dictionary<string, string> aliases)
        {
            ImageList imageList;
            (_entryList, imageList, _iconIndices) = LoadSpeciesImagesAndCreateSpeciesList(species, aliases);

            imageList.ImageSize = new Size(64, 64);
            lvLastSpecies.LargeImageList = imageList;
            lvSpeciesInLibrary.LargeImageList = imageList;

            // autocomplete for species-input
            var al = new AutoCompleteStringCollection();
            al.AddRange(_entryList.Select(e => e.SearchName).ToArray());
            _textBox.AutoCompleteCustomSource = al;

            cbDisplayUntameable.Checked = Properties.Settings.Default.DisplayNonDomesticableSpecies;
            FilterList();
        }

        private static (List<SpeciesListEntry>, ImageList, List<string>) LoadSpeciesImagesAndCreateSpeciesList(List<Species> species, Dictionary<string, string> aliases)
        {
            Dictionary<string, Species> speciesNameToSpecies = new Dictionary<string, Species>();

            var creatureColors = new int[]
                {44, 42, 57, 10, 26, 78}; // uniform color pattern that is used for all species in the selector
            var creatureColorsPolar = new int[]
                {18, 18, 18, 18, 18, 18}; // uniform color pattern that is used for all polar species in the selector
            ImageList lImgList = new ImageList();
            var iconIndices = new List<string>();

            //var speciesWOImage = new List<string>();// TODO debug
            foreach (Species ss in species)
            {
                if (!speciesNameToSpecies.ContainsKey(ss.DescriptiveNameAndMod))
                    speciesNameToSpecies.Add(ss.DescriptiveNameAndMod, ss);

                var (imgExists, imagePath, speciesListName) = CreatureColored.SpeciesImageExists(ss,
                    ss.name.Contains("Polar") ? creatureColorsPolar : creatureColors);
                if (imgExists && !iconIndices.Contains(speciesListName))
                {
                    lImgList.Images.Add(Image.FromFile(imagePath));
                    iconIndices.Add(speciesListName);
                }

                //if (!imgExists && !speciesWOImage.Contains(ss.name)) speciesWOImage.Add(ss.name);
            }
            //Clipboard.SetText(string.Join("\n", speciesWOImage));


            var entryList = new List<SpeciesListEntry>();

            foreach (var s in species)
            {
                entryList.Add(new SpeciesListEntry
                {
                    DisplayName = s.name,
                    SearchName = s.name,
                    ModName = s.Mod?.title ?? string.Empty,
                    Species = s
                });<commit_msg>ignore imageCreation for speciesSelector if no images are available<commit_after>﻿using ARKBreedingStats.species;
using ARKBreedingStats.values;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ARKBreedingStats
{
    public partial class SpeciesSelector : UserControl
    {
        /// <summary>
        /// Is invoked if a species was selected. The parameter is true if the species was changed.
        /// </summary>
        public event Action<bool> OnSpeciesSelected;

        /// <summary>
        /// The currently selected species
        /// </summary>
        public Species SelectedSpecies { get; private set; }

        /// <summary>
        /// Items for the species list.
        /// </summary>
        private List<SpeciesListEntry> _entryList;

        /// <summary>
        /// The TextBox control for the species searching which is outside of this control.
        /// </summary>
        private uiControls.TextBoxSuggest _textBox;

        /// <summary>
        /// List of species-blueprintPaths last used by the user
        /// </summary>
        private List<string> _lastSpeciesBPs;
        private List<string> _iconIndices;
        private CancellationTokenSource _cancelSource;

        public SpeciesSelector()
        {
            InitializeComponent();
            _lastSpeciesBPs = new List<string>();
            _iconIndices = new List<string>();
            SplitterDistance = Properties.Settings.Default.SpeciesSelectorVerticalSplitterDistance;
        }

        /// <summary>
        /// Initializes the species lists.
        /// </summary>
        /// <param name="species"></param>
        /// <param name="aliases"></param>
        public void SetSpeciesLists(List<Species> species, Dictionary<string, string> aliases)
        {
            ImageList imageList;
            (_entryList, imageList, _iconIndices) = LoadSpeciesImagesAndCreateSpeciesList(species, aliases);

            imageList.ImageSize = new Size(64, 64);
            lvLastSpecies.LargeImageList = imageList;
            lvSpeciesInLibrary.LargeImageList = imageList;

            // autocomplete for species-input
            var al = new AutoCompleteStringCollection();
            al.AddRange(_entryList.Select(e => e.SearchName).ToArray());
            _textBox.AutoCompleteCustomSource = al;

            cbDisplayUntameable.Checked = Properties.Settings.Default.DisplayNonDomesticableSpecies;
            FilterList();
        }

        private static (List<SpeciesListEntry>, ImageList, List<string>) LoadSpeciesImagesAndCreateSpeciesList(List<Species> species, Dictionary<string, string> aliases)
        {
            Dictionary<string, Species> speciesNameToSpecies = new Dictionary<string, Species>();

            var creatureColors = new int[]
                {44, 42, 57, 10, 26, 78}; // uniform color pattern that is used for all species in the selector
            var creatureColorsPolar = new int[]
                {18, 18, 18, 18, 18, 18}; // uniform color pattern that is used for all polar species in the selector
            ImageList lImgList = new ImageList();
            var iconIndices = new List<string>();
            bool imageFolderExist = Directory.Exists(FileService.GetPath(FileService.ImageFolderName));

            //var speciesWOImage = new List<string>();// to determine which species have no image yet
            foreach (Species ss in species)
            {
                if (!speciesNameToSpecies.ContainsKey(ss.DescriptiveNameAndMod))
                    speciesNameToSpecies.Add(ss.DescriptiveNameAndMod, ss);

                if (imageFolderExist)
                {
                    var (imgExists, imagePath, speciesListName) = CreatureColored.SpeciesImageExists(ss,
                        ss.name.Contains("Polar") ? creatureColorsPolar : creatureColors);
                    if (imgExists && !iconIndices.Contains(speciesListName))
                    {
                        lImgList.Images.Add(Image.FromFile(imagePath));
                        iconIndices.Add(speciesListName);
                    }

                    //if (!imgExists && !speciesWOImage.Contains(ss.name)) speciesWOImage.Add(ss.name);
                }
            }
            //Clipboard.SetText(string.Join("\n", speciesWOImage));

            var entryList = new List<SpeciesListEntry>();

            foreach (var s in species)
            {
                entryList.Add(new SpeciesListEntry
                {
                    DisplayName = s.name,
                    SearchName = s.name,
                    ModName = s.Mod?.title ?? string.Empty,
                    Species = s
                });
{'fraction_non_alphanumeric': 0.09535530821917808, 'fraction_numerical': 0.0059931506849315065, 'mean_word_length': 2.043973941368078, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5197677', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2284, 'n_words': 597}