using System;
using System.Collections.Generic;
using System.Text;
using HumanLikeBattleRobots.Graphic;

namespace HumanLikeBattleRobots.Logic
{
    abstract class Tower //несет две пушки
    {
        protected Vec2f position = new Vec2f(0, 0);
        protected Vec2f normal = new Vec2f(0, 0);
        private Vec2f aimVector = new Vec2f(0, 0);
        private float angularVelocity = 0;
        protected Sprite towerSprite;
        protected float health;

        //пушки
        protected Weapon w1;
        protected Weapon w2;
        protected float w1Radius;
        protected float w2Radius;

        public Vec2f Position { get { return position; } set { position = value; } }
        public Vec2f Normal { get { return normal; } set { normal = value; } }

        public Tower(Vec2f position, float angle, List<List<Sprite>> sprites)
        {
            this.position = position;
            normal = new Vec2f(angle);
            aimVector = normal;

            SetParams();

            sprites[towerSprite.Stage - 1].Add(towerSprite);
            w1.SendToSprites(sprites);
            w2.SendToSprites(sprites);
        }

        protected abstract void SetParams();

        public void Rotate(Vec2f aimPosition)
        {
            aimVector = aimPosition - position;
            aimVector.Normalize();
        }

        public void Fire(Vec2f aimPosition, bool left)
        {
            if (left)
                w1.Fire(aimPosition);
            else
                w2.Fire(aimPosition);
        }

        public void TakeDamage(float damage)
        {
            health -= damage;
        }

        public virtual void Proceed(double delta)
        {
            if (health > 0)
            {
                angularVelocity = Math.Sign(-(aimVector.X * normal.Y - aimVector.Y * normal.X))*1.05f;
                normal = normal.Rotate(angularVelocity * delta * 1.5f);//* скорость

                //if (normal.AngleBetween(aimVector) < Math.PI / 96)
                    normal = aimVector.Normalize();
            }
            // это для спрайта
            towerSprite.Position = this.position;
            towerSprite.Angle = this.normal.Angle360();

            

            w1.Position = this.position - normal.Rotate(Math.PI / 2) * w1Radius;
            w1.Normal = this.normal;
            w1.Proceed(delta);

            w2.Position = this.position + normal.Rotate(Math.PI / 2) * w2Radius;
            w2.Normal = this.normal;
            w2.Proceed(delta);
        }
    }
}
