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

namespace Snake
{
    public class Snake
    {
        Point head = new Point();
        List<Point> tail = new List<Point>();
        static Texture2D texture;
        int cellSize;
        int score;

        public Point Head
        {
            get { return head; }
            set { head = value; }
        }

        public int Length
        {
            get { return tail.Count; }
        }

        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        public Snake(int cellSize)
        {
            this.cellSize = cellSize;
        }

        public static void Load(ContentManager content)
        {
            texture = content.Load<Texture2D>("snake");
        }

        public void Spawn(Point start)
        {
            tail.Clear();
            head = start;
            score = 0;
        }

        public void Move(Direction dir)
        {
            // Перемещаем все ячейки хвоста на место предыдущего элемента
            for (int i = tail.Count - 1; i > 0; --i)
            {
                tail[i] = tail[i - 1];
            }
            // Первая ячейка - на место гдей сейчас голова
            if (tail.Count > 0)
            {
                tail[0] = head;
            }
            // Перемещаем голову вперёд
            head = NextMove(dir);
        }

        public void Grow()
        {
            tail.Add(Point.Zero);
            for (int i = tail.Count - 1; i > 0; --i)
            {
                tail[i] = tail[i - 1];
            }
            tail[0] = head;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle rect = new Rectangle(head.X * cellSize, head.Y * cellSize, cellSize, cellSize);
            // Рисуем голову
            spriteBatch.Draw(texture, rect, Color.White);
            // Рисуем хвост
            foreach (Point point in tail)
            {
                rect = new Rectangle(point.X * cellSize, point.Y * cellSize, cellSize, cellSize);
                spriteBatch.Draw(texture, rect, Color.White);
            }
        }

        public bool CheckSuicide(Point next)
        {
            foreach (Point it in tail)
            {
                if (it == next)
                    return true;
            }
            return false;
        }

        public Point NextMove(Direction dir)
        {
            Point result = head;
            switch (dir)
            {
                case Direction.Down:
                    result.Y += 1;
                    break;
                case Direction.Left:
                    result.X -= 1;
                    break;
                case Direction.Right:
                    result.X += 1;
                    break;
                case Direction.Up:
                    result.Y -= 1;
                    break;
            }
            if (result.X < 0)
                result.X = Game1.WIDTH - 1;
            if (result.X >= Game1.WIDTH)
                result.X = 0;
            if (result.Y < 0)
                result.Y = Game1.HEIGHT - 1;
            if (result.Y >= Game1.HEIGHT)
                result.Y = 0;
            return result;
        }
    }
}
