﻿using System.Data;
using WarframeDropRatesData.Models.Repositories;
using WarframeDropRatesData.Models.Status;
using WarframeDropRatesDataParser;

namespace WarframeDropRatesData.Models.Search.SearchDropRatesChain
{
    /// <summary>
    /// </summary>
    internal class EnemySearcher : DropRatesDataSearcher
    {
        /// <summary>
        /// </summary>
        /// <param name="dropRatesData"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public override DataView Search(DropRatesRawData dropRatesData, string search)
        {
            IEnemyDropsRepository repository = new EnemyDropsRepository(dropRatesData);

            if (repository.IsEnemy(search))
            {
                var enemyDrops = repository.GetDropsByEnemy(search);

                if (enemyDrops != null)
                {
                    StatusService.CurrentStatus = $"Enemy: {enemyDrops.TableName}";
                    return CreateDataView(enemyDrops);
                }

                return null;
            }

            if (Successor == null)
            {
                StatusService.SetStatusNothingFound();
                return null;
            }

            return Successor.Search(dropRatesData, search);
        }

        private static DataView CreateDataView(DataTable enemyDrops)
        {
            enemyDrops.Columns.Remove(EnemyDropsRepository.EnemyNameColumnName);

            var dataView = enemyDrops.DefaultView;
            dataView.AllowNew = false;
            dataView.AllowEdit = false;
            dataView.AllowDelete = false;
            dataView.Sort = $"{Repository.DropChanceColumnName} DESC, {Repository.ItemColumnName} ASC";

            return dataView;
        }
    }
}