using System;
using System.Reflection;
using Word;

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

        public WordHandler()
        {
            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;
            bool _boolToF = 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);
        }
    }
}