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 Word = Microsoft.Office.Interop.Word;
using System.IO;

namespace presentacion
{
    public partial class etiquetar : Form
    {
        public etiquetar()
        {
            InitializeComponent();
            guardar.Enabled = false;
            Mostrar.Enabled = false;
            ruta.ReadOnly = true;
            abrir_xml.Enabled = false;
        }

        private void volver_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void Abrir_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog path = new OpenFileDialog();
                //making an object for the OpenFileDialog
                path.Title = "Elige el texto";
                string ruta_ = "..\\..\\..\\..\\..\\";
                path.InitialDirectory = ruta_+"\\textos\\texto_prueba.doc";
                path.RestoreDirectory = true;

                if (path.ShowDialog() == DialogResult.OK)
                {
                    ruta.ReadOnly = false;
                    ruta.Text = path.FileName.ToString();
                    ruta.ReadOnly = true;
                    guardar.Enabled = true;
                    Mostrar.Enabled = true;
                }
            }
            catch (Exception l)
            {

                MessageBox.Show(l.Message);
            }
        }

        private void Mostrar_Click(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Word.Application wordObject = new Word.Application();
                object file = ruta.Text; //this is the path
                object nullobject = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
                    (ref file, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject);


                IDataObject data;
                int contador = 1;
                SortedList<int, string> lineas = new SortedList<int, string>();
                foreach (Microsoft.Office.Interop.Word.Paragraph p in docs.Paragraphs)
                {
                    p.Range.Select();
                    docs.ActiveWindow.Selection.Copy();
                    data = Clipboard.GetDataObject();
                    lineas.Add(contador++, data.GetData(DataFormats.Text).ToString());
                }
                string ruta_ = "..\\..\\..\\..\\..\\";
                classes.Etiquetado etiquetar = new classes.Etiquetado();
                string nombre = docs.Name;
                bool etiquetado = etiquetar.etiquetar(nombre, lineas, true, ruta_);


                //mostrar el texto
                docs.ActiveWindow.Selection.WholeStory();

                docs.ActiveWindow.Selection.Copy();
                data = Clipboard.GetDataObject();
                textBox2.Visible = true;
                textBox2.Text = data.GetData(DataFormats.Text).ToString();
                docs.Close(ref nullobject, ref nullobject, ref nullobject);

            }
            catch (Exception j)
            {
                MessageBox.Show(j.Message);
            }
        }

        private void guardar_Click_1(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Word.Application wordObject = new Word.Application();
                object file = ruta.Text; //this is the path
                object nullobject = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
                    (ref file, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject
                                    );

                IDataObject data;
                int contador = 1;
                SortedList<int, string> lineas = new SortedList<int, string>();
                foreach (Microsoft.Office.Interop.Word.Paragraph p in docs.Paragraphs)
                {
                    p.Range.Select();
                    docs.ActiveWindow.Selection.Copy();
                    data = Clipboard.GetDataObject();
                    lineas.Add(contador++, data.GetData(DataFormats.Text).ToString());
                }
                string ruta_ = "..\\..\\..\\..\\..\\";
                classes.Etiquetado etiquet = new classes.Etiquetado();
                string nombre = docs.Name;
                bool etiquetado = etiquet.etiquetar(nombre, lineas, false,ruta_);

                //mostrar el texto
                docs.ActiveWindow.Selection.WholeStory();

                docs.ActiveWindow.Selection.Copy();
                data = Clipboard.GetDataObject();
                textBox2.Visible = true;
                textBox2.Text = data.GetData(DataFormats.Text).ToString();
                docs.Close(ref nullobject, ref nullobject, ref nullobject);

            }
            catch (Exception j)
            {
                MessageBox.Show(j.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ruta_ = "..\\..\\..\\..\\..\\";
            string[] files = Directory.GetFiles(ruta_+"\\textos", "*.doc");
            
            foreach (string archivo in files)
            {
                try
                {
                    Microsoft.Office.Interop.Word.Application wordObject = new Word.Application();
                    object file = archivo; //this is the path
                    object nullobject = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
                        (ref file, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject
                                        );
                    IDataObject data;
                    Clipboard.Clear();
                    int contador = 1;
                    SortedList<int, string> lineas = new SortedList<int, string>();
                    foreach (Microsoft.Office.Interop.Word.Paragraph p in docs.Paragraphs)
                    {
                        p.Range.Select();
                        docs.ActiveWindow.Selection.Copy();
                        data = Clipboard.GetDataObject();
                        lineas.Add(contador++, data.GetData(DataFormats.Text).ToString());
                    }
                    string nombre = docs.Name;
                    classes.Etiquetado et= new classes.Etiquetado();
                    bool etiquetado = et.etiquetar(nombre, lineas, true,ruta_);

                  docs.Close(ref nullobject, ref nullobject, ref nullobject);

                }
                catch (Exception j)
                {
                    MessageBox.Show(j.Message);
                }

            }
        }

        private void estado_Click_1(object sender, EventArgs e)
        {
            string ruta_ = "..\\..\\..\\..\\..\\";
            string[] files = Directory.GetFiles(ruta_+"\\xml", "*.xml");

            classes.Consultas cons = new classes.Consultas();

            textBox2.Text = "";
            SortedList<string, string> estado = cons.consulta_estado(files);
            foreach (string s in estado.Keys)
            {
                string value;
                estado.TryGetValue(s, out value);
                textBox2.Text += s + " " + value + "\r\n";
            }
            textBox2.Visible = true;
        }

        private void abrir_xml_Click(object sender, EventArgs e)
        {
            string ruta_ = "..\\..\\..\\..\\..\\";
            try
            {
                OpenFileDialog path = new OpenFileDialog();
                //making an object for the OpenFileDialog
                path.Title = "Elige el texto";
                path.InitialDirectory = ruta_+"xml\\texto_prueba.xml";
                path.RestoreDirectory = true;

                if (path.ShowDialog() == DialogResult.OK)
                {
                    ruta.ReadOnly = false;
                    ruta.Text = path.FileName.ToString();
                    ruta.ReadOnly = true;
                    guardar.Enabled = true;
                    Mostrar.Enabled = true;
                    abrir_xml.Enabled = true;
                }
            }
            catch (Exception l)
            {

                MessageBox.Show(l.Message);
            }

        }
        
    }
}
