//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Pluto.Api
{
    using System;
    using RemObjects.SDK;
    using RemObjects.SDK.Types;
    using RemObjects.SDK.Server;
    using RemObjects.SDK.Server.ClassFactories;
    using PlutoServer.MSL.Connectors;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;

    [RemObjects.SDK.Server.ClassFactories.StandardClassFactory()]
    [RemObjects.SDK.Server.Service(Name = "Billing", InvokerClass = typeof(Billing_Invoker), ActivatorClass = typeof(Billing_Activator))]
    public class Billing : RemObjects.SDK.Server.Service, IBilling
    {
        private System.ComponentModel.Container components = null;
        public Billing() :
            base()
        {
            this.InitializeComponent();
        }
        private void InitializeComponent()
        {
        }

        protected override void Dispose(bool aDisposing)
        {
            if (aDisposing)
            {
                if ((this.components != null))
                {
                    this.components.Dispose();
                }
            }
            base.Dispose(aDisposing);
        }

        public virtual bool PostBilling(string apiKey, string User, BillingPost1 billingInfo)
        {
            if (ProductType.IsKeyLytec(apiKey))
            {
                return LytecConnector.PostBilling(apiKey, User, billingInfo);
            }
            else if (ProductType.IsKeyMedisoft(apiKey))
            {
                return MedisoftConnector.PostBilling(apiKey, User, billingInfo);
            }

            MSLSpecific.Logger.Error("Invalid Product");
            return false;
        }

        #region Superbill Templates

        /// <summary>
        /// Load a specified superbill for the specified practice
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public virtual System.Xml.XmlNode LoadSuperBill(string apiKey, string Name)
        {
            try
            {
                if (!String.IsNullOrEmpty(Name))
                {
                    string strPath = ProductType.GetProductSpecificSuperBillDirectoryPath(apiKey);                    
                    string file = strPath + "\\" + Name + "." + ProductType.MOBILE_SB_FILE_TYPE;
                    if (System.IO.File.Exists(file))
                    {
                        XmlDocument xmlDocument = new XmlDocument();
                        xmlDocument.Load(file);
                        return xmlDocument;
                    }
                }
            }
            catch (Exception e) 
            {
                MSLSpecific.Logger.Error("LoadSuperBill", e);
            }
            return null;
        }

        /// <summary>
        /// Save a specified superbill for a specified practice
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="Name"></param>
        /// <param name="Data"></param>
        public virtual void SaveSuperBill(string apiKey, string Name, System.Xml.XmlNode Data)
        {
            try
            {
                if (Data != null && !String.IsNullOrEmpty(Name))
                {
                    string strPath = ProductType.GetProductSpecificSuperBillDirectoryPath(apiKey);
                    XmlDocument xmlDocument = new XmlDocument();
                    XmlNode oNode = xmlDocument.ImportNode(Data, true);
                    xmlDocument.AppendChild(oNode);
                    xmlDocument.Save(strPath + "\\" + Name + "." + ProductType.MOBILE_SB_FILE_TYPE);
                }
            }
            catch (Exception e) 
            {
                MSLSpecific.Logger.Error("SaveSuperBill", e);
            }
        }

        /// <summary>
        /// Retrieve a list of available superbills for a specified practice
        /// </summary>
        /// <param name="apiKey"></param>
        /// <returns></returns>
        public virtual string[] GetListOfAvailableSuperBills(string apiKey)
        {
            try
            {
                string strPath = ProductType.GetProductSpecificSuperBillDirectoryPath(apiKey);
                string[] files = Directory.GetFiles(strPath, "*." + ProductType.MOBILE_SB_FILE_TYPE, SearchOption.TopDirectoryOnly);
                List<string> FileNames = new List<string>();                
                foreach (string file in files)
                         FileNames.Add(Path.GetFileNameWithoutExtension(file));
                return FileNames.ToArray();
            }
            catch (Exception e)
            {
                MSLSpecific.Logger.Error("GetListOfAvailableSuperBills", e);
            }
            return null;
        }

        /// <summary>
        /// Delete a specified superbill for a specified practice
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="Name"></param>
        public virtual void DeleteSuperbill(string apiKey, string Name)
        {
            try
            {
                string strPath = ProductType.GetProductSpecificSuperBillDirectoryPath(apiKey);
                string[] files = Directory.GetFiles(strPath, "*." + ProductType.MOBILE_SB_FILE_TYPE, SearchOption.TopDirectoryOnly);
                foreach (string file in files)
                {
                    var fileName = Path.GetFileNameWithoutExtension(file);
                    if (fileName.Equals(Name))
                    {
                        File.Delete(file);
                    }
                }
            }
            catch (Exception e) 
            {
                MSLSpecific.Logger.Error("Deleting Superbill", e);
            }
        }

        #endregion
    }
}
