using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Slayter
{
    public class MiniMap:IClickable,IDrawable
    {
        Map map;
        Rectangle position;

        double miniMapRatio;
        double markerRatio;

        Texture2D marker;
        Rectangle rectMarker;

        public MiniMap(Map m,Rectangle r)
        {
            map = m;
            position = r;
            miniMapRatio =(double) m.Picture.Width / r.Width;
            markerRatio =(double) m.Picture.Width / 640;
            marker = m.parent.parent.Content.Load<Texture2D>("Marker");
            rectMarker = new Rectangle(r.X,r.Y,(int)(marker.Width/markerRatio),(int)(marker.Height/markerRatio));
        }

        public bool isClicked(MouseState state)
        {
            return position.X <= state.X && (position.X + position.Width) >= state.X &&
                        position.Y <= state.Y && (position.Y + position.Height) >= state.Y;
        }

        public void Click(MouseState state)
        {
            map.TopX=(int)((state.X-position.X)*miniMapRatio-map.Rect.Width/2);
            map.TopY=(int)((state.Y -position.Y) * miniMapRatio-map.Rect.Height/2);
        }

        public void draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(map.Picture,position,Color.White);
            rectMarker.X =(int)(position.X + map.TopX / miniMapRatio);
            rectMarker.Y =(int)(position.Y + map.TopY / miniMapRatio);
            spriteBatch.Draw(marker, rectMarker, Color.White);
        }
    }
}
