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

public class TitleScreen : MonoBehaviour 
{
    [SerializeField] private CanvasScaler _scaler;
    [SerializeField] private Button _continueButton;
    private Vector2 _continueDir = -Vector2.one;

    

	// Use this for initialization
	void Start () {
        _continueButton.onClick.AddListener(GoToLevel1);
	}
	
	// Update is called once per frame
	void Update () 
    {
        Vector2 movement = _continueDir * Time.deltaTime * 1920 * 0.25f;
        Vector2 dest = _continueButton.image.rectTransform.anchoredPosition + movement;

        float leftBorder = dest.x - _continueButton.image.rectTransform.sizeDelta.x * 0.5f;
        float rightBorder = dest.x + _continueButton.image.rectTransform.sizeDelta.x * 0.5f;

        float upBorder = dest.y + _continueButton.image.rectTransform.sizeDelta.y * 0.5f;
        float downBorder = dest.y - _continueButton.image.rectTransform.sizeDelta.y * 0.5f;


        if (leftBorder < _scaler.referenceResolution.x * -0.5f || rightBorder > _scaler.referenceResolution.x * 0.5f)
            FlipXDir();

        if (downBorder < _scaler.referenceResolution.y * -0.5f || upBorder > _scaler.referenceResolution.y * 0.5f)
            FlipYDir();

        movement = _continueDir * Time.deltaTime * 1920 * 0.25f;
        _continueButton.image.rectTransform.anchoredPosition += movement;
	}

    void FlipXDir()
    {
        _continueDir.x = -_continueDir.x;
    }

    void FlipYDir()
    {
        _continueDir.y = -_continueDir.y;
    }
  

    void GoToLevel1()
    {
        SceneManager.LoadScene(1);
    }
}
