﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE.Core.CommonTypes.Services;
using BE.Core.CommonTypes;
using BE.WorldServices.SQLiteProvider;
using Dapper;

namespace BE.WorldServices.Items
{
    public class ItemsLoadService : BaseLoadService<Item>
    {
        protected override Item LoadNextEntity(params object[] @params)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Item> Load(params object[] @params)
        {
            var itemGroups = @params as ItemGroup[];

            var res = SQLite.GetConnection()
                .Query<Item>("select Id, Name, Description, ItemGroup ItemGroupId from Items")
                .Select((i) =>
                {
                    i.ItemGroup = itemGroups.First(g => g.Id == i.ItemGroupId);                    
                    return i;
                }).ToArray();

            return res;
        }

        public override void CheckParams(params object[] @params)
        {
            if (!(@params is ItemGroup[]))
                throw new LoadServiceException(string.Format(Properties.Exceptions.LoadService_CheckParams_WrongParams, this, "ItemGroup[]"));
        }
    }
}
