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

public class AblaufKleinerStern2 : MonoBehaviour
{
    public GameObject kleinerStern;
    public GameObject hase;
    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()
    {
        nextStep = SternFaellt;
        taskList = new List<Task>();
        kleinerStern.GetComponent<Rigidbody2D>().simulated = false;
        hase.GetComponent<Rigidbody2D>().simulated = false;
        globalLight.GetComponent<Light2D>().intensity = 0;
    }

    // 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)){
            nextStep();
        }
    }

    private void SternFaellt(){
        globalLight.GetComponent<Lichtsteuerung>().FadeToIntensity(1, .3f);
        Rigidbody2D sternRigBod = kleinerStern.GetComponent<Rigidbody2D>();
        sternRigBod.simulated = true;
        kleinerStern.transform.position = new Vector2(-0.8f, 6.0f);
        hase.transform.position = new Vector2(11, -4);
        hase.GetComponent<HaseBewegung>().Turn();
        SternBewegung sternBewegung = kleinerStern.GetComponent<SternBewegung>();
        scheduleNewTask(2.5f, delegate(){
            sternRigBod.simulated = false;
            sternBewegung.enabled = true;
            hase.GetComponent<Rigidbody2D>().simulated = true;
        });
        scheduleNewTask(2.52f, delegate(){
            sternBewegung.GoToPositionWithVelocity(new Vector2(-1.4f, -3.2f), 1);
        });
        nextStep = AuftrittHase;
    }

    private void AuftrittHase(){
        hase.GetComponent<HaseBewegung>().GoToXPosition(2.2f);
        nextStep = Abgang;
    }

    private void Abgang(){
        HaseBewegung haseBewegung = hase.GetComponent<HaseBewegung>();
        haseBewegung.GoToXPosition(15);
        haseBewegung.Turn();
        SternBewegung sternBewegung = kleinerStern.GetComponent<SternBewegung>();
        sternBewegung.GoToPositionWithVelocity(new Vector2(12, 0), haseBewegung.jumpVelocityX * .9f);
        sternBewegung.randomnessIntensity = 1;
        sternBewegung.randomnessBaseAngle = 5;
        sternBewegung.randomMovementDuration = .6f;
        sternBewegung.wobblingAngle = 10;
        sternBewegung.wobblingDuration = .6f;
        scheduleNewTask(3, delegate(){
            globalLight.GetComponent<Lichtsteuerung>().FadeToIntensity(0, .5f);
        });
        nextStep = delegate(){};
    }
    
    private void scheduleNewTask(float timeFromNow, Action action){
        Task t = new(action, Time.time + timeFromNow);
        taskList.Add(t);
    }
}
