using System.Collections.Generic;
using UnityEngine;
namespace FairyGUI
{
    /// 
    /// 
    /// 
    public class Shape : DisplayObject
    {
        /// 
        /// 
        /// 
        public Shape()
        {
            CreateGameObject("Shape");
            graphics = new NGraphics(gameObject);
            graphics.texture = NTexture.Empty;
            graphics.meshFactory = null;
        }
        /// 
        /// 
        /// 
        public Color color
        {
            get
            {
                return graphics.color;
            }
            set
            {
                graphics.color = value;
                graphics.SetMeshDirty();
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawRect(float lineSize, Color lineColor, Color fillColor)
        {
            RectMesh mesh = graphics.GetMeshFactory();
            mesh.lineWidth = lineSize;
            mesh.lineColor = lineColor;
            mesh.fillColor = null;
            mesh.colors = null;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawRect(float lineSize, Color32[] colors)
        {
            RectMesh mesh = graphics.GetMeshFactory();
            mesh.lineWidth = lineSize;
            mesh.colors = colors;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawRoundRect(float lineSize, Color lineColor, Color fillColor,
            float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius)
        {
            RoundedRectMesh mesh = graphics.GetMeshFactory();
            mesh.lineWidth = lineSize;
            mesh.lineColor = lineColor;
            mesh.fillColor = null;
            mesh.topLeftRadius = topLeftRadius;
            mesh.topRightRadius = topRightRadius;
            mesh.bottomLeftRadius = bottomLeftRadius;
            mesh.bottomRightRadius = bottomRightRadius;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        public void DrawEllipse(Color fillColor)
        {
            EllipseMesh mesh = graphics.GetMeshFactory();
            mesh.lineWidth = 0;
            mesh.startDegree = 0;
            mesh.endDegreee = 360;
            mesh.fillColor = null;
            mesh.centerColor = null;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawEllipse(float lineSize, Color centerColor, Color lineColor, Color fillColor, float startDegree, float endDegree)
        {
            EllipseMesh mesh = graphics.GetMeshFactory();
            mesh.lineWidth = lineSize;
            if (centerColor.Equals(fillColor))
                mesh.centerColor = null;
            else
                mesh.centerColor = centerColor;
            mesh.lineColor = lineColor;
            mesh.fillColor = null;
            mesh.startDegree = startDegree;
            mesh.endDegreee = endDegree;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawPolygon(IList points, Color fillColor)
        {
            PolygonMesh mesh = graphics.GetMeshFactory();
            mesh.points.Clear();
            mesh.points.AddRange(points);
            mesh.fillColor = null;
            mesh.colors = null;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawPolygon(IList points, Color32[] colors)
        {
            PolygonMesh mesh = graphics.GetMeshFactory();
            mesh.points.Clear();
            mesh.points.AddRange(points);
            mesh.fillColor = null;
            mesh.colors = colors;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawPolygon(IList points, Color fillColor, float lineSize, Color lineColor)
        {
            PolygonMesh mesh = graphics.GetMeshFactory();
            mesh.points.Clear();
            mesh.points.AddRange(points);
            mesh.fillColor = null;
            mesh.lineWidth = lineSize;
            mesh.lineColor = lineColor;
            mesh.colors = null;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void DrawRegularPolygon(int sides, float lineSize, Color centerColor, Color lineColor, Color fillColor, float rotation, float[] distances)
        {
            RegularPolygonMesh mesh = graphics.GetMeshFactory();
            mesh.sides = sides;
            mesh.lineWidth = lineSize;
            mesh.centerColor = centerColor;
            mesh.lineColor = lineColor;
            mesh.fillColor = null;
            mesh.rotation = rotation;
            mesh.distances = distances;
            graphics.color = fillColor;
            graphics.SetMeshDirty();
        }
        /// 
        /// 
        /// 
        public void Clear()
        {
            graphics.meshFactory = null;
        }
        /// 
        /// 
        /// 
        public bool isEmpty
        {
            get { return graphics.meshFactory == null; }
        }
        protected override DisplayObject HitTest()
        {
            if (graphics.meshFactory == null)
                return null;
            Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
            IHitTest ht = graphics.meshFactory as IHitTest;
            if (ht != null)
                return ht.HitTest(_contentRect, localPoint) ? this : null;
            else if (_contentRect.Contains(localPoint))
                return this;
            else
                return null;
        }
    }
}