﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Arugula.Animation;
using Arugula.Audio;

using TMPro;

namespace Arugula.UI
{
    public class Countdown : MonoBehaviour
    {
        public bool autoStart;
        public bool autoDestroy;
        public int duration;
        public string lastMessage = "";
        public TMP_Text label;

        public TimeMode timeMode;

        public bool tweenScale = true;
        public bool tweenAlpha = true;
        public Tween.Options tweenOptions;
        [PhonicSound]
        public string[] countSounds;
        [PhonicSound]
        public string finishedSound;

        public Phonic.Options soundOptions;

        public event System.Action OnFinished = delegate { };

        private void Start()
        {
            label.text = "";

            if (autoStart)
                StartCoroutine(CountdownRoutine());
        }

        public void Begin()
        {
            StartCoroutine(CountdownRoutine());
        }

        public void Stop()
        {
            label.text = "";
            StopAllCoroutines();
        }

        string GetCountSound(int index)
        {
            if (index >= countSounds.Length)
                return countSounds.LastOrDefault();
            else
                return countSounds[index];
        }
        IEnumerator CountdownRoutine()
        {
            for (int i = duration; i > 0; i--)
            {
                Display(i.ToString());
                soundOptions.position = transform.position;
                Phonic.Play(GetCountSound(i), soundOptions);

                if (timeMode == TimeMode.Scaled)
                {
                    yield return new WaitForSeconds(1f);
                }
                else
                {
                    yield return new WaitForSecondsRealtime(1f);
                }
            }

            OnFinished();

            if (lastMessage != "")
            {
                Display(lastMessage);
                soundOptions.position = transform.position;
                Phonic.Play(finishedSound, soundOptions);
                if (timeMode == TimeMode.Scaled)
                {
                    yield return new WaitForSeconds(1f);
                }
                else
                {
                    yield return new WaitForSecondsRealtime(1f);
                }
            }


            if (autoDestroy)
                Destroy(gameObject);
        }

        void Display(string txt)
        {
            label.text = "";
            if (tweenScale)
                label.transform.localScale = Vector3.one;
            label.text = txt;

            if (tweenScale)
            {
                label.transform.ScaleTo(Vector3.zero, tweenOptions);
            }

            if (tweenAlpha)
            {
                var t = 1f.To(0, tweenOptions, context: label);
                t.OnUpdatedValue += (x) => label.alpha = x;
                Tweener.Enqueue(t);
            }
        }
    }
}
