﻿
namespace ThreeDimensionStruct.Models
{
    using System;
    public struct Point3D
    {
        const int numberOfDimensions = 3;

        private static readonly Point3D o = new Point3D(0, 0, 0);

        public Point3D(double x, double y, double z)
            : this()
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }

        public static Point3D O
        {
            get
            {
                return o;
            }
        }

        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }

        public override string ToString()
        {
            return String.Format("{{X = {0}, Y = {1}, Z = {2}}}", this.X, this.Y, this.Z);
        }
        public static Point3D Parse(string stringToParse)
        {
            //The parse method is based on the assumption that the Points will be provided in the format
            //{X = double, Y = double, Z = double}
            var startIndex = 0;
            var endIndex = 0;
            var length = 0;
            var arrayOfCoordinates = new double[3];

            for (int i = 0; i < numberOfDimensions; i++)
            {
                startIndex = stringToParse.IndexOf("=", startIndex) + 2;
                endIndex = stringToParse.IndexOf(",", endIndex + 1);
                if (endIndex == -1)
                {
                    endIndex = stringToParse.Length - 1;
                }
                length = endIndex - startIndex;
                arrayOfCoordinates[i] = double.Parse(stringToParse.Substring(startIndex, length));
            }
            return new Point3D(arrayOfCoordinates[0], arrayOfCoordinates[1], arrayOfCoordinates[2]);
        }
    }
}
