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

namespace Slayter
{
    public partial class Map : Background, IDrawable, IClickable
    {
        Texture2D pictureOfHero;
        public Texture2D PictureOfHero { get { return pictureOfHero; } }


        int[] last=new int[2] {0,0};
        Texture2D arrRight;
        Texture2D arrDown;
        Texture2D Ex;

        int Frame;
        int MaxFrames;

        public List<int[]> WalkingPath;
        public List<int[]> DrawingPath;
        Hero hero;

        public void MovingOfHeroLoad(Game1 game,Hero h)
        {
            Ex = game.Content.Load<Texture2D>("hikso");
            hero = h;
            arrRight = game.Content.Load<Texture2D>("arr");
            arrDown = game.Content.Load<Texture2D>("arr-down");
            pictureOfHero = game.Content.Load<Texture2D>("hero");
            Frame = 0;
            WalkingPath = new List<int[]>();
            DrawingPath = new List<int[]>();
            MaxFrames = (pictureOfHero.Width / 40) * (pictureOfHero.Height / 40) - 1;
        }

        public void drawHero(SpriteBatch spriteBatch)
        {
            SpriteEffects effects = SpriteEffects.None;
            Rectangle frame = GetRectangleForFrame();
            UpdateCoordinatesOfMapandHero();
            Rectangle heroRec= new Rectangle(hero.CurrentX - TopX, hero.CurrentY - TopY, 40, 40);
            drawPath(spriteBatch);
            if (Rect.Intersects(heroRec))
            {
                Rectangle r = Rectangle.Intersect(heroRec,Rect);
                if (r.Y + r.Height == 480)
                {
                    frame.Height = r.Height;
                    heroRec.Height = r.Height;
                }
                if (DrawingPath.Count != 0 && hero.CurrentX > DrawingPath[0][0]*40)
                {
                    effects = SpriteEffects.FlipHorizontally;
                }
                spriteBatch.Draw(PictureOfHero, heroRec, frame, Color.White, 0, new Vector2(0, 0), effects, 0);
            }
        }

        private void drawPath(SpriteBatch spriteBatch)
        {
            Color color=Color.White;
            int heroMoves = hero.leftForTheDay;
            Texture2D tex = arrRight;
            SpriteEffects effects = SpriteEffects.None;
            if (DrawingPath.Count != 0)
            {
                float rot;
                for (int i = 0; i < DrawingPath.Count; i++)
                {
                    heroMoves -= (map[DrawingPath[i][1], DrawingPath[i][0]]+1);
                    if (heroMoves < 0)
                    {
                        color = Color.Red;
                    }
                    Rectangle arrow=new Rectangle(0, 0, tex.Width, tex.Height);
                    Rectangle position=new Rectangle(DrawingPath[i][0] * 40 + (40 - tex.Width) / 2 - TopX, DrawingPath[i][1] * 40 + (40 - tex.Height) / 2 - TopY, 20, 20);
                    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 = arrDown;
                        effects = SpriteEffects.FlipVertically;
                    }
                    rot = 0;
                    if (i == DrawingPath.Count - 1)
                    {
                        tex = Ex;
                        effects = SpriteEffects.None;
                    }
                    if (Rect.Intersects(position))
                    {
                        Rectangle r = Rectangle.Intersect(position, Rect);
                        if (r.Y + r.Height == 480)
                        {
                            arrow.Height = r.Height;
                            position.Height = r.Height;
                        }
                        spriteBatch.Draw(tex, position, arrow, color, rot, new Vector2(0, 0), effects, 0);
                    }
                    effects = SpriteEffects.None;
                    tex = arrRight;
                }
            }
        }

        private void UpdateCoordinatesOfMapandHero()
        {
            if (WalkingPath.Count != 0)
            {
                hero.UpdateCurrent(WalkingPath.ElementAt<int[]>(0));
            }
        }

        private Rectangle GetRectangleForFrame()
        {
            Rectangle frame;
            if (WalkingPath.Count == 0 || !hero.heroCanMove)
                frame = getFrame(0);
            else
            {
                frame = getFrame(nextFrame());
                if (CheckPath(WalkingPath.ElementAt<int[]>(0)))
                {
                    if (!FirstClick)
                    {
                        DrawingPath.RemoveAt(0);
                        hero.hasMoved(map[WalkingPath[0][1], WalkingPath[0][0]]);
                    }
                    last=WalkingPath[0];
                    WalkingPath.RemoveAt(0);
                    CheckMovement();
                    
                }
            }
            return frame;
        }


        public Rectangle getFrame(int f)
        {
            int row = f / (pictureOfHero.Width / 40);
            int col = f % (pictureOfHero.Width / 40);
            return new Rectangle(col * 40, row * 40, 40, 40);
        }

        public int nextFrame()
        {
            if (Frame == MaxFrames)
            {
                Frame = -1;
            }
            Frame++;
            return Frame;
        }

        private bool CheckPath(int[] coordinates)
        {
            return coordinates[0] * 40 == hero.CurrentX && coordinates[1] * 40 == hero.CurrentY;
        }

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