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


public class KineticFactBehaviour : MonoBehaviour
{
    List<FunctionFact<float, float>> functions;
    private int active_func_ind = -1;
    private float time = 0;

    private bool _done;
    public bool done { 
        get => _done; 
        private set {
            if (value && repeat) {
                Reset();
                StartExecution();
            } else
                _done = value;
        } 
    }

    public bool repeat = true;

    public bool execute { 
        get { return active_func_ind < 0; }
        set { if (value) StartExecution(); else StoppExecution(); }
    }

    private Transform startcondition;

    void Awake()
    {
        startcondition = transform;
    }

    private void Update()
    {
        if (!execute) 
            return;

        active_func_ind = functions.FindIndex(active_func_ind, ff => ff.domain.Item1 <= time);
        if (!execute) {
            done = true;
            return;
        }

        transform.position = new Vector3(transform.position.x, functions[active_func_ind].function(time), transform.position.z);
        time += Time.deltaTime;
    }

    public void Reset()
    {
        active_func_ind = -1;
        time = 0;
        transform.position = startcondition.position;
        done = false;
    }

    public void StartExecution()
    {
        active_func_ind = 0;
    }

    public void StoppExecution()
    {
        active_func_ind = -1;
    }
}