/// <summary>
 /// Constructor for this extension class.
 /// </summary>
 /// <param name="searchViewModel">The view model.</param>
 /// <param name="user">The user.</param>
 /// <param name="searchViewModelFromSession">The view model stored in session.</param>
 public static void InitSearchViewModel(this SearchViewModel searchViewModel, IUserContext user, SearchViewModel searchViewModelFromSession)
 {
     mUser = user;
     mSearchViewModelFromSession = searchViewModelFromSession;
 }
        ///// <summary>
        ///// Check dataselection and if only red listed is selected then check if taxa has factor 743/red listed.
        ///// Remove other taxa then from result depending on data selection LC/NE/NA taxa could be included or excluded.
        ///// </summary>
        ///// <param name="result">
        ///// Result taxa list from taxa selection search.
        ///// </param>
        //private void RemoveTaxaNotRedlistedOrRedlistedEnsuredFromList(List<TaxonListInformation> result)
        //{
        //    // Make a copy of the list
        //    var tempResult = new List<TaxonListInformation>();
        //    if (result.IsNotEmpty())
        //    {
        //        tempResult.AddRange(result);
        //    }

        //    // Check dataselection and if only redlisted is selected then check if taxa has factor 743/redlisted and is not of type
        //    // LC/NE/NA. Remove other taxa then from result.
        //    if (SessionHandler.UseOnlyRedlistedData)
        //    {
        //        foreach (TaxonListInformation taxonListInformation in tempResult.Where(taxonListInformation => !taxonListInformation.IsRedListed))
        //        {
        //            result.RemoveAll(x => x.Id == taxonListInformation.Id);
        //        }
        //    }
        //}

        /// <summary>
        /// Get taxa by user selections.
        /// </summary>
        /// <param name="model">
        /// Search view model.
        /// </param>
        /// <param name="useNonCategorizedTaxaIdOnly">
        /// Indicates if only taxa without category red listed (743) set.
        /// </param>
        /// <param name="selectedCategories"> returns selected categories to filter on if selection of non categorized data is choosen to be viewed.</param>
        /// <returns>
        /// List of taxon ids.
        /// </returns>
        private TaxonIdList GetTaxaBySearchCriteria(SearchViewModel model, out bool useNonCategorizedTaxaIdOnly, out IList <RedListCategoryItemViewModel> selectedCategories)
        {
            var searchCriteria = new AnalysisSearchCriteria();

            bool onlyNonCategorizedTaxaIdUsed = false;

            useNonCategorizedTaxaIdOnly = false;
            selectedCategories          = null;

            if (model.RedListCategories.IsNotEmpty())
            {
                // Get number of selected
                selectedCategories = model.RedListCategories.Where(item => item.Selected).ToList();
                const int NonCategorizedTaxaId = 1000;

                // Check if non categorizedtaxa is the selected then we have to get all taxa.
                if (model.RedListCategories.Any(redListCategory =>
                                                (redListCategory.Id == NonCategorizedTaxaId) && redListCategory.Selected))
                {
                    onlyNonCategorizedTaxaIdUsed = true;
                    useNonCategorizedTaxaIdOnly  = true;
                }
            }

            if (!onlyNonCategorizedTaxaIdUsed)
            {
                searchCriteria.InitRedListCategories(model);
            }
            else
            {
                // Getting alla taxa for all categories; must filter below on data not categorized.
                searchCriteria.RedListCategories = null;
            }

            //// Red list taxon categories / (Arter/Småarter/Underarter)
            //searchCriteria.InitRedListTaxonCategories(model);

            //// Scope
            //searchCriteria.InitTaxonScope(model);

            //// Swedish occurrence
            //searchCriteria.InitSwedishOccurrenceInformation(model);

            //// Organism / Organismgrupp
            //searchCriteria.InitOrganismGroups(model);

            //// Landscape / Landskapstyp
            //searchCriteria.InitLandscapeTypes(model);

            //// County / Län
            //searchCriteria.InitCountyOccurrencesInformation(model);

            //// Biotope / Biotop
            //searchCriteria.InitBiotopeInformation(model);

            //// Substrate / Substrat
            //searchCriteria.InitSubstrate(model);

            //// Impact / Påverkan
            //searchCriteria.InitImpact(model);

            //// Lifeforms / Livsform
            //searchCriteria.InitLifeForms(model);

            //// Host / Värd
            //searchCriteria.InitHost(model);

            //// Thematic listing / Tematisk lista
            //searchCriteria.InitThematicListInformation(model);

            // Fetch the taxonids
            TaxonIdList taxonIds = CoreData.AnalysisManager.GetTaxonIds(searchCriteria);

            return(taxonIds);
        }
        //public static void InitSwedishOccurrenceInformation(this SearchViewModel searchViewModel)
        //{
        //    if (mSearchViewModelFromSession.IsNotNull() && mSearchViewModelFromSession.SwedishOccurrences.IsNotNull())
        //    {
        //        searchViewModel.IsSwedishOccurrenceEnabled = mSearchViewModelFromSession.IsSwedishOccurrenceEnabled;
        //        searchViewModel.SwedishOccurrenceTitle = mSearchViewModelFromSession.SwedishOccurrenceTitle;
        //        searchViewModel.SwedishOccurrences = mSearchViewModelFromSession.SwedishOccurrences;
        //    }
        //    else
        //    {
        //        searchViewModel.SwedishOccurrences = new List<RedListSwedishOccurrenceItemViewModel>();

        //        // Get swedish occurrence factor
        //        FactorList swedishOccurrenceFactors = SwedishOccurrenceCache.GetFactors(mUser);

        //        // Populate swedish occurrence
        //        foreach (var swedishOccurrenceFactor in swedishOccurrenceFactors)
        //        {
        //            foreach (var swedishOccurrenceItem in swedishOccurrenceFactor.DataType.Field1.Enum.Values)
        //            {
        //                // The level of swedish occurrence is defined in the appsettings
        //                if (swedishOccurrenceItem.KeyInt.HasValue && swedishOccurrenceItem.KeyInt.Value > AppSettings.Default.SwedishOccurrenceExist)
        //                {
        //                    var redListSwedishOccurrenceItemViewModel = new RedListSwedishOccurrenceItemViewModel
        //                    {
        //                        Id = swedishOccurrenceItem.KeyInt.Value,
        //                        Name = swedishOccurrenceItem.OriginalLabel,
        //                        Selected = true,
        //                    };
        //                    searchViewModel.SwedishOccurrences.Add(redListSwedishOccurrenceItemViewModel);
        //                }
        //            }
        //        }

        //        // Set swedish occurrences to true as default
        //        searchViewModel.IsSwedishOccurrenceEnabled = true;
        //    }
        //}

        ///// <summary>
        ///// Init Biotopes information.
        ///// </summary>
        ///// <param name="searchViewModel">Search criteria view model.</param>
        //public static void InitBiotopeInformation(this SearchViewModel searchViewModel)
        //{
        //    if (mSearchViewModelFromSession.IsNotNull() && mSearchViewModelFromSession.Biotopes.IsNotNull())
        //    {
        //        searchViewModel.IsBiotopeEnabled = mSearchViewModelFromSession.IsBiotopeEnabled;
        //        searchViewModel.IsBiotopeImportant = mSearchViewModelFromSession.IsBiotopeImportant;
        //        searchViewModel.BiotopeOperator = mSearchViewModelFromSession.BiotopeOperator;
        //        searchViewModel.Biotopes = mSearchViewModelFromSession.Biotopes;
        //        searchViewModel.BiotopesTitle = mSearchViewModelFromSession.BiotopesTitle;
        //    }
        //    else
        //    {
        //        searchViewModel.Biotopes = new List<RedListBiotopeItemViewModel>();

        //        // Get superior factor for biotopes.
        //        IFactor superiorFactor = CoreData.FactorManager.GetFactor(mUser, FactorId.Biotopes);

        //        // Get biotope factors
        //        FactorList biotopeFactors = BiotopeCache.GetFactors(mUser);

        //        // Set title for biotopes, from factor.
        //        searchViewModel.BiotopesTitle = superiorFactor.Label;

        //        // Populate biotopes
        //        foreach (var biotopeFactor in biotopeFactors)
        //        {
        //            var redListBiotopeItemViewModel = new RedListBiotopeItemViewModel
        //            {
        //                Id = biotopeFactor.Id,
        //                Name = biotopeFactor.Name
        //            };

        //            searchViewModel.Biotopes.Add(redListBiotopeItemViewModel);
        //        }

        //        searchViewModel.BiotopeOperator = LogicalOperator.Or;
        //    }
        //}

        ///// <summary>
        ///// Init CountyOccurrences information.
        ///// </summary>
        ///// <param name="searchViewModel">Search criteria view model.</param>
        //public static void InitCountyOccurrencesInformation(this SearchViewModel searchViewModel)
        //{
        //    if (mSearchViewModelFromSession.IsNotNull() && mSearchViewModelFromSession.CountyOccurrences.IsNotNull())
        //    {
        //        searchViewModel.IsCountyOccurrenceEnabled = mSearchViewModelFromSession.IsCountyOccurrenceEnabled;
        //        searchViewModel.CountyOccurrences = mSearchViewModelFromSession.CountyOccurrences;
        //        searchViewModel.CountyOccurrencesTitle = mSearchViewModelFromSession.CountyOccurrencesTitle;
        //    }
        //    else
        //    {
        //        searchViewModel.CountyOccurrences = new List<RedListCountyOccurrenceItemViewModel>();

        //        // Get superior factor, for county occurrence.
        //        IFactor superiorFactor = CoreData.FactorManager.GetFactor(mUser, FactorId.CountyOccurrence);

        //        // Get county occurrence factors
        //        FactorList countyOccurenceFactors = CountyOccurrenceCache.GetFactors(mUser);

        //        // Set title for county occurrence, from factor.
        //        searchViewModel.CountyOccurrencesTitle = superiorFactor.Label;

        //        // Populate county occurrence
        //        foreach (var countyOccurenceFactor in countyOccurenceFactors)
        //        {
        //            var redListCountyOccurrenceItemViewModel = new RedListCountyOccurrenceItemViewModel
        //            {
        //                Id = countyOccurenceFactor.Id,
        //                Name = countyOccurenceFactor.Name
        //            };

        //            searchViewModel.CountyOccurrences.Add(redListCountyOccurrenceItemViewModel);
        //        }
        //    }
        //}

        ///// <summary>
        ///// Init ThematicLists information.
        ///// </summary>
        ///// <param name="searchViewModel">Search criteria view model.</param>
        //public static void InitThematicListInformation(this SearchViewModel searchViewModel)
        //{
        //    searchViewModel.ThematicLists = new List<RedListThematicListItemViewModel>();
        //    if (mSearchViewModelFromSession.IsNotNull() && mSearchViewModelFromSession.ThematicLists.IsNotNull())
        //    {
        //        searchViewModel.IsThematicListEnabled = mSearchViewModelFromSession.IsThematicListEnabled;
        //        searchViewModel.ThematicListOperator = mSearchViewModelFromSession.ThematicListOperator;
        //        searchViewModel.ThematicLists = mSearchViewModelFromSession.ThematicLists;
        //    }
        //    else
        //    {
        //        foreach (ThematicListEnum @enum in Enum.GetValues(typeof(ThematicListEnum)).Cast<ThematicListEnum>())
        //        {
        //            var redListThematicListItemViewModel = new RedListThematicListItemViewModel();
        //            switch (@enum)
        //            {
        //                case ThematicListEnum.ContinuousDecline:
        //                    redListThematicListItemViewModel.Id = (int)@enum;
        //                    redListThematicListItemViewModel.Name = RedListResource.TaxonSearchThematicListContinuousDecline;
        //                    break;
        //                case ThematicListEnum.ExtremeFluctuations:
        //                    redListThematicListItemViewModel.Id = (int)@enum;
        //                    redListThematicListItemViewModel.Name = RedListResource.TaxonSearchThematicListExtremeFluctuations;
        //                    break;
        //                case ThematicListEnum.SevereleyFragmented:
        //                    redListThematicListItemViewModel.Id = (int)@enum;
        //                    redListThematicListItemViewModel.Name = RedListResource.TaxonSearchThematicListSeverelyFragmented;
        //                    break;
        //                case ThematicListEnum.SmallPopulationOrDistribution:
        //                    redListThematicListItemViewModel.Id = (int)@enum;
        //                    redListThematicListItemViewModel.Name = RedListResource.TaxonSearchThematicListSmallPopulationOrDistribution;
        //                    break;
        //            }

        //            searchViewModel.ThematicLists.Add(redListThematicListItemViewModel);
        //        }

        //        searchViewModel.ThematicListOperator = LogicalOperator.Or;
        //    }
        //}

        /// <summary>
        /// Init RedListCategories information.
        /// </summary>
        /// <param name="searchViewModel">Search criteria view model.</param>
        public static void InitRedListCategories(this SearchViewModel searchViewModel)
        {
            if (mSearchViewModelFromSession.IsNotNull() && mSearchViewModelFromSession.RedListCategories.IsNotNull())
            {
                searchViewModel.IsRedListCategoriesEnabled = mSearchViewModelFromSession.IsRedListCategoriesEnabled;
                searchViewModel.RedListCategories          = mSearchViewModelFromSession.RedListCategories;
                searchViewModel.RedListCategoriesTitle     = mSearchViewModelFromSession.RedListCategoriesTitle;
            }
            else
            {
                var otherListedGroupCategoryIds = new List <int>();

                // Get red list category factor
                FactorList redListCategoryFactors = RedListCategoryCache.GetFactors(mUser);

                // Set red list categories title, from factor.
                searchViewModel.RedListCategoriesTitle = redListCategoryFactors[0].Label;

                // Group a limited list of red list categories; "red listed group"
                var        categories = RedListedHelper.GetRedListCategoriesDdToNt();
                List <int> redListedGroupCategoryIds = categories.Select(cat => (int)cat).ToList();

                // Group a limited list of red list categories; "other group"
                RedListCategory redListCategory;
                for (redListCategory = RedListCategory.LC; redListCategory <= RedListCategory.NE; redListCategory++)
                {
                    otherListedGroupCategoryIds.Add((int)redListCategory);
                }

                searchViewModel.RedListCategories = new List <RedListCategoryItemViewModel>();

                // Populate red list categories
                RedListCategoryItemViewModel redListCategoryItemViewModel;
                foreach (var redListCategoryFactor in redListCategoryFactors)
                {
                    foreach (var redListCategoryItem in redListCategoryFactor.DataType.Field1.Enum.Values)
                    {
                        if (redListCategoryItem.KeyInt.HasValue && redListedGroupCategoryIds.Contains(redListCategoryItem.KeyInt.Value))
                        {
                            redListCategoryItemViewModel = new RedListCategoryItemViewModel
                            {
                                Id               = redListCategoryItem.Id,
                                Name             = redListCategoryItem.OriginalLabel,
                                OrderNumber      = redListCategoryItem.KeyInt.Value,
                                Selected         = true,
                                InRedListedGroup = true,
                                InOtherGroup     = false
                            };
                            searchViewModel.RedListCategories.Add(redListCategoryItemViewModel);
                        }
                        if (redListCategoryItem.KeyInt.HasValue && otherListedGroupCategoryIds.Contains(redListCategoryItem.KeyInt.Value))
                        {
                            redListCategoryItemViewModel = new RedListCategoryItemViewModel
                            {
                                Id               = redListCategoryItem.Id,
                                Name             = redListCategoryItem.OriginalLabel,
                                OrderNumber      = redListCategoryItem.KeyInt.Value,
                                Selected         = false,
                                InRedListedGroup = false,
                                InOtherGroup     = true
                            };
                            searchViewModel.RedListCategories.Add(redListCategoryItemViewModel);
                        }
                    }
                }

                const bool ShowNonCategorizedTaxa = true;
                const int  NonCategorizedTaxaId   = 1000;

                // Add value for non categorized taxa. Ie taxa that dont have category 743
                if (ShowNonCategorizedTaxa)
                {
                    redListCategoryItemViewModel = new RedListCategoryItemViewModel
                    {
                        Id               = NonCategorizedTaxaId,
                        Name             = "Non categorized taxa -TEST", //RedListResource.TaxonSearchNonCategorizedTaxaLabel,
                        OrderNumber      = NonCategorizedTaxaId,
                        Selected         = false,
                        InRedListedGroup = false,
                        InOtherGroup     = true
                    };
                    searchViewModel.RedListCategories.Add(redListCategoryItemViewModel);
                }

                // Preselect red list category group as default
                searchViewModel.IsRedListCategoriesEnabled = true;
            }
        }