using System.Collections.Generic;
using UnityEngine;

public class MovableFurniture : Furniture
{
    private List<Transform> children;
    private List<ObjectOutline> outlines;
    private Rigidbody _rigidbody;
    public bool wallMount;

    // Start is called before the first frame update
    new void Start()
    {
        base.Start();
        _rigidbody = GetComponent<Rigidbody>();
        children = new List<Transform>(GetComponentsInChildren<Transform>());
        outlines = new List<ObjectOutline>();
        foreach (Transform child in children)
        {
            var part = child.gameObject.AddComponent<MovableFurniturePart>();
            part.parent = this;
            var outline = child.gameObject.AddComponent<ObjectOutline>();
            if (!outline) continue;
            outlines.Add(outline);
            outline.OutlineMode = ObjectOutline.Mode.OutlineAll;
            outline.OutlineColor = Color.yellow;
            outline.OutlineWidth = 5f;
            outline.enabled = false;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnPointerClickUp()
    {
        if (_rigidbody != null)
        {
            _rigidbody.useGravity = true;
        }

    }
    public void OnPointerClickDown()
    {
        if (_rigidbody != null)
        {
            _rigidbody.useGravity = false;
        }

    }

    public void OnPointerEnter()
    {
        foreach (ObjectOutline outline in outlines)
        {
            /*child.OutlineMode = ObjectOutline.Mode.OutlineAll;
            child.OutlineColor = Color.yellow;
            child.OutlineWidth = 5f;*/
            outline.enabled = true;
        }
    }

    public void OnPointerExit()
    {
        foreach (ObjectOutline outline in outlines)
        {
            outline.enabled = false;
        }
    }

    /*public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction)
    {
        Vector3 displacement = Vector3.zero;
        this.transform.position = position + centerOffset;
        Vector3[] directions = { Vector3.right, Vector3.left, Vector3.up, Vector3.down, Vector3.forward, Vector3.back };
        int[] extents = { 0, 0, 1, 1, 2, 2 };

        for (int i = 0; i < 6; i++)
        {
            Ray raycast = new Ray(transform.position - centerOffset - (directions[i] * combinedBounds.extents[extents[i]]), (directions[i] * combinedBounds.size[extents[i]]));
            RaycastHit hit;
            bool SnapbHit = Physics.Raycast(raycast, out hit, combinedBounds.size[extents[i]], layerMask);
            if (SnapbHit)
            {
                displacement[i / 2] -= (i % 2 == 1 ? -1 : 1) * (combinedBounds.size[extents[i]] - (hit.distance));
            }
        }

        this.transform.Translate(displacement, Space.World);
    }*/

    public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction)
    {
        Vector3 previousPosition = transform.position;
        Vector3 offset = Vector3.zero;
        if (wallMount)
        {
            transform.position = position;
            Bounds objectBounds;
            if (combinedBounds == null)
            {
                objectBounds = new Bounds();
                foreach (Transform child in children)
                {
                    objectBounds.Encapsulate(child.GetComponent<Renderer>().bounds);
                }
            }
            else
            {
                objectBounds = (Bounds)combinedBounds;
            }

            Vector3 objectSize = objectBounds.size;
            offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2);
            Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask);
            if (colliders.Length > 0)
            {
                for (int i = 0; i < colliders.Length; i++)
                {
                    offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position;
                }
                offset /= colliders.Length;
                transform.position += offset;
            }
        }
        else
        {
            Vector3 newPosition = new Vector3(position.x, transform.position.y, position.z);
            transform.position = newPosition;
            Bounds objectBounds = GetComponent<Collider>().bounds;
            Vector3 objectSize = objectBounds.size;
            offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2);
            Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask);
            if (colliders.Length > 0)
            {
                for (int i = 0; i < colliders.Length; i++)
                {
                    offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position;
                }
                offset /= colliders.Length;
                transform.position += offset;
            }
        }
    }

    void OnDrawGizmos()
    {
        // Draw a yellow sphere at the transform's position
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(transform.position - centerOffset, combinedBounds.size);
        Gizmos.DrawRay((transform.position - centerOffset) - (new Vector3(0, 0, combinedBounds.extents.z)), (new Vector3(0, 0, combinedBounds.size.z)));
    }

    private void OnDisable()
    {
        if(outlines != null && outlines.Count > 0)
        {
            foreach (ObjectOutline outline in outlines)
            {
                outline.enabled = false;
            }
        }
        
    }

}
