using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TribbleExtinction
{
    enum NodeColor
    {
        red,
        green,
        blue
    }
    public class Node
    {
        static Texture2D node = null;
        SoundEffect nodeSound;
        Vector2 position;
        float dist2;
        float store;
        float rate;
        int active;

        NodeColor type;

        public Node()
        {
            if (node == null)
                node = Program.masterGame.Content.Load<Texture2D>("Node");
            bool GoodPosition = false;
            nodeSound = Program.masterGame.Content.Load<SoundEffect>("ResourceBeamLoop");
            
            while (!GoodPosition)
            {
                GoodPosition = true;
                position = new Vector2((float)Program.game.rand.NextDouble() * 896, (float)Program.game.rand.NextDouble() * 640);
                foreach (Node n in Program.game.NodeList)
                    if ((position - n.position).LengthSquared() < 4096) //64 ^2
                        GoodPosition = false;
                if (position.X < 200 && position.Y < 200)
                    GoodPosition = false;
            }

            dist2 = (float)Program.game.rand.NextDouble() * 24 + 24;
            dist2 = dist2 * dist2;
            store = (float)Program.game.rand.NextDouble() * 6f + 8f;
            rate = (float)Program.game.rand.NextDouble() * 2.0f + store / 2f;
            type = (NodeColor)Program.game.rand.Next(0, 3);
        }

        public void update(GameTime gameTime)
        {
            Vector2 distPlayer = Program.game.myPlayer.dronePos - position;

            if (distPlayer.LengthSquared() < dist2)
            {
                Player play = Program.game.myPlayer;
                nodeSound.Play(0.2f, ((float)Program.game.rand.NextDouble() + 4.0f) / 5.0f, 0.0f);
                if (store > 0)
                {
                    if (type == NodeColor.blue)
                        play.ResourceBlue += (float)gameTime.ElapsedGameTime.TotalSeconds * rate;
                    else if (type == NodeColor.green)
                        play.ResourceGreen += (float)gameTime.ElapsedGameTime.TotalSeconds * rate;
                    else if (type == NodeColor.red)
                        play.ResourceRed += (float)gameTime.ElapsedGameTime.TotalSeconds * rate;
                    store -= (float)gameTime.ElapsedGameTime.TotalSeconds * rate;
                }
                active++;
            }
            else active = 0;

            if (store <= 0)
            {
                position = new Vector2((float)Program.game.rand.NextDouble() * 896, (float)Program.game.rand.NextDouble() * 640);
                store = (float)Program.game.rand.NextDouble() * 6f + 8f;
                rate = (float)Program.game.rand.NextDouble() * 2.0f + store / 2f;
                type = (NodeColor)Program.game.rand.Next(0, 3);
                Program.game.effectLibrary.CreateDustPuffs(position);
            }
        }

        public void render(SpriteBatch spriteBatch, GameTime cTime)
        {
            Color color = Color.Black;
            int value = (int)(store * 40f);
            byte data = (byte)value;

            if (value > 255)
                data = 255;
            else if (value < 0)
                data = 0;

            if ((active & 4) != 4)
            {
                if (type == NodeColor.red) color.R = data;
                else if (type == NodeColor.blue) color.B = data;
                else if (type == NodeColor.green) color.G = data;
            }
            else
            {
                color.R = (byte)Program.game.rand.Next(32, data);
                color.G = (byte)Program.game.rand.Next(32, data);
                color.B = (byte)Program.game.rand.Next(32, data);
            }

            spriteBatch.Draw(node, position, null, color, 0, new Vector2(node.Width / 2, node.Height / 2), 1.0f, SpriteEffects.None, 0);
        }
    }
}
