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

public class CelestialOrbBezier : MonoBehaviour
{
    public Vector3 startingPoint;
    public float curveFactor; // Controls the curve
    public Vector3 endingPoint;
    public Color daySky = new Color(0.947f, 0.947f, 0.947f, 1.000f);
    public Color nightSky = Color.black;
    public float timeFactor = 0.2f;
    public GameObject Background;

    private float count = 0.0f;
    private Vector3 controlPoint;

    private SpriteRenderer orbRenderer;
    private SpriteRenderer skyBoxRenderer;
    private Color yellow = Color.yellow;
    private Color white = Color.white;
    
    private bool isDay = false;

    // Start is called before the first frame update
    void Start()
    {
        transform.localPosition = startingPoint;
        controlPoint = startingPoint + (endingPoint - startingPoint) / 2 + Vector3.up * curveFactor;
        orbRenderer = GetComponent<SpriteRenderer>();
        skyBoxRenderer = Background.GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (count < 1.0f)
        {
            
            count += timeFactor * Time.deltaTime;
            //float skyCoefficient = !isDay ? count - 1 : count;
            skyBoxRenderer.color = isDay ? Color.Lerp(daySky, nightSky, count) : Color.Lerp(nightSky, daySky, count);
            Vector3 m1 = Vector3.Lerp(startingPoint, controlPoint, count);
            Vector3 m2 = Vector3.Lerp(controlPoint, endingPoint, count);
            transform.localPosition = Vector3.Lerp(m1, m2, count);
        }
        else
        {
            transform.localPosition = startingPoint;
            count = 0.0f;
            isDay = !isDay;

            if (isDay)
            {
                
                orbRenderer.color = yellow;
                EventManager.TriggerSunrise();
            }
            else
            {
                orbRenderer.color = white;
                EventManager.TriggerSunset();
            }

            controlPoint = startingPoint + (endingPoint - startingPoint) / 2 + Vector3.up * curveFactor;
            


        }

    }
}
