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

public class SleepDepth : MonoBehaviour
{

    public static SleepDepth Instance;

    public Color badColour;
    public Color goodColor;
    public GameObject fill;
    private Vector2 fillScale;
    
    public float sleepDepth;
    public float timeToRestoreInS = 10f;
    private SpriteRenderer _spriteRenderer;
        
        

    public static readonly float PERFECT_SCORE = 44f + .8f + .6f + .4f + .2f;
    public static float score;

    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        _spriteRenderer = fill.GetComponent<SpriteRenderer>();
        fillScale = fill.transform.localScale;
        
        reset();
    }

    public void reset()
    {
        sleepDepth = 0;
        score = 0;
    }

    // Update is called once per frame
    void Update()
    {
        sleepDepth += Time.deltaTime / timeToRestoreInS;
        sleepDepth = Mathf.Clamp01(sleepDepth);
    }

    private void OnGUI()
    {
        Color c = Color.Lerp(badColour, goodColor, sleepDepth);
        c.a = 1;
        fill.transform.localScale = new Vector2(fillScale.x * sleepDepth, fillScale.y);
        _spriteRenderer.color = c;
    }

    public static bool isAsleep()
    {
        return Instance.sleepDepth >= .25f;
    }

    public static void subtract(float value)
    {
        Instance.sleepDepth -= value;
    }

    public static void drainInSecs(float n)
    {
        subtract((1f / n) * Time.deltaTime);
    }

    public static void addScore()
    {
        if (Timer.Instance.oversleeping)
        {
            score -= Instance.sleepDepth;
        }
        else
        {
            score += Instance.sleepDepth;
        }
    }
}
