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

public class MovementController : MonoBehaviour
{
    public Transform heldItem;
    [SerializeField] Transform interactableItem;
    [SerializeField] float maxSpeed;
    [SerializeField] float currentSpeed;
    public float acceleration;
    Furniture body;

    public GameObject grabIcon;

    public Transform InteractableItem
    {
        get => interactableItem; set
        {
            interactableItem = value;

            if (interactableItem && !heldItem)
            {
                //Show the grab icon
                grabIcon.SetActive(true);
            }
            else
            {
                grabIcon.SetActive(false);
            }
        }
    }


    // Start is called before the first frame update
    void Start()
    {
        grabIcon.SetActive(false);
        body = GetComponent<Furniture>();
        currentSpeed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if (GameState.instance.isEndGame)
        {
            return;
        }

        Vector3 pos = transform.localPosition;
        float input = Input.GetAxis("Horizontal");
        //currentSpeed = Mathf.Lerp(currentSpeed, maxSpeed, (acceleration * Time.deltaTime));
        //if (Mathf.Abs(input) < Mathf.Epsilon)
        //{
        //    currentSpeed = Mathf.Lerp(currentSpeed, 0, (acceleration * 10f * Time.deltaTime));
        //}

        pos.x += input * maxSpeed; //was currentspeed


        float maxX = (GameState.instance.roomsCreated * GameSettings.settings.roomWidth * 0.5f) - GameSettings.settings.roomWidthBuffer;

        pos.x = Mathf.Clamp(pos.x, -maxX + GameState.instance.offsetX, maxX + GameState.instance.offsetX);

        transform.localPosition = pos;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (IsHoldingItem() && !IsAbleToInteract()) //drop item
            {
                DropItem();
            }
            else if (!IsHoldingItem() && IsAbleToInteract()) //hold item
            {
                heldItem = InteractableItem;
                heldItem.parent = transform;

                Furniture furniture = heldItem.GetComponent<Furniture>();
                body.weight += furniture.weight;
                maxSpeed /= (furniture.weight * 2f);
                furniture.enabled = false;
            }
        }

    }

    public void DropItem()
    {
        if (heldItem)
        {
            Furniture furniture = heldItem.GetComponent<Furniture>();

            body.weight -= furniture.weight;
            furniture.enabled = true;

            heldItem.parent = transform.parent;
            heldItem = null;
            maxSpeed *= (furniture.weight * 2f);
        }
    }

    public void GiveItem(Transform item)
    {
        if (!heldItem)
        {
            body = GetComponent<Furniture>();

            heldItem = item;
            heldItem.parent = transform;

            Furniture furniture = heldItem.GetComponent<Furniture>();
            body.weight += furniture.weight;
            maxSpeed /= furniture.weight;
            furniture.enabled = false;
        }
        else Debug.LogError("Trying to give an item to something that already is holding one! This shouldn't be happening!");
    }


    private void OnTriggerStay(Collider other)
    {
        InteractableItem = other.transform;
    }

    private void OnTriggerExit(Collider other)
    {
        InteractableItem = null;
    }

    public bool IsHoldingItem()
    {
        return heldItem != null;
    }

    private bool IsAbleToInteract()
    {
        return InteractableItem != null && InteractableItem.GetComponent<Furniture>() && !InteractableItem.GetComponent<Furniture>().immovable;
    }
}
