using ActionGame.GameResources.Models;
using ActionGame.RawResources.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;


namespace ActionGame.GameResources.Repository
{
    public class FileCharacterSetRepository : IGameContentRepository<CharacterSet>
    {
        private IDictionary<Int32, CharacterSet> ContentByKey { get; set; }
        private IList<Int32> Keys { get; set; }
        private IRawContentManager rawContentManager;


        public FileCharacterSetRepository(IRawContentManager rawContentManager, String filename)
        {
            this.rawContentManager = rawContentManager;

            this.ContentByKey = new Dictionary<Int32, CharacterSet>();
            this.Keys = new List<Int32>();

            Load(filename);
        }


        public CharacterSet GetContent(Int32 key)
        {
            CharacterSet 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)
        {
            CharacterSet characterSet = null;

            characterSet = new CharacterSet()
            {
                Key = Int32.Parse(lineContents[0])
            };

            Texture2D rawTexture = this.rawContentManager.TextureRepository.GetContent(Int32.Parse(lineContents[1]));
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Up, 0, 0);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Up_1, 1, 0);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Up_2, 0, 0);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Up_3, 2, 0);

            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Down, 0, 1);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Down_1, 1, 1);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Down_2, 0, 1);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Down_3, 2, 1);

            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Left, 0, 2);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Left_1, 1, 2);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Left_2, 0, 2);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Left_3, 2, 2);

            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Right, 0, 3);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Right_1, 1, 3);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Right_2, 0, 3);
            SetFrame(characterSet, rawTexture, CharacterSet.Frame.Right_3, 2, 3);

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


        private void SetFrame(CharacterSet characterSet, Texture2D rawTexture, CharacterSet.Frame frame, Int32 x, Int32 y)
        {
            Int32 w = CharacterSet.DRAW_WIDTH;
            Int32 h = CharacterSet.DRAW_HEIGHT;

            Texture2D frameTexture = new Texture2D(rawTexture.GraphicsDevice, w, h);
            Color[] frameColor = new Color[w * h];
            Rectangle frameRect = default(Rectangle);

            frameRect = new Rectangle(x * w, y * h, w, h);
            rawTexture.GetData(0, frameRect, frameColor, 0, frameColor.Length);
            frameTexture.SetData(frameColor);

            characterSet.FrameTextures.Add(frame, frameTexture);
        }



    }
}
