using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using HumanLikeBattleRobots.Control;
using HumanLikeBattleRobots.Graphic;

namespace HumanLikeBattleRobots.Logic
{
    class PlayerRobot: Robot
    {
        private Vec2f cursorPosition = new Vec2f(0, 0);
        private bool leftFiring = false;
        private bool rightFiring = false;

        public delegate void OnMoveHandler(Vec2f position);
        public OnMoveHandler OnMove;

        public PlayerRobot (Vec2f position) : base(position) {}

        public void KeyReaction()
        {
            this.speedDirection = 0;
            this.rotationDirection = 0;

            if (Keyboard.KeyIsDown(Keys.S) || Keyboard.KeyIsDown(Keys.Down))
                speedDirection += 1;

            if (Keyboard.KeyIsDown(Keys.W) || Keyboard.KeyIsDown(Keys.Up))
                speedDirection -= 1;

            if (Keyboard.KeyIsDown(Keys.A) || Keyboard.KeyIsDown(Keys.Left))
                this.rotationDirection -= 1;
            if (Keyboard.KeyIsDown(Keys.D) || Keyboard.KeyIsDown(Keys.Right))
                this.rotationDirection += 1;
        }

        public void MouseMoveReaction(Vec2f cursorPosition, Dictionary<MouseButtons, bool> buttonDown)
        {
            this.cursorPosition = cursorPosition;
        }

        public void MouseDownReaction(Vec2f cursorPosition, Dictionary<MouseButtons, bool> buttonDown)
        {
            leftFiring = buttonDown[MouseButtons.Left];
            rightFiring = buttonDown[MouseButtons.Right];
        }

        public override void Proceed(double delta)
        {
            base.Proceed(delta);
            
            Vec2f smokePosition = legs.Normal * -50;
            new Smoke(this.position + smokePosition);

            shadow.Position = this.position;

            base.RotateTower(cursorPosition + GameManager.camera);
            if (leftFiring)
                base.Fire(cursorPosition + GameManager.camera, true);
            if (rightFiring)
                base.Fire(cursorPosition + GameManager.camera, false);

            if (OnMove != null)
                OnMove(this.Position);
            
        }

        protected override void SetParams()
        {
            float angle = 0;

            health = 10;
            //forwardSpeed = 320;
            //backwardSpeed = 240;
            //forwardAngularVelocity = 1;
            //backwardAngularVelocity = 0.75f;
            forwardSpeed = 384;
            backwardSpeed = 288;
            forwardAngularVelocity = 1.2f;
            backwardAngularVelocity = 0.9f;
            size = 45;
            legs = new MyLegs(this.position, angle, sprites);
            tower = new MyTower(this.position, angle, sprites);
            body = new MyRobotHullSprite(2);
            shadow = new RobotShadowSprite(0);
            sprites[body.Stage - 1].Add(body);
        }

        public void SetSpeedBonus()
        {
            (tower as MyTower).SetSpeedBonus();
        }
    }
}
