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

public enum RotationAxis
{
    X, Y
}

public class DayTimeManager : MonoBehaviour
{

    public Light sunlight;
    public float secondsPerDay;

    public Color MorningColor;
    public Color NoonColor;
    public Color EveningColor;
    public Color NightColor;

    public float maxAngle = 50.0f;

    private float _elapsedTime;

    public float timeFactor
    {
        get
        {
            return _elapsedTime / secondsPerDay;
        }
    }

    public AnimationCurve colorCurve;

    public float dayTime;

    float _morningStartFactor;
    float _noonStartFactor;
    float _eveningStartFactor;
    float _nightStartFactor;
    public float morningStartTime = 8.0f;
    public float noonStartTime = 11.0f;
    public float eveningStartTime = 17.0f;
    public float nightStartTime = 20.0f;

    public DayTimeLabel startTime = DayTimeLabel.Noon;

    public DayTimeLabel dayTimeLabel = DayTimeLabel.Night;

    private float _timeDirection = 1.0f;

    private Vector3 _position;
    private Vector3 _rotation;

    void Awake()
    {

    }

    // Use this for initialization
    void Start()
    {
        _morningStartFactor = morningStartTime / 24.0f;
        _noonStartFactor = noonStartTime / 24.0f;
        _eveningStartFactor = eveningStartTime / 24.0f;
        _nightStartFactor = nightStartTime / 24.0f;

        switch (startTime)
        {
            case DayTimeLabel.Morning:
                {
                    _elapsedTime = _morningStartFactor * secondsPerDay;
                }
                break;
            case DayTimeLabel.Noon:
                {
                    _elapsedTime = _noonStartFactor * secondsPerDay;
                }
                break;
            case DayTimeLabel.Evening:
                {
                    _elapsedTime = _eveningStartFactor * secondsPerDay;
                }
                break;
            case DayTimeLabel.Night:
                {
                    _elapsedTime = _nightStartFactor * secondsPerDay;
                }
                break;
        }
        _position = sunlight.gameObject.transform.position;
        _rotation = sunlight.gameObject.transform.rotation.eulerAngles;
    }

    // Update is called once per frame
    void Update()
    {

        _elapsedTime += Time.deltaTime;
        if (_elapsedTime >= secondsPerDay)
        {
            _elapsedTime = 0;
        }

        float intensityAngle = this.timeFactor * Mathf.PI;
        sunlight.intensity = Mathf.Sin(intensityAngle);

        float rotationDirection = (this.timeFactor - 0.5f) * 2;

        // Debug.Log("DayTimeLabel: " + dayTimeLabel.ToString());

        sunlight.gameObject.transform.rotation = Quaternion.Euler(_rotation.x + rotationDirection * maxAngle, _rotation.y + rotationDirection * maxAngle, _rotation.z);
        // Debug.Log("Rotate around x: " + (_rotation.x + rotationDirection * maxAngle));

        Color resultColor = Color.white;
        if (timeFactor >= _morningStartFactor && timeFactor < _noonStartFactor)
        {
            float morningFactor = (timeFactor - _morningStartFactor) / (_noonStartFactor - _morningStartFactor);
            resultColor = Color.Lerp(MorningColor, NoonColor, colorCurve.Evaluate(morningFactor));
            dayTimeLabel = DayTimeLabel.Morning;
        }

        if (timeFactor >= _noonStartFactor && timeFactor < _eveningStartFactor)
        {
            float noonFactor = (timeFactor - _noonStartFactor) / (_eveningStartFactor - _noonStartFactor);
            resultColor = Color.Lerp(NoonColor, EveningColor, colorCurve.Evaluate(noonFactor));
            dayTimeLabel = DayTimeLabel.Noon;
        }

        if (timeFactor >= _eveningStartFactor && timeFactor < _nightStartFactor)
        {
            float eveningFactor = (timeFactor - _eveningStartFactor) / (_nightStartFactor - _eveningStartFactor);
            resultColor = Color.Lerp(EveningColor, NightColor, colorCurve.Evaluate(eveningFactor));
            dayTimeLabel = DayTimeLabel.Evening;
        }

        if (timeFactor >= _nightStartFactor && timeFactor < 1.0f)
        {
            float nightFactor1 = (timeFactor - _nightStartFactor) / ((1.0f - _nightStartFactor) + _morningStartFactor);
            resultColor = Color.Lerp(NightColor, MorningColor, colorCurve.Evaluate(nightFactor1));
            dayTimeLabel = DayTimeLabel.Night;
        }

        if (timeFactor >= 0 && timeFactor < _morningStartFactor)
        {
            float nightFactor2 = (timeFactor + (1.0f - _nightStartFactor)) / ((1.0f - _nightStartFactor) + _morningStartFactor);
            resultColor = Color.Lerp(NightColor, MorningColor, colorCurve.Evaluate(nightFactor2));
            dayTimeLabel = DayTimeLabel.Night;
        }
        sunlight.color = resultColor;
        dayTime = timeFactor * 24.0f;
    }

    float FactorToDayTime(float factor)
    {
        return factor * 24.0f;
    }

    float DayTimeToFactor(float dayTime)
    {
        return dayTime / 24.0f;
    }

}