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

public class CowControl : MonoBehaviour
{
    [SerializeField] Sprite[] m_cowBodySprites;
    [SerializeField] Sprite[] m_cowHeadSprites;
    HoverWave m_hover;
    SpriteRenderer m_rend; 

    [SerializeField] int m_spriteIndex;
    Transform m_child;

    public bool m_headAxis;

    enum INDEX_ROT
    {
        ir_NE = 0,
        ir_SE,
        ir_SW,
        ir_NW,
    };
    

    void Start()
    {
        m_child = transform.GetChild(0);
        m_rend = m_child.GetComponent<SpriteRenderer>();
        m_hover = m_child.GetComponent<HoverWave>();
        m_hover.enabled = false;
        m_child.localPosition = Vector3.zero;


        

        //m_rend.sprite = m_cowSprites[m_spriteIndex];
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Q))
        {
            Rotate(true);
        }
        
    }

    public void Rotate(bool _cw)
    {
        if(_cw)
        {
            m_spriteIndex++;
            m_spriteIndex %= 4;
        }
        else
        {
            m_spriteIndex--;
            if(m_spriteIndex < 0)
            {
                m_spriteIndex = 3;
            }
        }
        //Debug.Log("rotate. index = " + m_spriteIndex);

        //m_child.localPosition = GetLocalOffset(); 
        UpdateSprite();
    }

    void UpdateSprite()
    {
        if (m_headAxis)
        {
            m_rend.sprite = m_cowHeadSprites[m_spriteIndex];
        }
        else
        {
            m_rend.sprite = m_cowBodySprites[m_spriteIndex];
        }
    }

    public void SetAxis(bool _head)
    {
        bool oldAxis = m_headAxis;
        m_headAxis = _head;

        if(m_headAxis != oldAxis)
        {
            Vector3 offset = Vector3.zero;
            switch(m_spriteIndex)
            {
                case 0:
                    offset = new Vector3(1, 0.5f, 0);
                    break;
                case 1:
                    offset = new Vector3(1, -0.5f, 0);
                    break;
                case 2:
                    offset = new Vector3(-1, -0.5f, 0);
                    break;
                case 3:
                    offset = new Vector3(-1, 0.5f, 0);
                    break;
            }

            if(oldAxis == false)
            {
                //body to head
                //Debug.Log("body tp head");
                transform.position += offset;
            }
            else
            {
                //head to body
                //Debug.Log("head to body");
                transform.position -= offset;
            }
            UpdateSprite();
        }
    }

    public void Grab(Transform _grabber)
    {
        transform.parent = _grabber;
        m_hover.enabled = true;
        m_child.localPosition = Vector3.up * 0.25f;
        m_hover.SetLocalStart(Vector3.up * 0.25f);
    }

    public void LetGo()
    {
        transform.parent = null;
        m_hover.enabled = false;
        m_child.localPosition = Vector3.zero;

        PuzzleManager.Instance.OnCowDrop();
    }
}
