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

public class Piece : MonoBehaviour
{
    public bool amAI;
    public int player;
    public string state;
    public int power;

    public Text instructions;

    public List<GameObject> dancers;
    List<string> code;

    public string PieceType()
    {
        if (GetComponent<PawnScript>()) return "pawn";
        if (GetComponent<PonyScript>()) return "knight";
        if (GetComponent<BishopScript>()) return "bishop";
        if (GetComponent<RookScript>()) return "rook";
        if (GetComponent<QueenScript>()) return "queen";
        if (GetComponent<KingScript>()) return "king";
        return "";
    }
    public float Height()
    {
        if (GetComponent<PawnScript>()) return 1f;
        if (GetComponent<PonyScript>()) return 1.1f;
        if (GetComponent<BishopScript>()) return 1.2f;
        if (GetComponent<RookScript>()) return 1.6f;
        if (GetComponent<QueenScript>()) return 1.7f;
        if (GetComponent<KingScript>()) return 1.7f;
        return 0;
    }
    public virtual void InformHit(Collider c)
    {

    }
    public void Start()
    {
        amAI = Camera.main.GetComponent<ChessScript>().isAI[player];
    }
    public virtual void PowerUp()
    {
        //print("here");
        power += 1;
    }
    public virtual void Awake()
    {
        code = new List<string>();
        instructions = GameObject.Find("Instructions").GetComponent<Text>();
        dancers = new List<GameObject>();
        power = 0;
        int xx = (int)((transform.position.x + 16) / 4);
        int yy = (int)((transform.position.z + 16) / 4);
        transform.position = new Vector3(-16 + xx * 4 + 2, 2, -16 + yy * 4 + 2);
        // bishop hack because I don't wanna bother Tuuli
        if (GetComponent<BishopScript>())
        {
            transform.position = transform.position - transform.forward * 0.75f;
        }
    }
    public virtual void Update()
    {
        //if (Random.Range(0, 200) == 0) power += 1;
        if (power < dancers.Count)
        {
            Destroy(dancers[0]);
            dancers.RemoveAt(0);
        }
        if (power > dancers.Count)
        {
            GameObject dan = GameObject.Instantiate(Resources.Load<GameObject>("Dancer"));
            dan.transform.parent = transform;
            float t = 0.2f;
            dan.transform.position = transform.position + Vector3.up * Height() + Vector3.up * 0.4f + new Vector3(Random.Range(-t, t), Random.Range(-t, t), Random.Range(-t, t));
            dancers.Add(dan);
        }

        if (state == "aim") TrackCodes();
    }
    public void TrackCodes()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            code.Add("up");
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            code.Add("down");
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            code.Add("left");
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            code.Add("right");
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            code.Add("A");
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            code.Add("B");
        }
        string s = "";
        for (int k = 0; k < code.Count; k++)
        {
            s += code[k];
        }
        //print(s);

        List<string> konami = new List<string> { "up", "up", "down", "down", "left", "right", "left", "right", "B", "A" };
        List<string> imanok = new List<string> { "down", "down", "down", "down", "down", "down", "down", "down" };
        List<List<string>> codes = new List<List<string>> { konami, imanok };
        for(int j = 0; j < codes.Count; j++)
        {
            List<string> c = codes[j];
            if (c.Count <= code.Count)
            {
                int startidx = code.Count - c.Count;
                bool ok = true;
                for (int i = 0; i < c.Count; i++)
                {
                    if (code[startidx + i] != c[i])
                    {
                        ok = false;
                    }
                }
                if (ok)
                {
                    ProcessCode(j);
                    code = new List<string>();
                }
            }
        }
    }

    public void ProcessCode(int j)
    {
        //print("yep");
        if (j == 0)
        {
            power += 1;
        }
        if (j == 1)
        {
            power -= 1;
        }
    }
}
