using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameScript : MonoBehaviour {

    public static int level = 0;
    public GameObject ball;
    GameObject newBall;
    public Text levelLabel;

    // Use this for initialization
    void Start()
    {
        levelLabel.GetComponent<RectTransform>().localPosition = new Vector2(0, -180);
        LeanTween.move(levelLabel.GetComponent<RectTransform>(), new Vector2(0, 0), 1.0f).setEase(LeanTweenType.easeOutElastic).setOnComplete(AnimationDone);
    }

    // Update is called once per frame
    void Update()
    {

        // get mouse position in world coordinates
        Vector2 screenMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        // on mouse down, make a ball at the mouse position
        if (Input.GetMouseButtonDown(0))
        {
            newBall = Instantiate(ball);
            newBall.transform.position = new Vector2(screenMousePosition.x, screenMousePosition.y);
        }

        // on mouse up, shoot it
        if (Input.GetMouseButtonUp(0))
        {
            newBall.GetComponent<BallScript>().MoveUsingPoint(screenMousePosition);
        }

        // see if won
        GameObject[] blocksLeft = GameObject.FindGameObjectsWithTag("Box");
        if (blocksLeft.Length == 0)
        {
            GameScript.level++;
            SceneManager.LoadScene("Level-" + GameScript.level);
        }
    }

    public void AnimationDone() {
        LeanTween.textAlpha(levelLabel.GetComponent<RectTransform>(), 0f, 1.0f);

    }
}
