using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
namespace XCharts.Runtime
{
    /// 
    /// The base class of all graphs or components.
    /// ||所有图形的基类。
    /// 
    public partial class BaseGraph
    {
        /// 
        /// The x of graph.
        /// ||图形的X
        /// 
        public float graphX { get { return m_GraphX; } }
        /// 
        /// The y of graph.
        /// ||图形的Y
        /// 
        public float graphY { get { return m_GraphY; } }
        /// 
        /// The width of graph.
        /// ||图形的宽
        /// 
        public float graphWidth { get { return m_GraphWidth; } }
        /// 
        /// The height of graph.
        /// ||图形的高
        /// 
        public float graphHeight { get { return m_GraphHeight; } }
        /// 
        /// The position of graph.
        /// ||图形的左下角起始坐标。
        /// 
        public Vector3 graphPosition { get { return m_GraphPosition; } }
        public Rect graphRect { get { return m_GraphRect; } }
        public Vector2 graphSizeDelta { get { return m_GraphSizeDelta; } }
        public Vector2 graphPivot { get { return m_GraphPivot; } }
        public Vector2 graphMinAnchor { get { return m_GraphMinAnchor; } }
        public Vector2 graphMaxAnchor { get { return m_GraphMaxAnchor; } }
        public Vector2 graphAnchoredPosition { get { return m_GraphAnchoredPosition; } }
        /// 
        /// The postion of pointer.
        /// ||鼠标位置。
        /// 
        public Vector2 pointerPos { get; protected set; }
        /// 
        /// Whether the mouse pointer is in the chart.
        /// ||鼠标是否在图表内。
        /// 
        public bool isPointerInChart
        { get { return m_PointerEventData != null; } }
        /// 
        /// 警告信息。
        /// 
        public string warningInfo { get; protected set; }
        /// 
        /// 强制开启鼠标事件检测。
        /// 
        public bool forceOpenRaycastTarget { get { return m_ForceOpenRaycastTarget; } set { m_ForceOpenRaycastTarget = value; } }
        /// 
        /// 鼠标点击回调。
        /// 
        public Action onPointerClick { set { m_OnPointerClick = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标按下回调。
        /// 
        public Action onPointerDown { set { m_OnPointerDown = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标弹起回调。
        /// 
        public Action onPointerUp { set { m_OnPointerUp = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标进入回调。
        /// 
        public Action onPointerEnter { set { m_OnPointerEnter = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标退出回调。
        /// 
        public Action onPointerExit { set { m_OnPointerExit = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标开始拖拽回调。
        /// 
        public Action onBeginDrag { set { m_OnBeginDrag = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标拖拽回调。
        /// 
        public Action onDrag { set { m_OnDrag = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标结束拖拽回调。
        /// 
        public Action onEndDrag { set { m_OnEndDrag = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 鼠标滚动回调。
        /// 
        public Action onScroll { set { m_OnScroll = value; m_ForceOpenRaycastTarget = true; } }
        /// 
        /// 设置图形的宽高(在非stretch pivot下才有效,其他情况需要自己调整RectTransform)
        /// 
        /// 
        /// 
        public virtual void SetSize(float width, float height)
        {
            if (LayerHelper.IsFixedWidthHeight(rectTransform))
            {
                rectTransform.sizeDelta = new Vector2(width, height);
            }
            else
            {
                Debug.LogError("Can't set size on stretch pivot,you need to modify rectTransform by yourself.");
            }
        }
        /// 
        /// 重新初始化Painter
        /// 
        public void SetPainterDirty()
        {
            m_PainerDirty = true;
        }
        /// 
        /// Redraw graph in next frame.
        /// ||在下一帧刷新图形。
        /// 
        public virtual void RefreshGraph()
        {
            m_RefreshChart = true;
        }
        public void RefreshAllComponent()
        {
            SetAllComponentDirty();
            RefreshGraph();
        }
        /// 
        /// 检测警告信息。
        /// 
        /// 
        public string CheckWarning()
        {
            warningInfo = CheckHelper.CheckChart(this);
            return warningInfo;
        }
        /// 
        /// 移除并重新创建所有图表的Object。
        /// 
        public void RebuildChartObject()
        {
            ChartHelper.DestroyAllChildren(transform);
            SetAllComponentDirty();
        }
        public bool ScreenPointToChartPoint(Vector2 screenPoint, out Vector2 chartPoint)
        {
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
            var relative = Display.RelativeMouseAt(screenPoint);
            if (relative != Vector3.zero)
                screenPoint = relative;
#endif
            var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
            if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
                    screenPoint, cam, out chartPoint))
            {
                return false;
            }
            return true;
        }
        /// 
        /// chart local point to screen point.
        /// ||图表内坐标转屏幕坐标。
        /// 
        /// 图表内的坐标
        /// 屏幕坐标
        [Since("v3.7.0")]
        public Vector2 LocalPointToScreenPoint(Vector2 localPoint)
        {
            var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
            var wordPoint = rectTransform.TransformPoint(localPoint);
            return RectTransformUtility.WorldToScreenPoint(cam, wordPoint);
        }
        /// 
        /// chart local point to world point.
        /// ||图表内坐标转世界坐标。
        /// 
        /// 图表内的坐标
        /// 世界坐标
        [Since("v3.7.0")]
        public Vector2 LocalPointToWorldPoint(Vector2 localPoint)
        {
            return rectTransform.TransformPoint(localPoint);
        }
        /// 
        /// 保存图表为图片。
        /// 
        /// type of image: png, jpg, exr
        /// save path
        public void SaveAsImage(string imageType = "png", string savePath = "")
        {
            StartCoroutine(SaveAsImageSync(imageType, savePath));
        }
        private IEnumerator SaveAsImageSync(string imageType, string path)
        {
            yield return new WaitForEndOfFrame();
            ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path);
        }
    }
}