// GET: api/DeleteDocument
        public IHttpActionResult Get()
        {
            DocumentRepository documentData = new DocumentRepository();
            ArchiveDocument archiveDocument = new ArchiveDocument();

            CurrentDocuments documentsToDelete = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());

            string[] faxesToDelete = documentsToDelete.CurrentDocumentList.Split('~');

            for (int i = 0; i < faxesToDelete.Length - 1; i++)
            {
                string[] documentName = faxesToDelete[i].Split('\\');
                archiveDocument.ArchiveTheDocument(documentName[documentName.Length - 1],faxesToDelete[i], Utility.GetUserName());

                System.IO.File.Delete(faxesToDelete[i]);
                documentData.DeleteDocumentInFolder(faxesToDelete[i]);
            }
            return Ok();
        }
        // POST: api/SendFax
        public IHttpActionResult Post([FromBody] Fax faxValues)
        {

            Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Start Creating Fax");

            OperationResult createFaxRecordOperationResult = new Models.OperationResult();
            OperationResult sendFaxToMultiTechOperationResult = new Models.OperationResult();

            FaxRepository faxData = new FaxRepository();
            DocumentRepository documentData = new DocumentRepository();

            string[] faxesToSendList = faxValues.FaxTo.Split('~');
            string permanentFaxPath = string.Empty;

            SendFax sendFax = new SendFax();

            try
            {
                for (int i = 0; i < faxesToSendList.Length - 1; i++)
                {

                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Getting fax information");

                    FaxingInformation faxingInformatonInitial = new FaxingInformation();

                    faxingInformatonInitial.DocumentList = faxValues.DocumentList;
                    faxingInformatonInitial.FaxTo = faxesToSendList[i];
                    faxingInformatonInitial.Notes = faxValues.Notes;
                    faxingInformatonInitial.From = faxValues.From;
                    faxingInformatonInitial.Cover = faxValues.Cover;
                    faxingInformatonInitial.CoverSheetOnly = faxValues.CoverOnly;

                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Getting fax information");                    
                    //Create Fax
                    FaxingInformation faxingInformatonComplete = sendFax.CreateFax(faxingInformatonInitial);

                    System.IO.File.Copy(ConfigurationValues.TemporaryFaxPath
                        + Utility.GetUserName() + "\\" + faxingInformatonComplete.FaxName,
                        ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.FaxName, true);

                    createFaxRecordOperationResult = faxData.SendFax(faxingInformatonComplete, ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.FaxName, faxValues.From, faxValues.Notes);

                    if (createFaxRecordOperationResult.Success)
                    {
                        sendFaxToMultiTechOperationResult = sendFaxMultitech.SendTheFax(faxingInformatonComplete.Name, faxingInformatonComplete.FaxPath,
                            faxValues.From, faxingInformatonComplete.FaxNumber, createFaxRecordOperationResult.MessageList[0], ConfigurationValues.ApplicationPath);
                        if (sendFaxToMultiTechOperationResult.Success)
                        {
                            archiveDocument = new ArchiveDocument();

                            CurrentDocuments faxTodocument = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
                            string[] filesToDelete = faxTodocument.CurrentDocumentList.Split('~');
                            for (int j = 0; j < filesToDelete.Length - 1; j++)
                            {
                                //try
                                //{
                                //    File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + filesToDelete[j]);
                                //}
                                //catch { }
                                try
                                {
                                    string[] fileParts = filesToDelete[j].Split('\\');

                                    archiveDocument.ArchiveTheDocument(fileParts[fileParts.Length - 1], filesToDelete[j], Utility.GetUserName());

                                    File.Delete(filesToDelete[j]);
                                }
                                catch { }

                                documentData.DeleteDocumentInFolder(filesToDelete[j]);
                            }

                            System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                                + Utility.GetUserName()
                                + "\\" + faxingInformatonComplete.FaxName);
                        }
                        else
                        {
                            faxData.UpdateFaxRecord(createFaxRecordOperationResult.MessageList[0]);
                        }
                    }
                    else
                    {
                        return BadRequest(createFaxRecordOperationResult.ErrorMessage);
                    }
                }
            }
            catch (Exception er)
            {
                return InternalServerError(er);
            }

            CurrentDocuments documentsToDelete = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());

            try
            {
                File.Delete(documentData.GetFullPathToDocument(Utility.GetUserName()));
            }
            catch { }

            string[] faxesToDelete = documentsToDelete.CurrentDocumentList.Split('~');

            try
            {
                for (int i = 0; i < faxesToDelete.Length - 1; i++)
                {
                    System.IO.File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\"
                        + Utility.GetUserName() + "\\"
                        + faxesToDelete[i]);
                }
            }
            catch { }
            return Ok();
        }
        private static void ProcessBarcodedDocuments(ImportDocument newDocumentToImport)
        {
            PostOfficev2.API.DataAccess.DocumentRepository documentData = new PostOfficev2.API.DataAccess.DocumentRepository();
            CurrentDocuments currentDocument = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
            newDocumentToImport.FullPath = currentDocument.PathToCurrentPDFDocument;

            int pdfPageCount = 0;
            Document pdfDocument = new Document(newDocumentToImport.FullPath);
            pdfPageCount = pdfDocument.Pages.Count;

            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(newDocumentToImport.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    return;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    //LogErrors("Binary data array is null.");
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    //AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                ContractBuilder oBuilder = new ContractBuilder();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();

                oRequest.Header = new Greenway.PrimeSuite.DataContracts.API.Header();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);

                oRequest.PrimeSuitePatientId = newDocumentToImport.PatientID;
                oRequest.DocumentTypeID = newDocumentToImport.DocTypeId;

                oRequest.DocumentName = "Lab Result -";
                oRequest.DocumentDescription = string.Empty;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

                GreenwayDocuments.DocImporter.Document ocall = new GreenwayDocuments.DocImporter.Document();

                oResponse = ocall.SendPDF(oRequest, greenwayServer);

                ArchiveDocument archiveDocument = new ArchiveDocument();

                string[] listOfDocumentsToArchive = currentDocument.CurrentDocumentList.Split('~');

                for (int i = 0; i < listOfDocumentsToArchive.Length - 1; i++)
                {
                    string[] documentName = listOfDocumentsToArchive[i].Split('\\');
                    archiveDocument.ArchiveTheDocument(documentName[documentName.Length - 1], listOfDocumentsToArchive[i], Utility.GetUserName());
                    File.Delete(listOfDocumentsToArchive[i]);
                }

                return;
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                return;
            }
        }
        public void SendFileToAnotherUser(FaxFolders sendFileToAnotherUser)
        {
            //1.  Move the file to the new folder
            //2   Insert record giving folder that and name of document
            string s1 = string.Empty;

            CurrentDocument currentdocument = new CurrentDocument();
            List<OutboundFaxDocument> outboundFaxDocumentList = new List<OutboundFaxDocument>();
            DocumentRepository documentRepository = new DocumentRepository();
            ArchiveDocument archiveDocument = new ArchiveDocument();

            try {

                using (IDbConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection))
                {

                    const string query = "SELECT [ID],[ActiveDirectoyUser],[CurrentDocumentPathXOD],[CurrentDocumentPathPDF]"
                        + " ,[PathToDocument],[CurrentAnnotation],[CurrentListOfDocuments]"
                        + " FROM [CurrentDocument]"
                        + " where [ActiveDirectoyUser] = @ActiveDirectoyUser";

                    currentdocument = db.Query<CurrentDocument>(query, new { @ActiveDirectoyUser = Utility.GetUserName() }).Single();
                    //File.Move(currentdocument.CurrentDocumentPathPDF, sendFileToAnotherUser.NewPath + "\\" + currentdocumentName[currentdocumentName.Length - 1]);

                    string[] filestoDelete = currentdocument.CurrentListOfDocuments.Split('~');
                    for (int i = 0; i < filestoDelete.Length - 1; i++)
                    {
                        string[] currentDocumentName = filestoDelete[i].Split('\\');

                        archiveDocument.ArchiveTheDocument(currentDocumentName[currentDocumentName.Length - 1], filestoDelete[i], Utility.GetUserName());

                        File.Move(filestoDelete[i], sendFileToAnotherUser.NewPath + "\\" + currentDocumentName[currentDocumentName.Length - 1]);

                        try
                        {
                            documentRepository.DeleteDocumentThatIsInAInFolder(filestoDelete[i], Utility.GetUserName());
                            try
                            {
                                File.Delete(filestoDelete[i]);
                            }
                            catch { }
                        }
                        catch (Exception er)
                        {
                            Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                        }
                    }
                }
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
            }
        }