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

public class DayLoopManager : MonoBehaviour {
    TimerHelper timer;

    public int sec = 0;
    public int h;

    public Text textA;

    public bool day = true;

    static public DayLoopManager instance;

    static public DayLoopManager Get() {
        return(instance);
    }

    void Start() {
        timer = TimerHelper.Create();

        instance = this;
    }

    void Update() {
        if (timer.Get() > (0.25f / 4)) {
            timer.Reset();

            float var = (float)sec / (24 * 4);
                
            float c = 2 - var;
            
            if (c < 0) {
                c = 0;
            }

            if (c < 0.1f) {
                c = 0.1f;
            }

            bool stressy = true;
            PlayerController player = PlayerController.Get();
            foreach(Nest item in Nest.GetList()) {
                if (Vector2.Distance(item.transform.position, player.transform.position) < 4) {
                    stressy = false;

                }
            }

            if (stressy) {
                if ((1 - c) > 0.75f) {
                    player.stress -= (1 - c) * 2;
                }
               
            } else {
               player.stress += 1;
            }

            if (player.stress > 100) {
                player.stress = 100;
            }

            if (player.stress < 0) {
                Application.LoadLevel(Application.loadedLevel);
                player.stress = 0;
            } 
                


            if (day) {
                sec ++;

                textA.text = sec.ToString();
                
                
                if (sec == 220) {
                    day = false;
                }

                LightingManager2D.Get().darknessColor = new Color(c, c, c, 1);
            } else {
                sec --;

                textA.text = sec.ToString();
            
                if (sec == 0) {
                    day = true;
                }

                LightingManager2D.Get().darknessColor = new Color(c, c, c, 1);
            }
        }
    }
}
