﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace Arugula
{
    [RequireComponent(typeof(MeshSlicer))]
    public class MeshSlicerTextBox : MonoBehaviour
    {
        public TextMeshPro textMesh;
        public Vector3 offset;
        public bool useTextBounds;
        public float top;
        public float bottom;
        public float left;
        public float right;

        MeshSlicer slicer;

        [SerializeField][DebugMethod]
        private string updateMethod = "UpdateSlices";

        [ContextMenu("UpdateSlices")]
        void UpdateSlices()
        {
            if (slicer == null)
                slicer = GetComponent<MeshSlicer>();

            var bounds = useTextBounds ? textMesh.textBounds : textMesh.bounds;

            transform.localPosition = bounds.center - (slicer.baseMesh.bounds.center * transform.localScale.x);
            transform.rotation = textMesh.transform.rotation;
            transform.Translate(offset);

            slicer["X-"].shift = (bounds.extents.x + left) / transform.localScale.x;
            slicer["X+"].shift = (bounds.extents.x + right) / transform.localScale.x;
            slicer["Y-"].shift = (bounds.extents.y + bottom) / transform.localScale.y;
            slicer["Y+"].shift = (bounds.extents.y + top) / transform.localScale.y;

            slicer.UpdateMesh();

        }

    }

}
