using System;
using System.Reflection;
using Word;

namespace Baza_Reklam.Classes.Helpers
{
    internal class WordHelper
    {
        private readonly Application oWord;
        private Object oFalse = false;
        private Object oMissing = Missing.Value;
        private Object oTrue = true;
        private Document oWordDoc;

        public WordHelper()
        {
            oWord = new Application();
        }

        public void OpenFile(string filename)
        {
            Object oPlik = filename;
            oWordDoc = oWord.Documents.Open(ref oPlik, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                            ref oMissing, ref oTrue);
        }

        public void SetWordVisible()
        {
            if (!oWord.Visible) oWord.Visible = true;
        }

        public void FindAndReplace(string strFind, string strReplace)
        {
            Object oFindText = strFind;
            Object oReplaceText = strReplace;
            Object oReplace = WdReplace.wdReplaceAll;
            oWordDoc.Content.Find.Execute(ref oFindText, ref oFalse, ref oFalse, ref oFalse, ref oFalse,
                                          ref oFalse, ref oFalse, ref oMissing, ref oFalse,
                                          ref oReplaceText, ref oReplace, ref oFalse, ref oFalse,
                                          ref oFalse, ref oFalse);
        }

        public void SaveAs(string filename)
        {
            Object oFileNameAs = filename;
            oWordDoc.SaveAs(ref oFileNameAs, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        }
    }
}