using System.Collections;
using System.Collections.Generic;
using Assets._game.scripts;
using Rewired;
using UnityEngine;

public class Training : MonoBehaviour
{
	private Player player;

	private bool started;
	
	public GameObject infoScreen;

	public PlayerData playerData;

	public Camera camera;

	private void Awake()
	{
		player = new Player(playerData, 0);
	}

	private void Init()
	{
		// Create player model and controller
		var playerObject = new GameObject(string.Format("{0} - {1}", player.Data.Name, player.PlayerId));
		var rigidBody = playerObject.AddComponent<Rigidbody>();
		rigidBody.useGravity = false;
		rigidBody.constraints = RigidbodyConstraints.FreezeRotation;
		
		var model = Instantiate(player.Data.Model);
		model.transform.parent = playerObject.transform;
		model.transform.localPosition = Vector3.zero;
		
		var controller = playerObject.AddComponent<PlayerController>();
		controller.Init(player, model.GetComponent<Collider>());
		player.Controller = controller;
		
		// Make camera follow playerObject
		var trainingCamera = camera.GetComponent<TrainingCamera>();
		trainingCamera.cameraTransform = camera.transform;
		trainingCamera.followTarget = playerObject.transform;
		trainingCamera.moveSpeed = player.Data.MoveSpeed;
		
		// Hide info screen
		infoScreen.SetActive(false);

		started = true;
	}

	private void Reset()
	{
		infoScreen.SetActive(true);
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (ReInput.players.GetPlayer(0).GetButtonDown("Fire1") && !started)
		{
			Init();
		}
		
		if (ReInput.players.GetPlayer(0).GetButtonDown("Start") && started)
		{
			Reset();
		}
	}
}
