using UnityEngine;
using UnityEngine.UI;
using System;

public class PlayerController : MonoBehaviour {

    public float speed;
    public Rigidbody rigidBody;
    public Text countText;
    public Text winText;
    private int score;
    private int numOfPickups;
    public String pickupTag;

    private void Start()
    {
        score = 0;
        rigidBody = GetComponent<Rigidbody>();
        SetCountText();
        winText.text = "";
        numOfPickups = countPickUps();
    }

    private int countPickUps() {
      return GameObject.FindGameObjectsWithTag(pickupTag).Length;
    }

    private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
        rigidBody.AddForce(movement * speed);
    }

    void OnTriggerEnter(Collider other) {
      GameObject otherObject = other.gameObject;
      if (otherObject.CompareTag(pickupTag)) {
        otherObject.SetActive(false);
        score++;
        SetCountText();
      }
    }

    void SetCountText() {
      countText.text = "Score: " + score.ToString();
      if (score >= numOfPickups) {
        winText.text = "You Win!";
      }
    }
}