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

// tracks rotation
// reads also events from player
// observes when something is cool

public class CoolTracker : MonoBehaviour {
    List<KeyValuePair<float,KeyValuePair<string,int>>> events;
    List<float> rotationtracking;
    const int rotationtrackinglength = 150; // 3 seconds?
    float total;

    string currev;
    float currevstart;

    bool ceiling;
    bool ceilingcooloff;
    bool ceilingcongrat; // have we informed player about his cool ceiling action?
    float lastceiling;
    int killcombo;
    float lastkill;

    float lastflip; // when did we last do a flip

    //public List<KeyValuePair<float, string>> cools;

    private void Start()
    {
        currev = "";
        rotationtracking = new List<float>();
        events = GetComponent<PlayerScript>().events;
    }
    // Update is called once per frame
    void FixedUpdate () {
        while (rotationtracking.Count >= rotationtrackinglength)
            rotationtracking.RemoveAt(0);
        rotationtracking.Add(GetComponent<Rigidbody2D>().rotation);
	}

     void OnGUI()
    {
        string s = "";
        if (Time.time - currevstart < 0.5f) s = currev;
        else
        {
            bool flip = Flip();

            if (killcombo > 0)
            {
                s = "KILL x " + killcombo.ToString();
            }
            else if (flip)
            {
                s = "NICE FLIP!";
                rotationtracking = new List<float>();
                lastflip = Time.time;
            }
            else if (ceiling && !ceilingcongrat)
            {
                s = "NICE CEILING ACTION!";
                ceilingcongrat = true;
            }

            if (s != "")
            {
                currevstart = Time.time;
                currev = s;
            }
        }

        GUI.Label(new Rect(0, 120, 300, 60), /*currev.ToString()*/killcombo.ToString());
    }

    private void Update()
    {
        if (Time.time - lastceiling > 0.5f)
        {
            ceiling = false;
        }
        if (Time.time - lastkill > 3)
        {
            killcombo = 0;
        }
    }

    public void Inform(string ev, int deets)
    {
        if (ev == "ceiling")
        {
            lastceiling = Time.time;
            if (!ceilingcooloff)
            {
                ceiling = true;
                ceilingcooloff = true;
                ceilingcongrat = false;
            }
            
        }
        if (ev == "ground")
        {
            ceilingcooloff = false;
        }
        if (ev == "kill")
        {
            killcombo++;
            lastkill = Time.time;
        }
    }

    public bool XingEvent(string x)
    {
        foreach (KeyValuePair<float, KeyValuePair<string, int>> s in events)
        {
            if (s.Value.Key == x) return true;
        }
        return false;
    }

    // why does this look so stupid? because keyvaluepairs are non-nullable (because fuck me)
    // and THEN THERE'S A FUCKING STATIC CHECK THAT I PUT SOME VALUE IN THERE
    // I MEAN COME ONE WHAT HTE SHIT
    // I mean i can't exactly make a dummy KeyValuePair<float, KeyValuePair<string, int>>
    // because i get cancer from just writing that horrible shit of a type name let alone actually writing an object of said type
    void EraseXingEvent(string x)
    {
        bool yes = false;
        KeyValuePair<float, KeyValuePair<string, int>> ss = events[0];
        foreach (KeyValuePair<float, KeyValuePair<string, int>> s in events)
        {
            if (!yes && s.Value.Key == x) ss = s;
            yes = true;
        }
        if (yes)
            events.Remove(ss);
    }

    // definition of flip is that you have rotated in the last second
    public bool Flip()
    {
        if (rotationtracking.Count < rotationtrackinglength) return false;
        total = 0;
        for (int i = rotationtrackinglength-100; i < rotationtrackinglength-1; i++)
        {
            float angadd = rotationtracking[i + 1] - rotationtracking[i];
            angadd = Mathf.Repeat(angadd, 360);
            if (angadd > 180) angadd -= 360;
            total += angadd;
        }
        return Mathf.Abs(total) > 360;
        //print(total);
        /*if (dir == 1)
            if (total > 300) return true;
        if (dir == -1)
            if (total < 300) return true;
        return false;*/
    }
}
