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 Form1 : Form
    {
        classes.Etiquetado etiquetar = new classes.Etiquetado();
        public Form1()
        {
            
            InitializeComponent();
            guardar.Enabled = false;
            Mostrar.Enabled = false;
            ruta.ReadOnly = true;
            consulta.Text = "Introduce la consulta";
            label1.Visible = false;

        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog path = new OpenFileDialog();
                //making an object for the OpenFileDialog
                path.Title = "Elige el texto";
                path.InitialDirectory = @"c:\\pfc\\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 button2_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());
                }
                classes.Etiquetado etiquetar = new classes.Etiquetado();
                string nombre = docs.Name;
                bool etiquetado=etiquetar.etiquetar(nombre,lineas, true);
                

                //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(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 nombre = docs.Name;
                bool etiquetado = etiquetar.etiquetar(nombre, lineas, false);
                //consulta.Visible=true;
                //label1.Visible=true;
                
                //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 consulta_TextChanged(object sender, EventArgs e)
        {
            string[] categorias = consulta.Text.Split();
           // etiquetar.buscar_primera_linea(categorias);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles("C:\\pfc\\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;
                    bool etiquetado = etiquetar.etiquetar(nombre, lineas, true);

                //    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 estado_Click(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles("C:\\pfc\\xml", "*.xml");

            classes.Consultas cons=new classes.Consultas();
            
            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;
        }
    }
}
