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

public class PlayerHandler : MonoBehaviour
{
    private new Renderer renderer;

    private int playerID;

    private float speed = 1;

    Vector3 defaultPos;
    float defaultSize;

    private void Awake()
    {
        renderer = GetComponentInChildren<Renderer>();
    }

    public void Initialize(int playerID, Material material, Color color, Vector3 pos, float size)
    {
        this.playerID = playerID;
        renderer.sharedMaterial = material;
        renderer.material.color = color;
        defaultPos = pos;
        defaultSize = size;

        Respawn();
    }

    void Respawn()
    {
        Debug.LogError(string.Format("Player {0} RESPAWN", playerID));
        transform.position = defaultPos;
        transform.localScale = Vector3.one * defaultSize;
    }

    void AbilityA()
    {
        Debug.LogError(string.Format("Player {0} ABILITY A", playerID));
        transform.localScale = Vector3.one * defaultSize * 2;
    }
    void AbilityB()
    {
        Debug.LogError(string.Format("Player {0} ABILITY B", playerID));
        transform.localScale = Vector3.one * defaultSize / 2;
    }

    Vector3 movementDir;

    const float abilityCD = 2;
    float lastAbilityTime = -abilityCD;

    private void Update()
    {
        if (isTLPressed && isTRPressed && isBLPressed && isBRPressed)
        {
            if (Time.time - lastAbilityTime > abilityCD)
            {
                Respawn();
                lastAbilityTime = Time.time;
            }
            else
                Debug.LogError(string.Format("Player {0} FAIL RESPAWN {1}", playerID, (Time.time - lastAbilityTime).ToString("#.0")));
        }
        else if (isTLPressed && isBRPressed)
        {
            if (Time.time - lastAbilityTime > abilityCD)
            {
                AbilityA();
                lastAbilityTime = Time.time;
            }
            else
                Debug.LogError(string.Format("Player {0} FAIL ABILITY A {1}", playerID, (Time.time - lastAbilityTime).ToString("#.0")));
        }
        else if (isBLPressed && isTRPressed)
        {
            if (Time.time - lastAbilityTime > abilityCD)
            {
                AbilityB();
                lastAbilityTime = Time.time;
            }
            else
                Debug.LogError(string.Format("Player {0} FAIL ABILITY B {1}", playerID, (Time.time - lastAbilityTime).ToString("#.0")));
        }
        // MOVEMENT
        else
        {
            movementDir = Vector3.zero;

            if (isTLPressed && isTRPressed)
                movementDir = new Vector3(0, 0, 1);
            else if (isTRPressed && isBRPressed)
                movementDir = new Vector3(1, 0, 0);
            else if (isBRPressed && isBLPressed)
                movementDir = new Vector3(0, 0, -1);
            else if (isBLPressed && isTLPressed)
                movementDir = new Vector3(-1, 0, 0);
            else if (isTLPressed)
                movementDir = new Vector3(-1, 0, 1).normalized;
            else if (isTRPressed)
                movementDir = new Vector3(1, 0, 1).normalized;
            else if (isBRPressed)
                movementDir = new Vector3(1, 0, -1).normalized;
            else if (isBLPressed)
                movementDir = new Vector3(-1, 0, -1).normalized;
            else
                movementDir = Vector3.zero;

            transform.Translate(movementDir * Time.deltaTime * speed);
        }
    }

    bool isTLPressed;
    bool isTRPressed;
    bool isBLPressed;
    bool isBRPressed;

    internal void HandleInput(int buttonID, bool state)
    {
        Button button = (Button)buttonID;
        Trigger trigger = (Trigger)(state ? 1 : 0);

        Debug.LogError(string.Format("Player #{0} just {1} the {2} button.", playerID, button, trigger));

        switch (button)
        {
            case Button.TopLeftRed:
                isTLPressed = state;
                break;
            case Button.TopRightGreen:
                isTRPressed = state;
                break;
            case Button.BottomLeftWhite:
                isBLPressed = state;
                break;
            case Button.BottomRightYellow:
                isBRPressed = state;
                break;
        }
    }

    enum Button { TopLeftRed = 0, TopRightGreen, BottomLeftWhite, BottomRightYellow }
    enum Trigger { Pressed = 0, Released }
}
