// POST: api/MonitorFaxes
 public IHttpActionResult Post(int id,[FromBody]string value)
 {
     FaxData faxData = new FaxData();
     bool returnValue = faxData.RemoveFromMonitoringFaxList(id.ToString());
     if (returnValue)
     {
         return Ok();
     }
     else{
         return BadRequest();
     }
 }
        // GET: ViewPDF
        public ActionResult Index()
        {

            string document = this.Request.QueryString["document"];

            if (string.IsNullOrEmpty(document))
            {

                string documentID = this.Request.QueryString["id"];

                FaxData faxData = new FaxData();
                string faxPath = faxData.GetFaxPath(documentID);
                string faxName = GetFaxName(faxPath);
                ViewBag.FaxName = ConfigurationValues.PdfFolder + faxName;
                @ViewBag.FaxNameWithPath = ConfigurationValues.PdfFolder + faxName;
                System.IO.File.Copy(faxPath, ConfigurationValues.WhereToCopyPDfTo + faxName, true);
                return View();
            }
            else
            {
                FaxData faxData = new FaxData();
                //string faxPath = faxData.GetFaxPath(documentID);
                //string faxName = GetFaxName(faxPath);
                ViewBag.FaxName = ConfigurationValues.TemporaryFaxPath  + Utility.GetUserName()  + "\\" + document;
                @ViewBag.FaxNameWithPath = ConfigurationValues.PdfFolder + document;
                System.IO.File.Copy(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()  + "\\" + document, ConfigurationValues.WhereToCopyPDfTo + document, true);
                return View();
            }

            //string faxName = string.Empty;
            //using (SqlConnection cn = new SqlConnection(Database.WaldenFax))
            //{
            //    cn.Open();
            //    using (SqlCommand cm = cn.CreateCommand())
            //    {
            //        cm.CommandText = "select FaxPath"
            //            + " from FaxesSendServer "
            //            + " where SendID = " + id.ToString();

            //        SqlDataReader dr = cm.ExecuteReader();
            //        dr.Read();
            //        faxName = GetFaxName(dr.GetString(0));
            //        ViewBag.FaxName = Walden.CompleteFax.Library.Database.ApiPath + "pdf/" + faxName;
            //        System.IO.File.Copy(dr.GetString(0), Walden.CompleteFax.Library.Database.WhereToCopyPDFTo + faxName, true);
            //        return View();
            //    }
            //}



            //return View();
        }
        // GET: api/SendFaxAgain
        public string Get()
        {

            var queryString = this.Request.GetQueryNameValuePairs().ToList();

            string sendID = queryString[0].Value;
            string faxNumber = queryString[1].Value;


            FaxData faxData = new FaxData();
            bool returnValue = faxData.SendFaxAgain(faxNumber, sendID);
            if (returnValue)
            {

            }
            else
            {

            }
            //ViewBag.sendID = sendID;
            //ViewBag.faxNumber = faxNumber;
            //string faxName = string.Empty;
            //faxNumber = faxNumber.Replace("1,", "");
            //faxNumber = faxNumber.Replace("9,", "");
            //faxNumber = "9," + faxNumber;

            //using (var ctx = ConnectionManager<SqlConnection>.GetManager(Database.WaldenFax, false))
            //{
            //    using (var cm = ctx.Connection.CreateCommand())
            //    {
            //        cm.CommandText = "update FaxesSendServer"
            //            + " set FaxSent = @FaxSent"
            //            + " ,FaxNumber = @FaxNumber"
            //            + " ,CompletionTime = @CompletionTime"
            //            + " where SendID = @SendID";

            //        cm.CommandType = System.Data.CommandType.Text;
            //        cm.Parameters.AddWithValue("@FaxSent", "N");
            //        cm.Parameters.AddWithValue("@FaxNumber", faxNumber);
            //        cm.Parameters.AddWithValue("@SendID", sendID);
            //        cm.Parameters.AddWithValue("@CompletionTime", string.Empty);
            //        cm.ExecuteNonQuery();
            //    }
            //}

            //return View();


            return "OK";
        }
        // GET: ResendFax
        public ActionResult Index()
        {
            string sendID = this.Request.QueryString["sendID"];
            string faxNumber = this.Request.QueryString["faxnumber"];
            string apiPath = this.Request.QueryString["apiPath"];
            string faxPath = string.Empty;

            FaxData faxData = new FaxData();
            ViewBag.sendID = sendID;
            ViewBag.faxNumber = faxNumber;

            string faxName = string.Empty;

            faxPath = faxData.GetFaxPath(sendID);
            faxName = GetFaxName(faxPath);
            @ViewBag.apiPath = apiPath;
            ViewBag.FaxName = ConfigurationValues.PdfFolder + faxName;
            System.IO.File.Copy(faxPath,ConfigurationValues.WhereToCopyPDfTo + faxName, true);
            return View();
        }
 // GET: api/MonitorFaxes
 public IEnumerable<MonitorFax> Get()
 {
     FaxData faxData = new FaxData();
     return faxData.FaxHistory(Utility.GetUserName());
 }
        //public MiscFaxInformation CreateFax(string faxDocumentList, string _faxTo, string notes,
         //   string from, string cover)
        public FaxingInformation CreateFax(FaxingInformation faxingInformation)
        {
            //MiscFaxInformation miscFaxInformation = new MiscFaxInformation();

            string newPDF = string.Empty;
            string temporaryPath = string.Empty;

            faxingInformation.NameFromDateTimeString
                = Utility.GetFileNameFromDateTimeString() + ".pdf";

            //var repository = new WaldenCompleteFaxWeb.Repository.FaxToInformation();
            var repository = new WaldenFax.API.Models.FaxToInformation();

            FaxData faxData = new FaxData();
            string[] FirstFaxTo = faxingInformation.FaxTo.Split('~');
            List<FaxToInformation> data = new List<FaxToInformation>();

            if (faxingInformation.FaxTo.Substring(0,1) == "G")
            {
                data = faxData.GetFaxToInformationGreenway(FirstFaxTo[0]);
            }
            else
            {
                data = faxData.GetFaxToInformation(FirstFaxTo[0]);            
            }

            try
            {
                faxingInformation.FaxTo = data[0].Name;

            }
            catch
            {

            }
            try
            {
                StringCollection listOfPSFiles = new StringCollection();

                string[] words = faxingInformation.DocumentList.Split('~');

                for (int i = 0; i < words.Length; i++)
                {
                    if (words[i].Length > 0)
                    {
                        listOfPSFiles.Add(words[i]);
                    }
                }

                MemoryStream newPDFStream = CreatePreviewFax(listOfPSFiles);

                temporaryPath = ConfigurationValues.TemporaryFaxPath + Utility.GetUserName() + "\\"
                    + faxingInformation.NameFromDateTimeString;

                FileStream newPDFStreamFile = new FileStream(temporaryPath, FileMode.Create);

                newPDFStream.WriteTo(newPDFStreamFile);
                newPDFStream.Close();
                newPDFStreamFile.Close();

                MemoryStream coverSheet
                    = CreateCoverSheetV2(faxingInformation.FaxTo, data[0].FaxNumber, Walden.Medical.Library.Pdf.GetPDFPageCount(newPDFStreamFile.Name),
                    faxingInformation.Notes, faxingInformation.From, int.Parse(faxingInformation.Cover));


                //miscFaxInformation.PageCount = Walden.Medical.Library.Pdf.GetPDFPageCount(_newPDFStreamFile.Name);
                //miscFaxInformation.FaxNumber =  UpdatedFaxNumber(data[0].FaxNumber);
                //miscFaxInformation.Name = faxTo;

                faxingInformation.PageCount = Walden.Medical.Library.Pdf.GetPDFPageCount(newPDFStreamFile.Name);
                faxingInformation.FaxNumber = UpdatedFaxNumber(data[0].FaxNumber);
                faxingInformation.Name = faxingInformation.FaxTo;
                
                FileStream presStream = new FileStream(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                    + "\\cover.pdf", FileMode.Create);
                coverSheet.WriteTo(presStream);
                coverSheet.Close();
                presStream.Close();
                if (faxingInformation.CoverSheetOnly == "T") 
                {
                    File.Copy(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                            + "\\cover.pdf", newPDFStreamFile.Name, true);
                    File.Delete(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                            + "\\cover.pdf");
                }
                else
                {
                    Walden.Medical.Library.Pdf.MergeTwoDocuments(ConfigurationValues.TemporaryFaxPath + Utility.GetUserName()
                        + "\\cover.pdf", newPDFStreamFile.Name);
                }

                faxingInformation.FaxPath = newPDFStreamFile.Name;
                //miscFaxInformation.FaxPath = _newPDFStreamFile.Name;

                return faxingInformation;
            }
            catch (Exception er)
            {
                return faxingInformation;
            }
        }
 // GET: api/MoveFile/5
 public string Get(string id)
 {
     FaxData faxData = new FaxData();
     return faxData.MoveResendFileToView(id);
 }
        // POST: api/SendFax
        public IHttpActionResult Post([FromBody] Fax faxValues)
        {
            OperationResult createFaxRecordOperationResult = new Models.OperationResult();
            OperationResult sendFaxToMultiTechOperationResult = new Models.OperationResult();
            
            FaxData faxData = new FaxData();

            string documentList = faxValues.DocumentList;
            string faxTo = faxValues.FaxTo;
            string notes = faxValues.Notes;
            string cover = faxValues.Cover;
            string from = faxValues.From;

            string faxPath = string.Empty;
            string[] faxes = faxTo.Split('~');
            string permanentFaxPath = string.Empty;

            Misc misc = new Misc();

            try
            {
                for (int i = 0; i < faxes.Length - 1; i++)
                {
                    FaxingInformation faxingInformatonInitial = new FaxingInformation();

                    faxingInformatonInitial.DocumentList = documentList;
                    faxingInformatonInitial.FaxTo = faxes[i];
                    faxingInformatonInitial.Notes = notes;
                    faxingInformatonInitial.From = from;
                    faxingInformatonInitial.Cover = cover;
                    faxingInformatonInitial.CoverSheetOnly = faxValues.CoverOnly;

                    FaxingInformation faxingInformatonComplete = misc.CreateFax(faxingInformatonInitial);

                    permanentFaxPath = Walden.CompleteFax.Library.Database.PernamentFaxPath
                            + Utility.GetUserName() + "\\" + faxingInformatonComplete.NameFromDateTimeString;

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

                    string now = DateTime.Now.ToShortDateString()
                        + " "
                        + DateTime.Now.ToShortTimeString();

                    createFaxRecordOperationResult = faxData.SendFax(faxingInformatonComplete, ConfigurationValues.PernamentFaxPath
                        + faxingInformatonComplete.NameFromDateTimeString, from, notes);

                    if (createFaxRecordOperationResult.Success)
                    {
                        sendFaxToMultiTechOperationResult = sendFaxMultitech.SendTheFax(faxingInformatonComplete.Name, faxingInformatonComplete.FaxPath,
                            from, faxingInformatonComplete.FaxNumber, createFaxRecordOperationResult.MessageList[0], ConfigurationValues.ApplicationPath);
                        if (sendFaxToMultiTechOperationResult.Success)
                        {
                            System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                                + Utility.GetUserName()
                                + "\\" + faxingInformatonComplete.NameFromDateTimeString);
                        }
                        else
                        {
                            faxData.UpdateFaxRecord(createFaxRecordOperationResult.MessageList[0]);
                        }
                    }
                    else
                    {
                        return BadRequest(createFaxRecordOperationResult.ErrorMessage);
                    }
                }
            }
            catch (Exception er)
            {
                return InternalServerError(er);
            }

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

            for (int i = 0; i < faxesToDelete.Length - 1; i++)
            {
                System.IO.File.Delete(ConfigurationValues.TemporaryFaxPath
                    + Utility.GetUserName() + "\\"
                    + faxesToDelete[i]);
            }

            return Ok();
        }