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

namespace Slayter
{
    public class ScreenOrigin
    {
        protected List<IDrawable> drawables;
        public Game1 parent;
        public List<IDrawable> Drawables { get { return drawables; } }
        protected List<IClickable> clickables;

        public ScreenOrigin commingFrom;

        public ScreenOrigin(Game1 game)
        {
            parent = game;
            drawables = new List<IDrawable>();
            clickables = new List<IClickable>();
        }

        public void addDrawable(IDrawable draw)
        {
            drawables.Add(draw);
        }

        public void addClickable(IClickable click)
        {
            clickables.Add(click);
        }

        public virtual void draw(Game1 game, SpriteBatch spriteBatch)
        {
            foreach (IDrawable element in drawables)
            {
                element.draw(spriteBatch);
            }
        }

        protected void addToLists(ClickableObject b)
        {
            drawables.Add(b);
            clickables.Add(b);
        }

        protected void removeFromLists(ClickableObject b)
        {
            if (drawables.Contains(b))
            {
                drawables.Remove(b);
            }
            if (clickables.Contains(b))
            {
                clickables.Remove(b);
            }
        }

        public virtual void click(Game1 game, MouseState state)
        {
            CheckForClickableObject(game, state,clickables);
        }

        protected bool CheckForClickableObject(Game1 game, MouseState state,List<IClickable> list)
        {
            if (state.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed &&
                game.oldstate.LeftButton != Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                //Console.Out.WriteLine("here");
                foreach (IClickable element in list)
                {
                    if (element.isClicked(state))
                    {
                        game.oldstate = state;
                        element.Click(state);
                        return true;
                    }
                }
            }
            if (state.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released &&
                game.oldstate.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                game.oldstate = state;
                return true;
            }
            return false;
        }
    }
}
