using System;
using UnityEngine;

public sealed class Vehicle
{
    public readonly VehicleBlueprint Blueprint;
    public Vector3 Offset { get; private set; }
    private int lives;

    private float fireTime = 0;

    public bool IsAlive => lives > 0;

    public Vehicle(VehicleBlueprint blueprint, float randomTiming)
    {
        Blueprint = blueprint;
        lives = Blueprint.Lives;
        fireTime = -blueprint.ReloadTime * randomTiming;
    }

    public void SetOffset(Vector3 baseOffset) {
        Offset = baseOffset + Blueprint.Offset;
    }

    public bool Reload(float reloadTime)
    {
        if (lives <= 0)
            return false;
        if (fireTime < 0)
            fireTime = reloadTime - fireTime; // hack for random offset

        if (fireTime > reloadTime)
            return false;
        fireTime += Blueprint.ReloadTime;
        return true;
    }

    public BulletBlueprint FireBullet()
    {
        return Blueprint.Bullet;
    }

    public void Damage(BulletBlueprint bullet)
    {
        if (UnityEngine.Random.Range(0f, 1f) < bullet.Accuracy)
            return;

        lives -= bullet.Damage;
    }

    public void Restore(int count)
    {
        //if (lives <= 0)
        //    return;
        //if (lives == Blueprint.Lives)
        //    return;
        //lives = Mathf.Min(lives + count, Blueprint.Lives);
    }
}