// Update is called once per frame
    void Update()
    {
        // Debug.Log(_mouseState);
        if (Input.GetMouseButtonDown(0))
        {
            origin = transform.localPosition;
            RaycastHit hitInfo;
            target = GetClickedObject(out hitInfo);
            if (target != null)
            {
                DragNDrop dnd = target.GetComponent <DragNDrop>();
                if (dnd != null)
                {
                    _mouseState = true;
                    screenSpace = Camera.main.WorldToScreenPoint(target.transform.position);
                    offset      = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
                    Collider col = target.GetComponent <Collider>();
                    if (col != null)
                    {
                        col.enabled = false;
                    }
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            _mouseState = false;
            RaycastHit hitInfo;
            target = GetClickedObject(out hitInfo);
            if (target != null)
            {
                Collider col = GetComponent <Collider>();
                if (col != null)
                {
                    col.enabled = true;
                }
                Body body = target.GetComponent <Body>();
                if (body != null)
                {
                    if (body.TryEquipGameObject(this.gameObject))
                    {
                        isEquipped = true;
                        Rotate rotate = GetComponent <Rotate>();
                        if (rotate != null)
                        {
                            rotate.enabled          = false;
                            transform.localRotation = originRotation;
                            transform.localScale    = originScale;
                        }
                    }
                    else
                    {
                        transform.localPosition = origin;
                        isEquipped = false;
                    }
                }
                else
                {
                    body = target.GetComponentInParent <Body>();
                    if (body != null)
                    {
                        if (body.TryEquipGameObject(this.gameObject))
                        {
                            isEquipped = true;
                            Rotate rotate = GetComponent <Rotate>();
                            if (rotate != null)
                            {
                                rotate.enabled          = false;
                                transform.localRotation = originRotation;
                                transform.localScale    = originScale;
                            }
                        }
                        else
                        {
                            transform.localPosition = origin;
                            isEquipped = false;
                        }
                    }
                    else
                    {
                        // InspectPosition needs  rigidbody and collider and larger hot spot
                        // Player needs larger hotspot as well
                        if (isEquipped && target.name == "InspectPosition")
                        {
                            if (target.transform.childCount > 0)
                            {
                                for (int i = 0; i < target.transform.childCount; i++)
                                {
                                    DestroyObject(target.transform.GetChild(i).gameObject);
                                }
                            }
                            BodyPartHover bph = GetComponent <BodyPartHover>();
                            if (bph != null)
                            {
                                bph.isEnabled = false;
                            }
                            isEquipped              = false;
                            transform.parent        = target.transform;
                            transform.localRotation = originRotation;
                            transform.localScale    = originScale;
                            Rotate rotate = GetComponent <Rotate>();
                            if (rotate != null)
                            {
                                rotate.enabled = true;
                            }
                            BodyPart[]      bodyParts = GetComponents <BodyPart>();
                            InventoryObject io        = GetComponent <InventoryObject>();
                            if (io != null)
                            {
                                foreach (BodyPart bodyPart in bodyParts)
                                {
                                    if (bodyPart != null)
                                    {
                                        switch (bodyPart.bodyPartType)
                                        {
                                        case BodyPartType.Hat:
                                            if (EquipedItems.GetHat() == io.ResourceName)
                                            {
                                                EquipedItems.EquipHat(null);
                                            }
                                            break;

                                        case BodyPartType.EyeNose:
                                            if (EquipedItems.GetLeftEye() == io.ResourceName)
                                            {
                                                EquipedItems.EquipLeftEye(null);
                                            }
                                            if (EquipedItems.GetRightEye() == io.ResourceName)
                                            {
                                                EquipedItems.EquipRightEye(null);
                                            }
                                            if (EquipedItems.GetNose() == io.ResourceName)
                                            {
                                                EquipedItems.EquipNose(null);
                                            }
                                            break;

                                        case BodyPartType.Mouth:
                                            if (EquipedItems.GetMouth() == io.ResourceName)
                                            {
                                                EquipedItems.EquipMouth(null);
                                            }
                                            break;

                                        case BodyPartType.Scarf:
                                            if (EquipedItems.GetNeck() == io.ResourceName)
                                            {
                                                EquipedItems.EquipNeck(null);
                                            }
                                            break;

                                        case BodyPartType.Mitten:
                                            if (EquipedItems.GetLeftHand() == io.ResourceName)
                                            {
                                                EquipedItems.EquipLeftHand(null);
                                            }
                                            if (EquipedItems.GetRightHand() == io.ResourceName)
                                            {
                                                EquipedItems.EquipRightHand(null);
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            transform.localPosition = origin;
                            isEquipped = false;
                        }
                    }
                }
            }
            else
            {
                transform.localPosition = origin;
                isEquipped = false;
            }
        }
        if (_mouseState)
        {
            //keep track of the mouse position
            var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

            //convert the screen mouse position to world point and adjust with offset
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

            //update the position of the object in the world
            target.transform.position = curPosition;
        }
    }
    public bool TryEquipGameObject(GameObject obj)
    {
        bool retval = false;

        if (obj != null)
        {
            BodyPart[] bodyParts = obj.GetComponents <BodyPart>();
            foreach (BodyPart bodyPart in bodyParts)
            {
                BodyPartHover bph = obj.GetComponent <BodyPartHover>();
                if (bph != null)
                {
                    if (bodyPart != null)
                    {
                        InventoryObject io = obj.GetComponent <InventoryObject>();
                        if (io != null)
                        {
                            switch (bodyPart.bodyPartType)
                            {
                            case BodyPartType.Hat:
                                if (EquipedItems.GetHat() == null)
                                {
                                    HatEquipmentPosition.Object = obj;
                                    EquipGameObject(bodyPart, HatEquipmentPosition.ObjectPosition);
                                    if (HatToggle != null)
                                    {
                                        HatToggle.isOn = true;
                                    }

                                    EquipedItems.EquipHat(io.ResourceName);

                                    retval = true;
                                }
                                break;

                            case BodyPartType.EyeNose:

                                if (EquipedItems.GetLeftEye() == null)
                                {
                                    EquipGameObject(bodyPart, LeftEyePosition);
                                    if (LeftEyeToggle != null)
                                    {
                                        LeftEyeToggle.isOn = true;
                                    }

                                    EquipedItems.EquipLeftEye(io.ResourceName);

                                    retval = true;
                                }
                                else if (EquipedItems.GetLeftEye() != io.ResourceName && EquipedItems.GetRightEye() == null)
                                {
                                    EquipGameObject(bodyPart, RightEyePosition);
                                    if (RightEyeToggle != null)
                                    {
                                        RightEyeToggle.isOn = true;
                                    }

                                    EquipedItems.EquipRightEye(io.ResourceName);

                                    retval = true;
                                }
                                else if (EquipedItems.GetLeftEye() != io.ResourceName && EquipedItems.GetRightEye() != io.ResourceName && EquipedItems.GetNose() == null)
                                {
                                    EquipGameObject(bodyPart, NosePosition);
                                    if (NoseToggle != null)
                                    {
                                        NoseToggle.isOn = true;
                                    }

                                    EquipedItems.EquipNose(io.ResourceName);

                                    retval = true;
                                }
                                break;

                            case BodyPartType.Mouth:
                                if (EquipedItems.GetMouth() == null)
                                {
                                    EquipGameObject(bodyPart, MouthPosition);
                                    if (MouthToggle != null)
                                    {
                                        MouthToggle.isOn = true;
                                    }

                                    EquipedItems.EquipMouth(io.ResourceName);

                                    retval = true;
                                }
                                break;

                            case BodyPartType.Scarf:
                                if (EquipedItems.GetNeck() == null)
                                {
                                    EquipGameObject(bodyPart, ScarfPosition);
                                    if (ScarfToggle != null)
                                    {
                                        ScarfToggle.isOn = true;
                                    }
                                    EquipedItems.EquipNeck(io.ResourceName);

                                    retval = true;
                                }
                                break;

                            case BodyPartType.Mitten:
                                if (EquipedItems.GetLeftHand() == null)
                                {
                                    EquipGameObject(bodyPart, LeftMittenPosition);
                                    if (LeftMittenToggle != null)
                                    {
                                        LeftMittenToggle.isOn = true;
                                    }

                                    EquipedItems.EquipLeftHand(io.ResourceName);

                                    // Rotate Mitten so Thumb is up.
                                    obj.transform.localRotation = new Quaternion(0f, 0.9f, 0f, 1f);
                                    retval = true;
                                }
                                else if (EquipedItems.GetLeftHand() != io.ResourceName && EquipedItems.GetRightHand() == null)
                                {
                                    EquipGameObject(bodyPart, RightMittenPosition);
                                    if (RightMittenToggle != null)
                                    {
                                        RightMittenToggle.isOn = true;
                                    }

                                    EquipedItems.EquipRightHand(io.ResourceName);

                                    // Rotate Mitten so Thumb is up.
                                    obj.transform.localRotation = new Quaternion(0f, -0.9f, 0f, 1f);
                                    retval = true;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        return(retval);
    }