using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;

enum GameState
{
  START_MENU,
  GAME_RUNNING,
  GAME_FINISHED,
}

public class GameManager : MonoBehaviour
{

  public GameObject player;
  public GameObject targetPrefab;

  private GameState gameState;

  // Game
  public float gameDuration = 30.0f;
  private float gameTimeRemaining = 0;

  public int numberOfTargets = 3;
  public float targetSpawnDelay = 1.0f;
  public float targetMoveSpeed = 1.0f;
  public float targetWaitTime = 1.0f;

  public float targetSpawnMinDistance = 1.0f;
  public float targetSpawnMaxDistance = 1.0f;
  public float targetSpawnAngle = 90;

  public float targetMovePointMinDistance = 1.0f;
  public float targetMovePointMaxDistance = 1.0f;


  // Start is called once before the first execution of Update after the MonoBehaviour is created
  void Start()
  {
    StartCoroutine(SpawnTargetDelay(targetSpawnDelay));
  }

  // Update is called once per frame
  void Update()
  {
    switch (gameState) {
      case GameState.START_MENU:

        break;
      case GameState.GAME_RUNNING:
        gameTimeRemaining -= Time.deltaTime;

        if (gameTimeRemaining <= 0)
        {
          EndGame();
        }


        break;
      case GameState.GAME_FINISHED:

        break;
      default:
        break;
    }
  }

  private void StartGame()
  {
    gameState = GameState.GAME_RUNNING;
    gameTimeRemaining = gameDuration;

    for (int i = 0; i < numberOfTargets; i++)
    {
      SpawnTarget();
    }
  }


  private void EndGame()
  {
    gameState = GameState.GAME_FINISHED;
  }

  private IEnumerator SpawnTargetDelay(float delay)
  {
    yield return new WaitForSeconds(delay);

    SpawnTarget();

  }

  private void SpawnTarget()
  {
    //Determine target spawn position
    float spawnAngle = Random.Range(-targetSpawnAngle/2,targetSpawnAngle/2);
    float spawnDistance = Random.Range(targetSpawnMinDistance, targetSpawnMaxDistance);

    spawnAngle = spawnAngle * Mathf.Deg2Rad;


    Vector3 spawnPosition = new Vector3(
      player.transform.position.x + Mathf.Sin(spawnAngle) * spawnDistance,
      player.transform.position.y,
      player.transform.position.z + Mathf.Cos(spawnAngle) * spawnDistance
    );

    //Determine target target position
    float targetMovePointDistance = Random.Range(targetMovePointMinDistance, targetMovePointMaxDistance);
    float targetMovePointAngleX = Random.Range(0, 360) * Mathf.Deg2Rad; //affects y and z position
    float targetMovePointAngleY = Random.Range(0, 360) * Mathf.Deg2Rad; //affects x and z position
    float targetMovePointAngleZ = Random.Range(0, 360) * Mathf.Deg2Rad; //affects x and y position

    Vector3 targetMovePosition = new Vector3(
      spawnPosition.x,
      spawnPosition.y + Mathf.Sin(targetMovePointAngleX),
      spawnPosition.z
      );



    GameObject spawnedTarget = Instantiate(targetPrefab);
    spawnedTarget.transform.position = spawnPosition;

    Debug.Log("Spawn Target, distance: " + (player.transform.position - spawnedTarget.transform.position).magnitude);
  }
}
