using ActionGame.GameResources.Models;
using System;
using System.Collections.Generic;
using System.IO;


namespace ActionGame.GameResources.Repository
{
    public class FileEnemyDefinitionRepository : IGameContentRepository<EnemyDefinition>
    {
        private IDictionary<Int32, EnemyDefinition> ContentByKey { get; set; }
        private IList<Int32> Keys { get; set; }


        public FileEnemyDefinitionRepository(String filename)
        {
            this.ContentByKey = new Dictionary<Int32, EnemyDefinition>();
            this.Keys = new List<Int32>();

            Load(filename);
        }


        public EnemyDefinition GetContent(Int32 key)
        {
            EnemyDefinition content = null;

            if (this.ContentByKey.ContainsKey(key))
            {
                content = this.ContentByKey[key];
            }

            return content;
        }


        public IList<Int32> GetKeys()
        {
            return this.Keys;
        }

        private void Load(String filename)
        {
            Stream stream = null;
            StreamReader reader = null;
            String line = null;
            String[] lineContents = null;

            stream = File.OpenRead(filename);
            reader = new StreamReader(stream);

            //skip first line
            reader.ReadLine();

            while ((line = reader.ReadLine()) != null)
            {
                lineContents = line.Split(',');

                if (lineContents[0].Equals("") == false)
                {
                    LoadContent(lineContents);
                }
            }

            reader.Close();
        }


        private void LoadContent(String[] lineContents)
        {
            EnemyDefinition enemyDefinition = new EnemyDefinition()
            {
                Key = Int32.Parse(lineContents[0]),
                Name = lineContents[1],
                CharacterSetKey = Int32.Parse(lineContents[2]),
                MovementKey = Int32.Parse(lineContents[3]),
                Health = Int32.Parse(lineContents[4]),
                Attack = Int32.Parse(lineContents[5]),
                Defence = Int32.Parse(lineContents[6]),
                AttackKey = Int32.Parse(lineContents[7]),
                AttackSpeed = Double.Parse(lineContents[8]),
                VariableToSetTrueUponDeath = lineContents[10],
                HideIfVariableTrue = lineContents[11]
            };

            String inventoryRaw = lineContents[9];
            if (inventoryRaw.Equals("") == false)
            {
                String[] containers = inventoryRaw.Split('|');
                foreach (String container in containers)
                {
                    String[] itemWithQuantity = container.Split(':');

                    ItemContainer itemDefinitionContainer = new ItemContainer()
                    {
                        ItemKey = Int32.Parse(itemWithQuantity[0]),
                        ChanceToDrop = Double.Parse(itemWithQuantity[2])
                    };

                    if (itemWithQuantity.Length > 1) itemDefinitionContainer.Quantity = Int32.Parse(itemWithQuantity[1]);
                    else itemDefinitionContainer.Quantity = null;

                    enemyDefinition.Inventory.Add(itemDefinitionContainer);
                }
            }

            this.ContentByKey.Add(enemyDefinition.Key, enemyDefinition);
            this.Keys.Add(enemyDefinition.Key);
        }
    }
}
