﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Radio : InteractableObject {

    public PauseMenu pauseMenu;
    public AudioSource audioSource;
    public AudioClip radioTuning;
    public AudioClip radioSong;
    public AudioClip click;
    public Transform dial;

    public List<GameObject> SeventiesObjectsAppear = new List<GameObject>();
    public List<GameObject> PresentObjectsAppear = new List<GameObject>();
    public List<Renderer> ObjectsTexChange = new List<Renderer>();

    public bool interactedOnce = false;
    public bool hasTurnedOn = false;

	void Update () {
        if (hasTurnedOn && !audioSource.isPlaying)
        {
            interactedOnce = true;
            audioSource.clip = radioSong;
            audioSource.loop = true;
            audioSource.volume = 0;
            hasTurnedOn = false;
            audioSource.Play();
            StartCoroutine(volumeUp(0, 0.35f, 2));
        }
    }

    public override void Interact()
    {
        if (displayName == "Turn On")
        {
            if (!interactedOnce)
            {
                //StartCoroutine(turnDial(Quaternion.Euler(new Vector3(0, 0, 90)), Quaternion.Euler(new Vector3(-40, 0, 90))));
                StartCoroutine(volumeUp(0, 0.35f, 6));
                audioSource.PlayOneShot(click);
                audioSource.PlayOneShot(radioTuning);
                hasTurnedOn = true;
            }
            else if (interactedOnce && audioSource.volume == 0)
            {
               // StartCoroutine(turnDial(Quaternion.Euler(new Vector3(0, 0, 90)), Quaternion.Euler(new Vector3(-40, 0, 90))));
                audioSource.PlayOneShot(click);
                StartCoroutine(volumeUp(0, 0.35f, 6));
            }
            displayName = "Turn Off";
        }
        else if (displayName == "Turn Off")
        {
            if (!interactedOnce)
            {
               // StartCoroutine(turnDial(Quaternion.Euler(new Vector3(-40, 0, 90)), Quaternion.Euler(new Vector3(0, 0, 90))));
                audioSource.Stop();
                audioSource.PlayOneShot(click);
            }
            else if (interactedOnce)
            {
              //  StartCoroutine(turnDial(Quaternion.Euler(new Vector3(-40, 0, 90)), Quaternion.Euler(new Vector3(0, 0, 90))));
                StartCoroutine(volumeUp(0.35f, 0, 6));
                audioSource.PlayOneShot(click);
            }
            hasTurnedOn = false;
            displayName = "Turn On";
        }
    }

    IEnumerator volumeUp(float start, float end, float rate)
    {
        float i = 0;
        while (i<1)
        {
            i += Time.deltaTime * rate;
            audioSource.volume = Mathf.Lerp(start, end, i);
            yield return new WaitForEndOfFrame();
        }
    }

    IEnumerator turnDial(Quaternion start, Quaternion end)
    {
        float i = 0;
        while (i<1)
        {
            i += Time.deltaTime * 5;
            dial.transform.rotation = Quaternion.Slerp(start, end, i);
            yield return new WaitForEndOfFrame();
        }
    }
}
