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

namespace Slayter
{
    public class Path:MapObject,IDrawable
    {

        Texture2D arrRight;
        Texture2D arrDown;
        Texture2D arrUp;
        Texture2D Ex;

        public List<int[]> WalkingPath;
        public List<int[]> DrawingPath;
        public bool movesToObject;
        Hero hero;
        public Path(Texture2D t, Rectangle r, Map map, Game1 game,Hero hero)
            :base(t,r,map)
        {
            movesToObject = false;
            this.hero = hero;
            Ex = game.Content.Load<Texture2D>("x");
            arrRight = game.Content.Load<Texture2D>("arr");
            arrDown = game.Content.Load<Texture2D>("arr-down");
            arrUp = game.Content.Load<Texture2D>("arr-up");

            WalkingPath = new List<int[]>();
            DrawingPath = new List<int[]>();
        }

        internal void CheckMovement()
        {
            if (WalkingPath.Count != 0)
            {
                hero.AbleToMove(map.map[WalkingPath[0][1], WalkingPath[0][0]]);
            }
            else
            {
                hero.heroIsMoving = false;
            }
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            Color color = Color.White;
            int heroMoves = hero.leftForTheDay;
            Texture2D tex = arrRight;
            SpriteEffects effects = SpriteEffects.None;
            if (DrawingPath.Count != 0)
            {
                Rectangle arrow;
                float rot;
                for (int i = 0; i < DrawingPath.Count; i++)
                {
                    arrow = new Rectangle(0, 0, tex.Width, tex.Height);
                    heroMoves -= (map.map[DrawingPath[i][1], DrawingPath[i][0]] + 1);
                    if (heroMoves < 0)
                    {
                        color = Color.Red;
                    }
                    Rectangle position = new Rectangle(DrawingPath[i][0] * 40,
                       DrawingPath[i][1] * 40, 40, 40);
                    //Rectangle position = new Rectangle(DrawingPath[i][0] * 40,DrawingPath[i][1] * 40,40,40);
                    if (i != DrawingPath.Count - 1 && DrawingPath[i][0] > DrawingPath[i + 1][0])
                    {
                        effects = SpriteEffects.FlipHorizontally;
                    }
                    else if (i != DrawingPath.Count - 1 && DrawingPath[i][1] < DrawingPath[i + 1][1])
                    {
                        tex = arrDown;
                    }
                    else if (i != DrawingPath.Count - 1 && DrawingPath[i][1] > DrawingPath[i + 1][1])
                    {
                        tex = arrUp;
                        //effects = SpriteEffects.FlipVertically;
                    }
                    rot = 0;
                    if (i == DrawingPath.Count - 1)
                    {
                        tex = Ex;
                        effects = SpriteEffects.None;
                    }
                    if (map.getPosition(ref position, ref arrow))
                    {
                        spriteBatch.Draw(tex, position, arrow, color, rot, new Vector2(0, 0), effects, 0);
                    }
                    effects = SpriteEffects.None;
                    tex = arrRight;
                }
            }
        }
    }
}
