using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace EduNetworkBuilder
{
    /// <summary>
    /// A class holding a project that needs to be done as either a test, quiz, or homework
    /// </summary>
    public class SchoolworkClass
    {
        public Network theProject = null;
        /// <summary>
        /// The name of the project.  Homework3, Quiz 4, etc.
        /// </summary>
        public string Name = "";

        /// <summary>
        /// The Suggested filename for this homework.  The Name + ".eduh"
        /// </summary>
        public string FileName { get { return Name + ".eduh"; } }
        /// <summary>
        /// The short description of the project.
        /// </summary>
        public string Description = "";
        /// <summary>
        /// The class to which this project belongs.  If the student is using this for multiple classes,
        /// but more likely just for the teacher to organize things with.
        /// </summary>
        public string Class = "";
        /// <summary>
        /// The date / time that the project is due.
        /// </summary>
        public DateTime DueDate;

        public bool isMaster = false; //Is this the original project?  If so, we never replace it.

        public bool IsSumbitted = false; //Is the student submitting this?  If so, date-stamp it and lock it.
        public DateTime SaveDate; //The date this file was saved.
        public UInt64 ThisID { get; protected set; }

        protected SchoolworkClass() { }

        public SchoolworkClass(XmlNode TheNode)
        {
            Load(TheNode);
        }

        public SchoolworkClass(string  FileName)
        {
            Load(FileName);
        }

        public SchoolworkClass(Network ToAdd, PersonClass WhoFor)
        {
            if(WhoFor != null && WhoFor.UserSettings != null)
                ThisID = WhoFor.UserSettings.IssueID();
            theProject = ToAdd.Clone();
            Name = theProject.PuzzleName;
            Description = theProject.PuzzleName;
            isMaster = true;
        }

        public SchoolworkClass Clone()
        {
            SchoolworkClass dest = new SchoolworkClass();
            Clone(this, dest);
            return dest;
        }

        public static void Clone(SchoolworkClass source, SchoolworkClass dest)
        {
            dest.ThisID = source.ThisID;
            dest.Class = source.Class;
            dest.Description = source.Description;
            dest.DueDate = source.DueDate;
            dest.isMaster = source.isMaster;
            dest.IsSumbitted = source.IsSumbitted;
            dest.Name = source.Name;
            dest.SaveDate = source.SaveDate;
            dest.theProject = source.theProject.Clone();
        }

        #region Load and Save 
        //Load and save functions
        public void Load(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            if (File.Exists(filename))
            {
                xmlDoc.Load(filename);
                Load(xmlDoc);
            }
        }

        public void Load(XmlNode TheNode)
        {
            foreach (XmlNode Individual in TheNode.ChildNodes)
            {
                XmlNodeType myNodetype = Individual.NodeType;
                if (myNodetype == XmlNodeType.Element)
                {
                    switch (Individual.Name.ToLower())
                    {
                        case "EduNetworkBuilderSchoolwork":
                        case "schoolwork":
                            Load(Individual);
                            break;
                        case "id":
                            UInt64 tID;
                            UInt64.TryParse(Individual.InnerText, out tID);
                            ThisID = tID;
                            break;
                        case "name":
                            Name = Individual.InnerText;
                            break;
                        case "description":
                            Description = Individual.InnerText;
                            break;
                        case "class":
                            Class = Individual.InnerText;
                            break;
                        case "duedate":
                            DateTime.TryParse(Individual.InnerText, out DueDate); 
                            break;
                        case "savedate":
                            DateTime.TryParse(Individual.InnerText, out SaveDate);
                            break;
                        case "ismaster":
                            bool.TryParse(Individual.InnerText, out isMaster);
                            break;
                        case "issubmitted":
                            bool.TryParse(Individual.InnerText, out IsSumbitted);
                            break;
                        case "network":
                            theProject = new Network();
                            theProject.Load(Individual, "",false,true);
                            break;
                    }
                }
            }
        }

        public void Save(string filename)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            XmlWriter writer = XmlWriter.Create(filename, settings);

            //Now we write the file:
            writer.WriteStartDocument();
            writer.WriteStartElement("EduNetworkBuilderSchoolwork");
            writer.WriteComment("This is a schoolwork file for EduNetworkBuilder.");
            Save(writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        public void Save(XmlWriter writer)
        {
            //Save the language name
            //save the number of items
            //Save all the items
            writer.WriteStartElement("schoolwork");
            writer.WriteElementString("ID", ThisID.ToString());
            writer.WriteElementString("Name", Name);
            writer.WriteElementString("Description", Description);
            writer.WriteElementString("Class", Class);
            writer.WriteElementString("DueDate", DueDate.ToString());
            writer.WriteElementString("SaveDate", SaveDate.ToString());
            writer.WriteElementString("IsMaster", isMaster.ToString());
            writer.WriteElementString("IsSubmitted", IsSumbitted.ToString());
            if (theProject != null) theProject.Save(writer);
            writer.WriteEndElement();
        }
        #endregion
    }
}
