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 enum BonusType
    {
        Bunny,
        Bonus
    };

    public class Bonus
    {
        static Texture2D bunnyTexture;
        static Texture2D bonusTexture;

        const int SCORE = 100;
        const int BONUS = 250;
        const int BONUS_TIME = 25;

        Point coord;
        int cellSize;
        bool alive = true;
        int time;

        BonusType type;

        public BonusType Type
        {
            get { return type; }
        }

        public bool Alive
        {
            get { return alive; }
        }

        public Point Coord
        {
            get { return coord; }
        }

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

        public static void Load(ContentManager content)
        {
            bunnyTexture = content.Load<Texture2D>("bunny");
            bonusTexture = content.Load<Texture2D>("bonus");
        }

        public void Eat(Snake snake)
        {
            switch (type)
            {
                case BonusType.Bunny:
                    snake.Score += SCORE;
                    snake.Grow();
                    break;
                case BonusType.Bonus:
                    snake.Score += BONUS;
                    break;
            }
            alive = false;
        }

        public void Spawn(Point coord, BonusType type = BonusType.Bunny)
        {
            this.coord = coord;
            this.type = type;
            time = 0;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle rect = new Rectangle(coord.X * cellSize, coord.Y * cellSize, cellSize, cellSize);
            Texture2D texture = null;
            switch (type)
            {
                case BonusType.Bunny:
                    texture = bunnyTexture;
                    break;
                case BonusType.Bonus:
                    texture = bonusTexture;
                    break;
            }
            spriteBatch.Draw(texture, rect, Color.White);
        }

        public void Update(GameTime gameTime)
        {
            switch (type)
            {
                case BonusType.Bonus:
                    time += gameTime.ElapsedGameTime.Milliseconds;
                    if (time >= BONUS_TIME * 1000)
                        type = BonusType.Bunny;
                    break;
            }
        }

        public void Transform()
        {
            type = BonusType.Bonus;
            time = 0;
        }

    }
}
