using ActionGame.RawResources.Models;
using GameMaker.Models;
using System;
using System.Drawing;
using System.Windows.Forms;


namespace GameMaker.UI
{
    public partial class WorldPreviewPanel : Panel
    {
        public WorldDefinition World { get; set; }
        public Int32 CellSize { get; set; }
        public Boolean AllowClicks { get; set; }
        public WorldCellDefinition SelectedCell { get; set; }
        public WorldCellEditPanel WorldCellEditPanel { get; set; }
        public ToolStripComboBox SongSelectComobBox { get; set; }


        public WorldPreviewPanel()
        {
            InitializeComponent();

            this.SelectedCell = null;
            this.AllowClicks = true;
            this.CellSize = 32;
            this.DoubleBuffered = true;
            this.BackColor = Color.DarkGray;

            this.AutoScrollMinSize = new Size(this.CellSize * 32, this.CellSize * 32);
        }

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

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

            if (this.World == null) return;

            Brush brush = new SolidBrush(Color.FromArgb(160, 0, 160));
            Brush selectedBrush = new SolidBrush(Color.FromArgb(192, 0, 192));
            Pen pen = new Pen(Color.FromArgb(224, 0, 224));
            Pen selectPen = new Pen(Color.FromArgb(255, 0, 0));

            Int32 x = 0;
            Int32 y = 0;
            Int32 w = this.CellSize;
            Int32 h = this.CellSize;

            foreach(WorldCellDefinition cell in this.World.Cells)
            {
                x = cell.WorldX * w;
                y = cell.WorldY * h;

                if(cell == this.SelectedCell) e.Graphics.FillRectangle(selectedBrush, x, y, w, h);
                else e.Graphics.FillRectangle(brush, x, y, w, h);

                e.Graphics.DrawRectangle(pen, x, y, w, h);
            }

            if (this.SelectedCell != null)
            {
                if (this.World.Cells.Contains(this.SelectedCell) == false)
                {
                    x = this.SelectedCell.WorldX * w;
                    y = this.SelectedCell.WorldY * h;

                    e.Graphics.DrawRectangle(selectPen, x, y, w, h);
                }
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (this.AllowClicks == false) return;
            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(this.WorldCellEditPanel != null) this.WorldCellEditPanel.WorldCell = null;
            this.SelectedCell = null;
            foreach (WorldCellDefinition cell in this.World.Cells)
            {
                Int32 cx = cell.WorldX * this.CellSize;
                Int32 cy = cell.WorldY * this.CellSize;

                if ((tx >= cx) && (tx <= (cx + this.CellSize)))
                {
                    if ((ty >= cy) && (ty <= (cy + this.CellSize)))
                    {
                        this.SelectedCell = cell;

                        if (this.WorldCellEditPanel != null)
                        {
                            this.WorldCellEditPanel.WorldCell = this.SelectedCell;
                            this.WorldCellEditPanel.Refresh();
                        }
                    }
                }
            }

            if(this.SelectedCell == null)
            {
                this.SelectedCell = new WorldCellDefinition()
                {
                    WorldX = tx / this.CellSize,
                    WorldY = ty / this.CellSize
                };
            }

            if(this.SongSelectComobBox != null)
            {
                foreach(Object obj in this.SongSelectComobBox.Items)
                {
                    RawResource song = (RawResource)obj;
                    if(song.Key == this.SelectedCell.SongKey)
                    {
                        this.SongSelectComobBox.SelectedItem = obj;
                    }

                }
            }
            

            this.Refresh();
        }



    }
}
