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.IO;
using System.Diagnostics;
using System.Net;


namespace Word2010AddIn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            List<string> headers = new List<string>();

            foreach (Microsoft.Office.Interop.Word.Section aSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
            {
                foreach (Microsoft.Office.Interop.Word.HeaderFooter aHeader in aSection.Headers)
                    headers.Add(aHeader.Range.Text);
            }

            ///////////////////string test = Globals.ThisAddIn.Application.ActiveDocument.Content.Text;
            ///////////////////richTextBox1.Text = String.Join(Environment.NewLine, headers);
            Globals.ThisAddIn.Application.ActiveDocument.Content.WholeStory();
            Globals.ThisAddIn.Application.ActiveDocument.Content.Copy();
          
            IDataObject Data1;
            Data1 = Clipboard.GetDataObject();
            string str=Data1.GetData(DataFormats.Rtf).ToString();
            richTextBox1.Rtf = str;
           // richTextBox2.Text = String.Join(Environment.NewLine, headers);
           //////////richTextBox1.Text = String.Join(Environment.NewLine, headers);// + test;
           //////webBrowser1.DocumentText=str;
        }
        
        
        private void button1_Click(object sender, EventArgs e)
        {

            this.Close();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            object missing = System.Reflection.Missing.Value;
            object Visible = false;
            object start1 = 0;
            object end1 = 0;

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            Microsoft.Office.Interop.Word.Range rng = adoc.Range(ref start1, ref missing);
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"C:\";
            saveFileDialog1.Title = "Save word Files";
            saveFileDialog1.CheckFileExists = false;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "docx";
            saveFileDialog1.Filter = "Docx files (*.docx)|*.doc|All files (*.*)|*.*";///BAGHIE PASVANDHA RO HAM BEHESH BEDAM
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.OverwritePrompt = true;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                rng.Font.Name = "Calibri (Body)";
                rng.InsertAfter(richTextBox1.Text);
                object filename = saveFileDialog1.FileName;
                adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                WordApp.Visible = false;
                adoc.Activate();
                ((Microsoft.Office.Interop.Word._Application)WordApp).Quit();
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WordApp);
                //Dispose();
               


            }
            
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //string url = "http://erp.payabtarh.com/word.php";
            //string word = "test hello";
            //using (WebClient client = new WebClient())
            //{
            //    client.UploadString(url, word);
            //}



            WebRequest request = WebRequest.Create("http://erp.payabtarh.com/word.php");
            request.Method = "POST";
            string word = richTextBox1.Rtf;
            byte[] byteArray = Encoding.UTF8.GetBytes (word);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream ();
            dataStream.Write (byteArray,0 , byteArray.Length);
            dataStream.Close ();
            WebResponse response = request.GetResponse ();
            MessageBox.Show (((HttpWebResponse)response).StatusDescription);
            dataStream = response.GetResponseStream ();
            StreamReader reader = new StreamReader (dataStream);
            string responseFromServer = reader.ReadToEnd ();
            MessageBox.Show (responseFromServer);
            reader.Close ();
            dataStream.Close ();
            response.Close ();
           // Process.Start("IExplore.exe", "http://erp.payabtarh.com/word.php");
            
            //////////////////////////////////////////////////////////////////////////////



        }

        private void tb1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            //{
            //    //Switch based on the tooltip of the button pressed
            //    //OR: This could be changed to switch on the actual button pressed (e.g. e.Button and the case would be tbbBold)
            //    switch (e.Button.ToolTipText)
            //    {
            //        case "Bold":
            //            {
            //                if (richTextBox1.SelectionFont != null)
            //                {
            //                    richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Bold);
            //                }

            //            }
            //            break;

            //        case "Italic":
            //            {
            //                if (richTextBox1.SelectionFont != null)
            //                {
            //                    richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Italic);
            //                }
            //            }
            //            break;

            //        case "Underline":
            //            {
            //                if (richTextBox1.SelectionFont != null)
            //                {
            //                    richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Underline);
            //                }
            //            }
            //            break;

            //        case "Strikeout":
            //            {
            //                if (richTextBox1.SelectionFont != null)
            //                {
            //                    richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Strikeout);
            //                }
            //            }
            //            break;

            //        case "Super":
            //            {
            //                if (tbbSuperScript.Pushed)
            //                {
            //                    richTextBox1.SetSuperScript(true);
            //                    richTextBox1.SetSubScript(false);
            //                }
            //                else
            //                {
            //                    richTextBox1.SetSuperScript(false);
            //                }
            //            }
            //            break;

            //        case "Sub":
            //            {
            //                if (tbbSubScript.Pushed)
            //                {
            //                    richTextBox1.SetSubScript(true);
            //                    richTextBox1.SetSuperScript(false);
            //                }
            //                else
            //                {
            //                    richTextBox1.SetSubScript(false);
            //                }
            //            }
            //            break;
            //        case "Left":
            //            {
            //                //change horizontal alignment to left
            //                richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
            //            }
            //            break;

            //        case "Right":
            //            {
            //                //change horizontal alignment to right
            //                richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
            //            }
            //            break;

            //        case "Center":
            //            {
            //                //change horizontal alignment to center
            //                richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            //            }
            //            break;

            //        case "Open":
            //            {
            //                try
            //                {
            //                    if ((ofd1.ShowDialog() == DialogResult.OK) && (ofd1.FileName.Length > 0))
            //                    {
            //                        string strExt = System.IO.Path.GetExtension(ofd1.FileName).ToLower();

            //                        if (strExt == ".rtf")
            //                        {
            //                            richTextBox1.LoadFile(ofd1.FileName, RichTextBoxStreamType.RichText);
            //                        }
            //                        else if (strExt == ".txt")
            //                        {
            //                            richTextBox1.LoadFile(ofd1.FileName, RichTextBoxStreamType.PlainText);
            //                        }
            //                        else if ((strExt == ".htm") || (strExt == ".html"))
            //                        {
            //                            // Read the HTML format
            //                            StreamReader sr = File.OpenText(ofd1.FileName);
            //                            string strHTML = sr.ReadToEnd();
            //                            sr.Close();

            //                            richTextBox1.AddHTML(strHTML);
            //                        }
            //                    }
            //                }
            //                catch
            //                {
            //                    MessageBox.Show("There was an error loading the file: " + ofd1.FileName);
            //                }
            //            }
            //            break;

            //        case "Save":
            //            {
            //                if ((sfd1.ShowDialog() == DialogResult.OK) && (sfd1.FileName.Length > 0))
            //                {
            //                    string strExt = System.IO.Path.GetExtension(sfd1.FileName).ToLower();

            //                    if (strExt == ".rtf")
            //                    {
            //                        richTextBox1.SaveFile(sfd1.FileName);
            //                    }
            //                    else if (strExt == ".txt")
            //                    {
            //                        richTextBox1.SaveFile(sfd1.FileName, RichTextBoxStreamType.PlainText);
            //                    }
            //                    else if ((strExt == ".htm") || (strExt == ".html"))
            //                    {
            //                        try
            //                        {
            //                            // save as HTML format
            //                            string strText = richTextBox1.GetHTML(true, true);

            //                            if (File.Exists(sfd1.FileName))
            //                                File.Delete(sfd1.FileName);

            //                            StreamWriter sr = File.CreateText(sfd1.FileName);
            //                            sr.Write(strText);
            //                            sr.Close();
            //                        }
            //                        catch
            //                        {
            //                            MessageBox.Show("There was an error saving the file: " + sfd1.FileName);
            //                        }
            //                    }
            //                }
            //            }
            //            break;

            //        case "Font":
            //            {
            //                if (richTextBox1.SelectionFont != null)
            //                    fontDialog1.Font = richTextBox1.SelectionFont;
            //                else
            //                    fontDialog1.Font = richTextBox1.Font;

            //                if (fontDialog1.ShowDialog() == DialogResult.OK)
            //                {
            //                    if (richTextBox1.SelectionFont != null)
            //                        richTextBox1.SelectionFont = fontDialog1.Font;
            //                    else
            //                        richTextBox1.Font = fontDialog1.Font;
            //                }
            //            }
            //            break;

            //        case "Color":
            //            {
            //                colorDialog1.Color = richTextBox1.SelectionColor;

            //                if (colorDialog1.ShowDialog() == DialogResult.OK)
            //                {
            //                    richTextBox1.SelectionColor = colorDialog1.Color;
            //                }
            //            }
            //            break;
            //    }

            //    UpdateToolbar(); //Update the toolbar buttons
            //}

        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            //Form2 HTMLshow = new Form2(this);
            //HTMLshow.Show();
        }
        
    }
}
