using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(Removable))]
public class Tank : MonoBehaviour {

    [SerializeField] private List<WheelCollider> _leftWhels;
    [SerializeField] private List<WheelCollider> _rightWhels;
    [SerializeField] private Transform _centerOfMass;

    [SerializeField] private GameObject _turret;
    [SerializeField] private Transform _turretEnd;

    [SerializeField] private float _maxRPM = 300;
    [SerializeField] private float _maxTorque = 150;
    [SerializeField] private float _brake = 50;
    [SerializeField] private float _rotationSpeed = 10;

    [SerializeField] private GameObject _shell;
    [SerializeField] private float _shootTime = 4;
    [SerializeField] public int lives = 1;
    private float _lastShootTime;
    private Transform _transform;
    private Removable _remover;
    private AudioSource _audioSource;
 

    public int fraction;

	void Start () {
        rigidbody.centerOfMass = _centerOfMass.localPosition;
        _lastShootTime = -_shootTime;
        _transform = rigidbody.transform;
        _remover = GetComponent<Removable>();
        _audioSource = GetComponent<AudioSource>();
	}

    public void Hit()
    {
        lives -= 1;
        if (fraction == 1)
            (FindObjectOfType(typeof(GameModel)) as GameModel).lives = lives;

        if (lives == -1)
        {
            _remover.Remove();
        }
    }

    public void Shoot()
    {
        if (Time.time - _lastShootTime > _shootTime)
        {
            var bullet = Instantiate(_shell, _turretEnd.position, _turretEnd.rotation) as GameObject;
            bullet.GetComponent<Bullet>().fraction = fraction;
            _lastShootTime = Time.time;
            _audioSource.Play();
        }
    }


    public void RotateTover(float angle, float dt)
    {
      /*  var dir = _turret.transform.rotation * Vector3.left;
        var pdir = new Vector2(Vector3.Project(dir, Vector3.left).x, Vector3.Project(dir, Vector3.forward).z);
        pdir.Normalize();
     
     
        var rotation = _turret.transform.localRotation.eulerAngles;
        rotation.y = Mathf.MoveTowardsAngle(rotation.y, angle - Mathf.Repeat((Mathf.Atan2(pdir.x, pdir.y) * Mathf.Rad2Deg + 90), 360f) + rotation.y, dt * _rotationSpeed);
       Debug.Log(_transform.rotation.eulerAngles.y + "asd" );
       _turret.transform.localRotation = Quaternion.Euler(rotation);

      */
        var rotation = _turret.transform.rotation.eulerAngles;
        rotation.x = 90;
        rotation.z = 0;
        rotation.y =  Mathf.MoveTowardsAngle(rotation.y, angle, dt * _rotationSpeed);
        _turret.transform.rotation = Quaternion.Euler(rotation);


    }

    public void Move(Vector2 dir, float dt)
    {
        
        float leftaccel = (dir.y + dir.x * 2.0f) * _maxRPM;
        float rightaccel = (dir.y - dir.x * 2.0f) * _maxRPM;
        float brake = _brake;

        foreach (var whell in _leftWhels)
        {
            whell.brakeTorque = brake;
            whell.motorTorque = Mathf.Lerp(-_maxTorque, _maxTorque, Mathf.Clamp((leftaccel - whell.rpm) / (_maxRPM) + 0.5f, 0.0f, 1.0f)) * dir.magnitude;
        }

        foreach (var whell in _rightWhels)
        {
            whell.brakeTorque = brake;
            whell.motorTorque = Mathf.Lerp(-_maxTorque, _maxTorque, Mathf.Clamp((rightaccel - whell.rpm) / (_maxRPM) + 0.5f, 0.0f, 1.0f)) * dir.magnitude;
        }

        /*
        transform.rotation = Quaternion.Euler(new Vector3(
        Mathf.Clamp(transform.rotation.eulerAngles.x, -15f, 15f),
        transform.rotation.eulerAngles.y,
         Mathf.Clamp(transform.rotation.eulerAngles.z, -15f, 15f)));
        */
        var correctAt = 10f;
        var rotCorrect = 60f * dt;

        if ((transform.eulerAngles.x >= correctAt) && (transform.eulerAngles.x < 180))
        {
            transform.Rotate(-rotCorrect, 0, 0);
        }
        if ((transform.eulerAngles.x <= (360 - correctAt)) && (transform.eulerAngles.x >= 180))
        {
            transform.Rotate(rotCorrect, 0, 0);
        }
        if ((transform.eulerAngles.z >= correctAt) && (transform.eulerAngles.z < 180))
        {
            transform.Rotate(0, 0, -rotCorrect);
        }
        if ((transform.eulerAngles.z <= (360 - correctAt)) && (transform.eulerAngles.z >= 180))
        {
            transform.Rotate(0, 0, rotCorrect);
        }

    }


}
