using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace Game200
{
    public class Source
    {
        public Vec position;
        public Texture2D image;
        public Color color;
        public int diam;
        public bool isFixed;
        public int rayCount;
        public Ray[] rays;

        public Source(Vec position, Color color)
        {
            this.position = position;
            image = C.LoadImage("disc");
            this.color = color;
            diam = 10;
            isFixed = false;
            rayCount = 1001;
            rays = new Ray[rayCount];
            for (int i = 0; i < rayCount; i++)
                rays[i] = new Ray();
        }

        public void ShedLight(Map map)
        {
            if (!isFixed)
            {
                for (int i = 0; i < map.dim.X; i++)
                    for (int j = 0; j < map.dim.Y; j++)
                    {
                        if (map.x[i, j].isWallCopy)
                            map.ShineRay(this, (float)Math.Atan2(i - position.X, j - position.Y + 0.5));
                        if (map.y[i, j].isWallCopy)
                            map.ShineRay(this, (float)Math.Atan2(i - position.X + 0.5, j - position.Y));
                    }
                /*for (int i = 0; i < rayCount; i++)
                {
                    float angle = MathHelper.TwoPi / rayCount * i;
                    float dist = C.length * map.ShineRay(this, angle);
                    rays[i].Update(position, dist, angle, color);
                }*/
            }
        }

        public void Update()
        {
            if (!isFixed)
            {
                position = C.MousePos();
                if (C.IsMousePressed())
                    isFixed = true;
            }
        }

        public void DrawRays()
        {
            //if (!isFixed)
            //    foreach (Ray r in rays)
            //        r.Draw();
        }

        public void Draw()
        {
            C.SimpleDraw(image, position, color, (float)diam / image.Width);
        }
    }
}
