using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl; 
using System.IO;
namespace UZVAS
{
    public partial class FormDataSet : Form
    {
        public bool writing = false;
        private int finalCount = 1;
        private int actualCount = 0;
        private bool wcount = false;

        public FormDataSet()
        {
            InitializeComponent();
        }

        public void AddRow(int[] data)
        {
            if (wcount == true)
            {
                actualCount++;
                if (actualCount > finalCount)
                {
                    wcount = false;
                    writing = false;
                    toolStripButtonSave_Click(null, null);
                    return;
                }
            }

            DataRow rowTemp = dataSetMeasured.Tables[0].NewRow();
            for (int i = 1; i <= data.Length; i++)
            {
                if(data[i - 1] == 65535)
                    rowTemp["t" + i.ToString()] = 0;
                else
                    rowTemp["t" + i.ToString()] = data[i - 1];
            }
            dataSetMeasured.Tables[0].Rows.Add(rowTemp);
            dataGridViewMeasured.Refresh();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void FormDataSet_Load(object sender, EventArgs e)
        {
            dataGridViewMeasured.DataSource = dataSetMeasured.Tables[0];
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialogXML.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StreamWriter myStreamWriter = new StreamWriter(saveFileDialogXML.FileName);
                this.dataSetMeasured.WriteXml(myStreamWriter);
                myStreamWriter.Close();
                MessageBox.Show("Saved!", "Saving XML file", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void toolStripButtonLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialogXML.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dataSetMeasured.Tables[0].Clear();
                StreamReader myStreamReader = new StreamReader(openFileDialogXML.FileName);
                dataSetMeasured.ReadXml(myStreamReader);
                myStreamReader.Close();
            }
        }

        private void toolStripButtonClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Clear database", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                dataSetMeasured.Tables[0].Clear();
        }

        private void toolStripButtonStartStop_Click(object sender, EventArgs e)
        {
            if (toolStripButtonStartStop.Text == "Start")
            {
                toolStripButtonStartStop.Text = "Stop";
                writing = true;
            }
            else
            {
                toolStripButtonStartStop.Text = "Start";
                writing = false;
            }
        }

        private void toolStripButtonGo_Click(object sender, EventArgs e)
        {
            wcount = true;
            finalCount = int.Parse(toolStripTextBoxReadCount.Text);
            actualCount = 0;
            if (finalCount > actualCount)
                writing = true;
        }

        
    }

    public class WorkbookEngine
    {

    public static void CreateWorkbook(DataSet ds, String filename)
    {
        XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
        XslTransform xt = new XslTransform();
        StreamReader reader =new StreamReader(typeof (WorkbookEngine).Assembly.GetManifestResourceStream(typeof (WorkbookEngine), "Excel.xsl"));
        XmlTextReader xRdr = new XmlTextReader(reader);
        xt.Load(xRdr, null, null);
        StringWriter sw = new StringWriter();
        xt.Transform(xmlDataDoc, null, sw, null);
        StreamWriter myWriter = new StreamWriter (filename+".xsl");
        myWriter.Write (sw.ToString());
        myWriter.Close ();
    }
   
    }
}
