using System;
using System.Collections.Generic;

using Grasshopper.Kernel;
using Rhino.Geometry;

namespace Workshop
{
    public class GhcCentroid : GH_Component
    {
        public GhcCentroid()
            : base("Centroid", "Centroid",
                "Find the centroid of a set of points, and compute the distance from the centroid to each point",
                "Workshop", "Utilities")
        {
        }


        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddPointParameter("Points", "Points", "Points", GH_ParamAccess.list);
        }


        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddPointParameter("Centroid", "Centroid", "Centroid", GH_ParamAccess.item);
            pManager.AddNumberParameter("Distances", "Distances", "Distances", GH_ParamAccess.list);
        }


        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<Point3d> iPoints = new List<Point3d>();
            DA.GetDataList("Points", iPoints);

            Point3d centroid = new Point3d(0.0, 0.0, 0.0);

            foreach (Point3d point in iPoints)
                centroid += point;

            centroid /= iPoints.Count;

            List<double> distances = new List<double>();

            foreach (Point3d point in iPoints)
                distances.Add(centroid.DistanceTo(point));

            DA.SetData("Centroid", centroid);
            DA.SetDataList("Distances", distances);
        }


        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                return null;
            }
        }


        public override Guid ComponentGuid
        {
            get { return new Guid("{3cc555fd-67e3-4651-926a-72a2aa7ae4fd}"); }
        }
    }
}