using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class AblaufKleinerStern4 : MonoBehaviour
{
    public GameObject wirt;
    public GameObject maria;
    public GameObject joseph;
    public GameObject kleinerStern;
    public GameObject globalLight;

    private Action nextStep;
    private class Task {
        private Action action;
        private float time;
        
        public Task(Action action, float time){
            this.action = action;
            this.time = time;
        }

        public bool isDue(){
            return Time.time >= time;
        }

        public void Do(){
            action();
        }
    }
    private List<Task> taskList;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        taskList = new();
        nextStep = AuftrittMariaUndJoseph;
        globalLight.GetComponent<Light2D>().intensity = 0;
        wirt.transform.position = new(3.41f, -2.6f);
        joseph.transform.position = new(-6.8f, -2.81f);
        maria.transform.position = new(-11.4f, -1.5f);
    }

    // Update is called once per frame
    void Update()
    {
        List<Task> newlist = new();
        foreach (Task t in taskList){
            if (t.isDue()){
                t.Do();
            } else {
                newlist.Add(t);
            }
        }
        taskList = newlist;
        if (Input.GetKeyDown(KeyCode.Space) && taskList.Count == 0){
            nextStep();
        }
    }

    private void scheduleNewTask(float timeFromNow, Action action){
        Task t = new(action, Time.time + timeFromNow);
        taskList.Add(t);
    }

    private void AuftrittMariaUndJoseph(){
        kleinerStern.GetComponent<SternBewegung>().GoToPosition(new(-5.6f, 3), 0);
        globalLight.GetComponent<Lichtsteuerung>().FadeToIntensity(1, 0.5f);
        joseph.GetComponent<FigurMitBeinenBewegung>().GoToXPosition(-2.64f);
        maria.GetComponent<FigurMitBeinenBewegung>().GoToXPosition(-7.24f);
        scheduleNewTask(0.001f, delegate(){
            kleinerStern.GetComponent<SternBewegung>().GoToPositionWithVelocity(new(-3.5f, 3), 1);
        });
        nextStep = AuftrittWirt;
    }

    private void AuftrittWirt(){
        wirt.GetComponent<FigurMitBeinenBewegung>().GoToXPosition(0.76f);
        nextStep = SchwenkAufStall;
    }

    private void SchwenkAufStall(){
        GetComponent<FigurMitBeinenBewegung>().GoToXPosition(-15);
        transform.rotation = Quaternion.identity;
        scheduleNewTask(3, delegate(){
            kleinerStern.GetComponent<SternBewegung>().GoToPosition(new(-4, 3), 0);
        });
        nextStep = SternWandert;
    }

    private void SternWandert(){
        kleinerStern.GetComponent<SternBewegung>().GoToPositionWithVelocity(new(-16, 3), 2);
        nextStep = SchwenkZurueck;
    }

    private void SchwenkZurueck(){
        GetComponent<FigurMitBeinenBewegung>().GoToXPosition(0);
        nextStep = WirtFuehrtZumStall;
    }

    private void WirtFuehrtZumStall(){
        wirt.GetComponent<FigurMitBeinenBewegung>().GoToXPosition(-10);
        scheduleNewTask(3, delegate(){
            globalLight.GetComponent<Lichtsteuerung>().FadeToIntensity(0, 1);
        });
        nextStep = delegate(){};
    }
}
