// clsMain.cs
// Copyright (C) 2008 Karel Jordaan http://zanzi.ops4it.com
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml;
using ResControlLogic;
//using System.Data.SqlClient;

namespace ZANZIRes
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class clsMain : clsGlobalErr
    {
        #region Variable declaration

        private const string FILE_XML_CATAGORIES_STRUCTURE = "Categories.xml";
        private const string FILE_XML_COUNTRIES_STRUCTURE = "Countries.xml";
        private const string FILE_XSD_CATAGORIES_STRUCTURE = "Categories.xsd";
        private const string FILE_XSD_COUNTRIES_STRUCTURE = "Countries.xsd";
        private Stream m_resFile;
        private Assembly m_thisFile;

        #endregion

        #region Functions

        /// <summary>
        /// Class retrives a stream of resource from self 
        /// </summary>
        /// <param name="str">the filename of the resource to retrieve</param>
        private void GetResoures(string str)
        {
            try
            {
                m_thisFile = Assembly.GetExecutingAssembly();
                string s = str.Remove(0, str.Length - 3);
                m_resFile = m_thisFile.GetManifestResourceStream("ZANZIRes." + s + "." + str);
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                ErrByte = 1;
            }
        }

        /// <summary>
        /// Function that creates a ArrayLists from XML/XSD files
        /// </summary>
        /// <param name="tf">The  enumeration of the field in the resource to get</param>
        /// <param name="ta">The  enumeration of the resource to get</param>
        /// <returns>The relefant ArrayList of values</returns>
        public ArrayList GetCatagory(TypeArray ta, TypeField tf)
        {
            string resXml = "";
            string resXsd = "";
            ArrayList aCat = new ArrayList();

            try
            {
                switch (ta)
                {
                    case TypeArray.Category:
                        resXml = FILE_XML_CATAGORIES_STRUCTURE;
                        resXsd = FILE_XSD_CATAGORIES_STRUCTURE;
                        break;
                    case TypeArray.Country:
                        resXml = FILE_XML_COUNTRIES_STRUCTURE;
                        resXsd = FILE_XSD_COUNTRIES_STRUCTURE;
                        break;
                }


                XmlDataDocument xdoc = new XmlDataDocument();
                GetResoures(resXsd);
                xdoc.DataSet.ReadXmlSchema(m_resFile);
                GetResoures(resXml);
                xdoc.Load(m_resFile);

                XmlNode root = xdoc.DocumentElement;
                if (root == null) return aCat;
                IEnumerator ienum = root.GetEnumerator();
                XmlNode category;


                while (ienum.MoveNext())
                {
                    switch (tf)
                    {
                        case TypeField.ID:

                            category = (XmlNode) ienum.Current;
                            aCat.Add(category.FirstChild.InnerText);

                            break;

                        case TypeField.Name:

                            category = (XmlNode) ienum.Current;
                            aCat.Add(category.LastChild.InnerText);

                            break;
                    }
                }

                return aCat;
            }

            catch (Exception  ex)
            {
                 new clsExeption(ex);
                ErrByte = 1;
                return null;
            }
        }

//		/// <summary>
//		/// extract ico files from this assembly and inserts into table
        /// for treeview usage
//		/// </summary>
//		/// <param name="tblFrom">Table source</param>
//		/// <param name="tblTo">Table destination</param>
//		private void  FillTable(DataTable tblFrom, DataTable tblTo)
//		{
//			try
//			{
//				for(int i= 0;i <= tblFrom.Rows.Count -1; i++ )
//				{
//					DataRow newrow = tblTo.NewRow();
//					object[] obj = tblFrom.Rows[i].ItemArray ;
//					GetResoures(obj[2].ToString());
//					obj[2] = m_resFile;
//					newrow.ItemArray  =  obj;
//
//					tblTo.Rows.Add (newrow);
//				}
//			}
//			catch (Exception  ex)
//			{
//				GUIControlLogic. new GUIControlLogic.clsExeption(ex);
//				ErrByte = 1;
//			//	return dirs;
//			}
//
//		}

        #endregion

        #region Enumerations

        #region TypeArray enum

        public enum TypeArray
        {
            Country = 0,
            Category = 1
        }

        #endregion

        #region TypeField enum

        public enum TypeField
        {
            ID = 0,
            Name = 1
        }

        #endregion

        #endregion
    }
}