using System;
using System.Collections.Generic;
using System.Text;
using ECOT.Engine;
using Microsoft.Xna.Framework;
using ECOT.Game.Buildings;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace ECOT.Game.Tools
{
    public enum ToolType
    {
        Selection = 0,
        Construction
    }

    public delegate void ToolEventHandler(Tool tool);

    public abstract class Tool : IUpdatable
    {
        private Vector2 position = Vector2.Zero;
        private Vector2 oldPosition = Vector2.Zero;

        public abstract void Begin();
        public abstract void Over(Vector2 position);
        public abstract void Click(Vector2 position);
        public virtual void RightClick(Vector2 position)
        {
            GameController.Instance.CurrentTool = ToolType.Selection;
        }
        public abstract void End();
        public abstract ToolType Type
        { get; }

        public static event ToolEventHandler OnClick;

        #region IUpdatable Membres

        public bool Enabled
        {
            get { return true; }
            set { }
        }

        public void Update(GameTime gameTime)
        {
            float zoom = Kernel.Instance.Camera.Zoom;

            this.oldPosition = this.position;

            this.position.X = (int)((Kernel.Instance.Input.MousePosition.X + Kernel.Instance.Origin.X * zoom) / (64 * zoom));
            this.position.Y = (int)((Kernel.Instance.Input.MousePosition.Y + Kernel.Instance.Origin.Y * zoom) / (64 * zoom));

#if XBOX
            // Align the mouse position with the grid
            Vector2 mousePos = new Vector2(
                this.position.X * (64 * zoom) - Kernel.Instance.Origin.X * zoom,
                this.position.Y * (64 * zoom) - Kernel.Instance.Origin.Y * zoom);

            Kernel.Instance.Input.MousePosition = new Point(
                (int)mousePos.X,
                (int)mousePos.Y);
#endif

            // Click de la souris
            if (Kernel.Instance.Input.MouseFirstPressed)
            {
                this.Click(this.position);
                if (Tool.OnClick != null)
                    Tool.OnClick(this);
            }
            else if (Kernel.Instance.Input.MouseFirstRPressed)
            {
                this.RightClick(this.position);
            }
            // Hover de la souris
            else if (this.position != this.oldPosition)
                this.Over(this.position);

            // Si la souris se trouve sur la zone de menu, le curseur redevient une fleche
            /*if (Mouse.GetState().X > 780)
            {
                if (Kernel.Instance.Cursor.IsTextureConstruction)
                {
                    // Ne dessine plus toutes les aires de villes
                    GameRenderer.Instance.DrawCityAreas = false;
                    // Cache la barre de construciton
                    ControlPanel.Instance.ConstructionBar.Hide();

                    Kernel.Instance.Cursor.TextureCursor = Kernel.Instance.Medias.Get<Texture2D>(@"Textures\Common\Cursor");
                    Kernel.Instance.Cursor.Color = Color.White;
                    Kernel.Instance.Cursor.IsTextureConstruction = false;
                }
            }*/
        }

        #endregion

        public Vector2 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }
    }
}
