using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;


namespace TheDarkWraith.WM16MEV
{
    public partial class EEPROM : Form
    {
        #region Constants
        #endregion

        #region Variables
        XmlDocument xdf1 = null;
        XmlDocument xdf2 = null;
        #endregion

        #region Constructor
        public EEPROM()
        {
            InitializeComponent();
        }
        #endregion

        #region Shown
        private void EEPROM_Shown(object sender, EventArgs e)
        {
            // open and load the XDF file
            xdf1 = OpenAndLoadXDFFile();

            // insure XDF file is valid and correct version
            XmlNodeList xdfformat = xdf1.GetElementsByTagName("XDFFORMAT");
            if (xdfformat == null || xdfformat.Count == 0 || xdfformat[0].Attributes[0].Name.ToLower() != "version" || xdfformat[0].Attributes[0].Value != "1.50")
            {
                return;
            }

            // get the header
            XmlNodeList xdfheader = xdf1.GetElementsByTagName("XDFHEADER");
            if (xdfheader == null || xdfheader.Count == 0)
            {
                return;
            }
            XmlNodeList xdfbaseoffset = xdf1.GetElementsByTagName("baseoffset");
            if (xdfbaseoffset == null || xdfbaseoffset.Count == 0 || xdfbaseoffset[0].InnerText == null || xdfbaseoffset[0].InnerText == "")
            {
                return;
            }
            //int baseoffset = 

            XmlNodeList nodelist2 = xdf1.GetElementsByTagName("XDFTABLE");
            //nodelist2[0].ChildNodes[0].Name

            if (xdf1 == null)
            {
            }
        }
        #endregion

        #region OpenAndLoadXDFFile
        private XmlDocument OpenAndLoadXDFFile()
        {
            String xdfpath = GetXDFPath();

            if (xdfpath == null || xdfpath == "")
            {
                return null;
            }

            return ReadInXDF(xdfpath);
        }
        #endregion

        #region GetXDFPath
        private String GetXDFPath()
        {
            OpenFileDialog ofd = null;

            try
            {
                ofd = new OpenFileDialog();
                ofd.AddExtension = true;
                ofd.AutoUpgradeEnabled = false;
                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                ofd.DefaultExt = "xdf";
                ofd.Filter = "XDF files|*.xdf";
                ofd.Multiselect = false;
                ofd.ReadOnlyChecked = false;
                ofd.RestoreDirectory = true;
                ofd.ShowHelp = false;
                ofd.SupportMultiDottedExtensions = false;
                ofd.Title = "Select XDF file";
                ofd.ValidateNames = true;
                DialogResult dr = ofd.ShowDialog();
                if (dr != DialogResult.OK || ofd.FileName == null || ofd.FileName == "")
                {
                    return null;
                }

                return ofd.FileName;
            }
            finally
            {
                if (ofd != null)
                {
                    ofd.Dispose();
                    ofd = null;
                }
            }
        }
        #endregion

        #region ReadInXDF
        private XmlDocument ReadInXDF(String filepath)
        {
            if (filepath == null || filepath == "" || !File.Exists(filepath) || !filepath.ToLower().EndsWith(".xdf"))
            {
                return null;
            }

            // create an XML document object
            XmlDocument xdftemp = new XmlDocument();

            // read in XDF specified
            try
            {
                xdftemp.Load(filepath);
            }
            catch (Exception e)
            {
                MessageBox.Show("ReadInXDF: Exception while loading XDF file. Exception is " + e.Message, "Error", MessageBoxButtons.OK);
                return null;
            }

            return xdftemp;
        }
        #endregion
    }
}
