/**
 * DatasetReader application (C#, .NET Framework 4.6.1)
 * for the motor impairments stroke-gesture dataset.
 *
 * 	    Radu-Daniel Vatavu, Ph.D.
 *      MintViz Lab, MANSiD Research Center
 *	    University Stefan cel Mare of Suceava
 *	    Suceava 720229, Romania
 *	    radu.vatavu@usm.ro
 *      http://www.eed.usv.ro/~vatavu
 *
 * 	    Ovidiu-Ciprian Ungurean, Ph.D.
 *      MintViz Lab, MANSiD Research Center
 *	    University Stefan cel Mare of Suceava
 *	    Suceava 720229, Romania
 *	    ungurean.ovidiu@gmail.com 
 *
 * The academic publication for this dataset, and what should be used to cite it, is:
 *
 *	  Vatavu, R.-D. and Ungurean, O.-C. (2019).  
 *	  Stroke-Gesture Input for People with Motor Impairments: Empirical Results & Research Roadmap.
 *    Proceedings of CHI '19, the 37th ACM Conference on Human Factors in Computing Systems (Glasgow, Scottland, UK). 
 *    New York, NY, USA: ACM Press, Paper 215
 *    https://doi.org/10.1145/3290605.3300445
 *
 * This software is distributed under the "New BSD License" agreement:
 *
 * Copyright (c) 2019, Radu-Daniel Vatavu and Ovidiu-Ciprian Ungurean. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * Neither the name of the University Stefan cel Mare of Suceava, nor 
 *      the names of its contributors may be used to endorse or promote products 
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Radu-Daniel Vatavu OR 
 * Ovidiu-Ciprian Ungurean BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
**/

using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace DatasetReader
{
    /// <summary>
    /// Implements input/output functions for gesture sets.
    /// </summary>
    public class GestureIO
    {
        /// <summary>
        /// Reads a gesture from an XML file
        /// </summary>
        public static Gesture[] Read(string fileName)
        {
            if (!File.Exists(fileName))
                return new Gesture[0];

            List<Gesture> gestures = new List<Gesture>();
            using (XmlTextReader xmlReader = new XmlTextReader(File.OpenText(fileName)))
            {
                List<Point> points = null;
                int strokeId = -1;
                while (xmlReader.Read())
                {
                    switch (xmlReader.Name.ToUpper())
                    {
                        case "GESTURE":
                            switch (xmlReader.NodeType)
                            {
                                case XmlNodeType.Element:
                                    points = new List<Point>();
                                    strokeId = -1;
                                    break;
                                case XmlNodeType.EndElement:
                                    gestures.Add(new Gesture(points.ToArray()));
                                    break;
                            }
                            break;
                        case "STROKE":
                            if (xmlReader.NodeType == XmlNodeType.Element)
                                strokeId++;
                            break;
                        case "POINT":
                            Point point = new Point(float.Parse(xmlReader["X"]), float.Parse(xmlReader["Y"]), float.Parse(xmlReader["T"]), strokeId);
                            points.Add(point);
                            break;
                    }
                }
            }
            return gestures.ToArray();
        }
    }
}