// -----------------------------------------------------------------------
// 
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
// 
// -----------------------------------------------------------------------
namespace UnityEngine.U2D.Animation.TriangleNet
    .IO
{
    using System;
    using System.Collections.Generic;
    using Animation.TriangleNet.Geometry;
    using Animation.TriangleNet.Meshing;
    internal static class FileProcessor
    {
        static List formats;
        static FileProcessor()
        {
            formats = new List();
            // Add Triangle file format as default.
            formats.Add(new TriangleFormat());
        }
        internal static void Add(IFileFormat format)
        {
            formats.Add(format);
        }
        internal static bool IsSupported(string file)
        {
            foreach (var format in formats)
            {
                if (format.IsSupported(file))
                {
                    return true;
                }
            }
            return false;
        }
        #region Polygon read/write
        /// 
        /// Read a file containing polygon geometry.
        /// 
        /// The path of the file to read.
        /// An instance of the  class.
        internal static IPolygon Read(string filename)
        {
            foreach (IPolygonFormat format in formats)
            {
                if (format != null && format.IsSupported(filename))
                {
                    return format.Read(filename);
                }
            }
            throw new Exception("File format not supported.");
        }
        /// 
        /// Save a polygon geometry to disk.
        /// 
        /// An instance of the  class.
        /// The path of the file to save.
        internal static void Write(IPolygon polygon, string filename)
        {
            foreach (IPolygonFormat format in formats)
            {
                if (format != null && format.IsSupported(filename))
                {
                    format.Write(polygon, filename);
                    return;
                }
            }
            throw new Exception("File format not supported.");
        }
        #endregion
        #region Mesh read/write
        /// 
        /// Read a file containing a mesh.
        /// 
        /// The path of the file to read.
        /// An instance of the  interface.
        internal static IMesh Import(string filename)
        {
            foreach (IMeshFormat format in formats)
            {
                if (format != null && format.IsSupported(filename))
                {
                    return format.Import(filename);
                }
            }
            throw new Exception("File format not supported.");
        }
        /// 
        /// Save a mesh to disk.
        /// 
        /// An instance of the  interface.
        /// The path of the file to save.
        internal static void Write(IMesh mesh, string filename)
        {
            foreach (IMeshFormat format in formats)
            {
                if (format != null && format.IsSupported(filename))
                {
                    format.Write(mesh, filename);
                    return;
                }
            }
            throw new Exception("File format not supported.");
        }
        #endregion
    }
}