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

public class InventorySystem : MonoBehaviour
{
    public static GameObject inventoryOne;
    public static GameObject inventoryThree;
    public static GameObject inventoryTwo;

    public GameObject rbone;
    public GameObject rbtwo;
    public GameObject rbthree;

    public static int activeSlot = 1;
    
    public static GameObject Hand;
    public Camera fpsCam;
    

    // Start is called before the first frame update
    void Start()
    {
        activeSlot = 1;
        Hand = GameObject.Find("Hand");
    }

    // Update is called once per frame
    void Update()
    {
        if (activeSlot == 1)
        {
            if (inventoryOne != null)
            {
                inventoryOne.gameObject.transform.position = Hand.transform.position;
            }
        }
        if (activeSlot == 2)
        {
            if (inventoryTwo != null)
            {
                inventoryTwo.gameObject.transform.position = Hand.transform.position;
            }
        }
        if (activeSlot == 3)
        {
            if (inventoryThree != null)
            {
                inventoryThree.gameObject.transform.position = Hand.transform.position;
            }
        }

        if (Input.GetKeyDown(KeyCode.F))
        {
            PickUpItem();
        }

        if(Input.GetKeyDown(KeyCode.G))
        {
            DropItem();
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SwitchToSlotOne();
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SwitchToSlotTwo();
        }

        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            SwitchToSlotThree();
        }
    }

    public void PickUpItem()
    {
        RaycastHit hitinfo;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hitinfo))
        {
            Debug.Log(hitinfo);

            InventoryItemSystem inventoryItemSystem = hitinfo.transform.GetComponent<InventoryItemSystem>();
            if(inventoryItemSystem != null)
            {
                inventoryItemSystem.GetMeAsAnItem();
            }
        }
    }

    public static void DropItem()
    {
        if (activeSlot == 1)
        {
            inventoryOne.GetComponent<Rigidbody>().AddForce(0, 0, 1);
            inventoryOne.GetComponent<Collider>().enabled = true;
            inventoryOne.GetComponent<Rigidbody>().freezeRotation = false;
            inventoryOne.GetComponent<Rigidbody>().useGravity = true;
            inventoryOne.transform.parent = null;

            inventoryOne = null;
        }
        if (activeSlot == 2)
        {
            inventoryTwo.GetComponent<Rigidbody>().AddForce(0, 0, 1);
            inventoryTwo.GetComponent<Collider>().enabled = true;
            inventoryTwo.GetComponent<Rigidbody>().freezeRotation = false;
            inventoryTwo.GetComponent<Rigidbody>().useGravity = true;
            inventoryTwo.transform.parent = null;

            inventoryTwo = null;
        }
        if (activeSlot == 3)
        {
            inventoryThree.GetComponent<Rigidbody>().AddForce(0, 0, 1);
            inventoryThree.GetComponent<Collider>().enabled = true;
            inventoryThree.GetComponent<Rigidbody>().freezeRotation = false;
            inventoryThree.GetComponent<Rigidbody>().useGravity = true;
            inventoryThree.transform.parent = null;

            inventoryThree = null;
        }
    }

    public void SwitchToSlotOne()
    {
        activeSlot = 1;
        if (inventoryTwo != null)
            inventoryTwo.gameObject.SetActive(false);
        if (inventoryThree != null)
            inventoryThree.gameObject.SetActive(false);
        if (inventoryOne != null)
            inventoryOne.gameObject.SetActive(true);
    }
    public void SwitchToSlotTwo()
    {
        activeSlot = 2;
        if (inventoryOne != null)
            inventoryOne.gameObject.SetActive(false);
        if (inventoryThree != null)
            inventoryThree.gameObject.SetActive(false);
        if (inventoryTwo != null)
            inventoryTwo.gameObject.SetActive(true);
    }
    public void SwitchToSlotThree()
    {
        activeSlot = 3;
        if(inventoryOne != null)
            inventoryOne.gameObject.SetActive(false);
        if (inventoryTwo != null)
            inventoryTwo.gameObject.SetActive(false);
        if (inventoryThree != null)
            inventoryThree.gameObject.SetActive(true);
    }

}