using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
using System;
using UnityEngine.Audio;

public class MotherDrone : MonoBehaviour
{
    public AudioSource pew;

    public GameObject gun;
    //  public Ammo AmmoPrefab;
    public float bulletVelocity;

    public float verticalInputAcceleration = 5;
    public float horizontalInputAcceleration = 20;

    public float maxSpeed = 100;
    public float maxRotationSpeed = 100;

    public float velocityDrag = 1;
    public float rotationDrag = 1;

    private Vector3 velocity;
    private float zRotationVelocity;
    public float ShootingThrontle = 1f;

    void Start()
    {
        Gm = FindObjectOfType<GM>();
        if (gun == null)
        {
            gun = gameObject.transform.GetChild(0).gameObject;
        }

        Observable.EveryUpdate().Where(x => Input.GetButtonDown("Fire1")).Select(x => Unit.Default).NextTo(Shooting as ISubject<Unit>);

        Shooting.ThrottleFirst(TimeSpan.FromSeconds(ShootingThrontle)).Subscribe(x =>
        {
            Shoot(Input.mousePosition);
        }).AddTo(this);


    }
    DynamicSubject<Unit> Shooting = new DynamicSubject<Unit>();
    private void Update()
    {
        // apply forward input
        Vector3 acceleration = Input.GetAxis("Vertical") * verticalInputAcceleration * transform.up;
        velocity += acceleration * Time.deltaTime;

        // apply turn input
        float zTurnAcceleration = -1 * Input.GetAxis("Horizontal") * horizontalInputAcceleration;
        zRotationVelocity += zTurnAcceleration * Time.deltaTime;

        if (Input.GetButtonDown("Fire1"))
        {
            // Shoot(Input.mousePosition);
        }

        LookTowardsMouse();
    }

    private void FixedUpdate()
    {
        // apply velocity drag
        velocity = velocity * (1 - Time.fixedDeltaTime * velocityDrag);

        // clamp to maxSpeed
        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);

        // apply rotation drag
        zRotationVelocity = zRotationVelocity * (1 - Time.fixedDeltaTime * rotationDrag);

        // clamp to maxRotationSpeed
        zRotationVelocity = Mathf.Clamp(zRotationVelocity, -maxRotationSpeed, maxRotationSpeed);

        // update transform
        transform.position += velocity * Time.fixedDeltaTime;
        transform.Rotate(0, 0, zRotationVelocity * Time.fixedDeltaTime);
    }

    void LookTowardsMouse()
    {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3 perpendicular = Vector3.Cross(gun.transform.position - mousePos, Vector3.forward);
        gun.transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular);
    }
    GM Gm;


    public float ShootingSpeed = 100f;
    public void Shoot(Vector2 mousePos)
    {
        pew.Play();
        var ammo = Gm.pool.Rent();
        CompositeDisposable disposable = new CompositeDisposable();
        ammo.transform.position = this.transform.position;
        Observable.Return(Unit.Default).Delay(System.TimeSpan.FromSeconds(10)).Subscribe(x =>
        {
            ammo.gameObject.SetActive(false);
            Gm.pool.Return(ammo);
            disposable.Dispose();
        }).AddTo(disposable);
        var angle = gun.transform.position.ToVector2().AngleBetweenVector2(mousePos);
        var direction = (angle + 70).DirectionFromAngle();

        Observable.EveryUpdate().Subscribe(x =>
        {
            ammo.transform.position += direction * Time.deltaTime * ShootingSpeed;
        }).AddTo(disposable);
    }
}
public class AmmoPool : SimplePool<Ammo>
{
    public AmmoPool(Ammo prefab) : base(prefab, "ammo")
    {

    }
}
