using ActionGame.GameResources.Models;
using ActionGame.RawResources.Models;
using GameMaker.Logic;
using GameMaker.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


namespace GameMaker.UI
{
    public partial class TileSetSelectPanel : Panel
    {
        public Session Session { get; set; }
        public Int32 TextureKey { get; set; }

        private Int32 cacheTextureKey;
        private Image cacheImage;
        private Dictionary<Int32, Image> cacheImages;
        public TileSetDefinition SelectedTileSet { get;  set; }


        public TileSetSelectPanel()
        {
            InitializeComponent();

            cacheImages = new Dictionary<Int32, Image>();
            this.DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.TranslateTransform(this.AutoScrollPosition.X,this.AutoScrollPosition.Y);

            if (this.Session == null) return;

            RawResource texture = this.Session.GetTexture(this.TextureKey);
            if (texture == null) return;

            if (cacheTextureKey != this.TextureKey)
            {
                cacheTextureKey = this.TextureKey;
                cacheImages.Clear();
                cacheImage = null;
            }

            //draw tileset image
            Image image = cacheImage;
            if (image == null)
            {
                image = Image.FromFile(RawResourceHelper.GetFilePathForTexture(this.Session.WorkingDirectory, texture.FilePath));
                cacheImage = image;
                this.AutoScrollMinSize = new Size(image.Width, image.Height);
            }

            //draw grid based on tile set            
            foreach (TileSetDefinition tileSet in Session.TileSets)
            {
                if (tileSet.TextureKey == this.TextureKey)
                {
                    if (cacheImages.ContainsKey(tileSet.Key) == false)
                    {
                        cacheImages.Add(tileSet.Key, GetImageSubset(image, new Rectangle(tileSet.X, tileSet.Y, tileSet.Width, tileSet.Height)));
                    }
                    Image tileImage = cacheImages[tileSet.Key];

                    Rectangle drawRect = new Rectangle(tileSet.X, tileSet.Y, tileSet.Width, tileSet.Height);
                    Rectangle borderRect = new Rectangle(tileSet.X, tileSet.Y, tileSet.Width - 1, tileSet.Height - 1);

                    e.Graphics.DrawImage(tileImage, drawRect);

                    if (tileSet == SelectedTileSet)
                    {
                        e.Graphics.DrawRectangle(new Pen(Color.FromArgb(255, 255, 0, 0)), borderRect);
                    }
                }
            }
        }


        private Image GetImageSubset(Image image, Rectangle rect)
        {
            Bitmap newImage = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(newImage);

            g.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel);

            return newImage;
        }


        protected override void OnClick(EventArgs e)
        {
            if (e.GetType() == typeof(MouseEventArgs) == false) return;

            MouseEventArgs me = (MouseEventArgs)e;
            Int32 tx = me.X + this.HorizontalScroll.Value;
            Int32 ty = me.Y + this.VerticalScroll.Value;

            if(me.Button == MouseButtons.Left)
            {
                foreach (TileSetDefinition tileSet in this.Session.TileSets)
                {
                    if (tileSet.TextureKey == this.TextureKey)
                    {
                        if ((tx >= tileSet.X) && (tx <= (tileSet.X + tileSet.Width)))
                        {
                            if ((ty >= tileSet.Y) && (ty <= (tileSet.Y + tileSet.Height)))
                            {
                                this.SelectedTileSet = tileSet;
                            }
                        }
                    }
                }
            }
            else
            {
                this.SelectedTileSet = null;
            }
            

            this.Refresh();
        }


        



    }
}
