using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;

public class BackScreen : MonoBehaviour
{
    static BackScreen _active = null;
    private Image _cachedImage;
    private float _deltaMojo=0f;
    private float _deltaWealth=0f;
    private int _jobLevel = 0;
    private UnityAction _callback=null;

    public Silhouette Man;
    public Silhouette Woman;

    public Bubble sceneSpeech;
    public Bubble sceneThink;
    public AudioClip audioMojoUp;
    public AudioClip audioMojoDown;
    public AudioClip audioWealthUp;
    public AudioClip audioWealthDown;

    private Silhouette CurrentAvatar;

    private void Awake()
    {
        _cachedImage = GetComponent<Image>();

        if (Man == null || Woman == null)
        {
            throw new System.NullReferenceException("Man or women avatar required");
        }
        gameObject.SetActive(false);
    }
    public void RecordResults(float deltaWealth, float deltaMojo, int jobLevel)
    {
        _deltaWealth = deltaWealth;
        _deltaMojo = deltaMojo;
        _jobLevel = jobLevel;
    }
    public void PlayResults(UnityAction callback)
    {
        _callback = callback;
        StartCoroutine(PlayResultBars());
    }
    private IEnumerator PlayResultBars()
    {
        yield return new WaitForSeconds(0.4f);
        GameStats stats = GameManager.Instance().Stats;
        bool someModi= false;
        AudioSource aSource = GetComponent<AudioSource>();
        if(_deltaMojo<0)
        {
            aSource.clip = audioMojoDown;
            someModi = true;
        }
        else if(_deltaMojo>0)
        {
            aSource.clip = audioMojoUp;
            someModi = true;
        }
        CurrentAvatar.mojo = CurrentAvatar.mojo + _deltaMojo;
        stats.mojo += _deltaMojo;
        GameManager.Instance().Stats = stats;
        if(someModi)
        { 
            aSource.Play();
            yield return new WaitForSeconds(2f);
            aSource.Stop();
        }
        someModi = false;
        if (_deltaWealth<0)
        {
            aSource.clip = audioWealthDown;
            someModi = true;
        }
        else if(_deltaWealth >0)
        {
            aSource.clip = audioWealthUp;
            someModi = true;
        }
        CurrentAvatar.money= CurrentAvatar.money+ _deltaWealth;
        stats.money += _deltaWealth;
        GameManager.Instance().Stats = stats;
        if(stats.mojo<0 || stats.money<0)
        {
            GameManager.Instance().GameOver(false);
        }
        if(stats.mojo<0 || stats.money<0)
        {
            GameManager.Instance().GameOver(false);
        }
        else if(stats.money>=1f)
        {
            GameManager.Instance().GameOver(true);
        }
        if(someModi)
        {
            aSource.Play();
            yield return new WaitForSeconds(2f);
        }
        else
            yield return new WaitForSeconds(1f);
        _deltaMojo = _deltaWealth = 0f;
        if (_callback == null)
            Debug.Log("No callback! " + _cachedImage.sprite.name);
        else
            _callback.Invoke();
    }
    public void SetBackImage(Sprite sprite)
    {
        _cachedImage.sprite = sprite;
    }
    public void Show()
    {
        if (_active != null)
        {
            _active.Hide();
        }
        _active = this;
        gameObject.SetActive(true);
    }
    public void Hide()
    {
        if (_active == this)
        {
            _active = null;
        }
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        _active = null;
    }

    public void SelectMale()
    {
        if (Man != null)
            Man.gameObject.SetActive(true);

        if (Woman != null)
            Woman.gameObject.SetActive(false);
        CurrentAvatar = Man;
    }

    public void SelectWoman()
    {
        if (Man != null)
            Man.gameObject.SetActive(false);

        if (Woman != null)
            Woman.gameObject.SetActive(true);

        CurrentAvatar = Woman;
    }


    public void SceneSay(string text)
    {
        sceneSpeech.SetText(text);
    }
    public void SceneThink(string text)
    {
        sceneThink.SetText(text);
    }

    public void Say(string text)
    {
        if (CurrentAvatar != null)
            CurrentAvatar.Say(text);
    }
    public void Think(string text)
    {
        if (CurrentAvatar != null)
            CurrentAvatar.Think(text);
    }
}
