﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;

public class ToYoutube : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void OpenNewTab(string url);

    public RectTransform textRect;
    public float pulseSpeed = 1, minSize = 1f, maxSize = 1.2f;
    public string[] randomLines = new string[] { "Check us out on YouTube!"};

    private void Start()
    {
        textRect.GetComponent<Text>().text = randomLines[Random.Range(0, randomLines.Length - 1)];
        StartCoroutine(PulseText());
    }

    private IEnumerator PulseText()
    {
        if (!textRect)
            yield break;

        while (true)
        {
            textRect.localScale = Vector3.one * Mathf.Lerp(minSize, maxSize, (1 + Mathf.Sin(Time.time * pulseSpeed)) / 2);

            yield return null;
        }
    } 

    public void OnClick()
    {
        if (Application.platform != RuntimePlatform.WebGLPlayer)
            openIt("https://www.youtube.com/user/BRPclips");
        else
            Application.OpenURL("https://www.youtube.com/user/BRPclips");
    }

    public void openIt(string url)
    {
        #if !UNITY_EDITOR && UNITY_WEBGL
        OpenNewTab(url);
        #endif
    }
}
