using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RelicGGJ;
using Rewired;

public class PlayerController : GridObject
{
    public static PlayerController Instance;
    public int m_playerID = 0;
    private Player m_player;

    public Vector2 m_previousPosition;

    Vector2 m_moveDir = new Vector2(0f, 0f);
    GameObject m_moveSelected = null;

    public float m_sightRadius = 1.5f;
    public float m_sonarRadius = 0f;

    public playerAction m_playerAction;

    public GameObject m_sonarPrefab;

    public void Vibrate(float duration)
    {
        foreach (Joystick j in m_player.controllers.Joysticks)
        {
            if (!j.supportsVibration)
            {
                continue;
            }
            if (j.vibrationMotorCount > 0)
            {
                j.SetVibration(0, 1.0f, 0.25f);
            }
            if (j.vibrationMotorCount > 1)
            {
                j.SetVibration(1, 1.0f, 0.25f);
            }
        }
    }

    void Awake ()
    { 
        if (Instance)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
            CameraController cam = Camera.main.GetComponent<CameraController>();
            if (cam != null)
            {
                cam.m_player = transform;
                cam.transform.position = new Vector3(transform.position.x, transform.position.y, cam.transform.position.z);
            }
        }
    }

    public override void Init(gridLayer layer)
    {
        Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
        m_layer = layer;
        m_previousPosition = m_position;
        transform.parent = GameManager.Instance.transform;
        GridController gridController = GameManager.Instance.m_currentLevel;
        if (gridController != null)
        {
            m_level = gridController;
        }       
        m_player = ReInput.players.GetPlayer(m_playerID);
        foreach (ControllerWithAxes c in m_player.controllers.Joysticks)
        {
            foreach (AxisCalibration a in c.calibrationMap.Axes)
            {
                a.deadZone = 0.75f;
            }
            
        }
    }
	
	override protected void Update ()
    {
        if (m_level != null)
        {
            if (m_moveSelected != null)
            {
                SpriteRenderer sr = m_moveSelected.GetComponent<SpriteRenderer>();
                sr.color = Color.white;
                m_moveSelected = null;
            }
            if (m_moveDir.x != 0f || m_moveDir.y != 0f)
            {
                Vector2 newPosition = m_position + m_moveDir;
                //Debug.Log("X: " + newPosition.x + ", Y: " + newPosition.y);
                GameObject newSelected = m_level.GetObjectAt(newPosition.x, newPosition.y);
                if (newSelected != null && newSelected != gameObject)
                {
                    GridObject gObj = newSelected.GetComponent<GridObject>();
                    SpriteRenderer sr = newSelected.GetComponent<SpriteRenderer>();
                    if (gObj != null && sr != null)
                    {
                        if (gObj.m_layer == gridLayer.STATIC || gObj.m_layer == gridLayer.ENEMIES)
                        {
                            sr.color = Color.red;
                        }
                        else
                        {
                            sr.color = Color.green;
                        }
                    }

                    m_moveSelected = newSelected;
                }
            }
            
            GetInput();
            ProcessInput();

        }
        base.Update();
	}

    public override void Push(GridObject source)
    {
        switch (source.m_type)
        {
            case objectType.ENEMY:
                source.m_position = m_position;
                source.UpdatePosition();
                GameManager.Instance.Lose(source);
                break;
        }
    }

    void GetInput()
    {

        if (m_player.GetButton("Move Up"))
        {
            m_moveDir.y = -1f;
        }
        else if (m_player.GetButton("Move Down"))
        {
            m_moveDir.y = 1f;
        }
        else
        {
            m_moveDir.y = 0f;
        }

        if (m_player.GetButton("Move Left"))
        {
            m_moveDir.x = -1f;
        }
        else if (m_player.GetButton("Move Right"))
        {
            m_moveDir.x = 1f;
        }
        else
        {
            m_moveDir.x = 0f;
        }
    }

    void ProcessInput()
    {
        if (m_player.GetButtonUp("Sonar"))
        {
            if (m_moveDir.x == 0 && m_moveDir.y < 0)
            {
                m_playerAction = playerAction.MOVE_UP;
            }
            else if (m_moveDir.x > 0 && m_moveDir.y < 0)
            {
                m_playerAction = playerAction.MOVE_UP_RIGHT;
            }
            else if (m_moveDir.x > 0 && m_moveDir.y == 0)
            {
                m_playerAction = playerAction.MOVE_RIGHT;
            }
            else if (m_moveDir.x > 0 && m_moveDir.y > 0)
            {
                m_playerAction = playerAction.MOVE_DOWN_RIGHT;
            }
            else if (m_moveDir.x == 0 && m_moveDir.y > 0)
            {
                m_playerAction = playerAction.MOVE_DOWN;
            }
            else if (m_moveDir.x < 0 && m_moveDir.y > 0)
            {
                m_playerAction = playerAction.MOVE_DOWN_LEFT;
            }
            else if (m_moveDir.x < 0 && m_moveDir.y == 0)
            {
                m_playerAction = playerAction.MOVE_LEFT;
            }
            else if (m_moveDir.x < 0 && m_moveDir.y < 0)
            {
                m_playerAction = playerAction.MOVE_UP_LEFT;
            }
            else
            {
                m_playerAction = playerAction.SONAR;
            }            
        }
        if (m_playerAction != playerAction.DO_NOTHING)
        {
            Vector2 newPos = m_position + m_moveDir;
            GameObject found = GameManager.Instance.m_currentLevel.GetObjectAt(newPos.x, newPos.y);
            if (found != null && found.GetComponent<GridObject>().m_layer != gridLayer.STATIC && 
                found.GetComponent<GridObject>().m_layer != gridLayer.ENEMIES)
            {
                m_level.GameTick(m_playerAction);
            }
            else
            {
                m_playerAction = playerAction.DO_NOTHING;
            }
            
        }
    }

    public override void GameTick()
    {
        if (m_playerAction == playerAction.SONAR)
        {
            if (m_sonarPrefab != null)
            {
                GameObject obj = Instantiate(m_sonarPrefab, transform.position, Quaternion.identity) as GameObject;
                obj.transform.parent = null;
                m_previousPosition = m_position;
            }
        }
        else
        {
            Vector2 newPos = m_position + m_moveDir;
            m_previousPosition = m_position;
            Move(ref newPos);
            UpdatePosition();
        }
        m_playerAction = playerAction.DO_NOTHING;

    }
}
