using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;

/// Summary description for Logger
public class Logger
{
    #region Declaration
    static string flow;
    static string RMAId;
    static string status;
    static string logMessage;
    static string methodName;
    static string historyTrack;
    public static string APP_PATH_ByDate = string.Empty;
    public static string APP_PATH_ByMonth = string.Empty;
    public static string APP_PATH_FileName = string.Empty;
    public static string APP_PATH_RMA_FileName = string.Empty;
    public static string APP_PATH = System.Web.HttpContext.Current.Server.MapPath("~/Logs");
    #endregion

    #region Log Type Enum
    public enum LogType { Info = 0, Error }
    #endregion

    #region Get the Type of log
    public static void WriteToLog(LogType logType, string strFlow, string _strRMAId, string strMethodName, string strMessage)
    {
        try
        {
            APP_PATH_RMA_FileName = APP_PATH;
            APP_PATH_ByMonth = APP_PATH_RMA_FileName + "\\" + DateTime.Now.ToString("MMM yyyy");
            APP_PATH_ByDate = APP_PATH_ByMonth + "\\" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year;
            flow = strFlow;
            RMAId = _strRMAId;
            methodName = strMethodName;
            logMessage = strMessage;
            switch (logType)
            {
                case LogType.Info:
                    status = "Application";
                    APP_PATH_FileName = APP_PATH_ByDate + "\\" + status + "_Log.xml";
                    WriteToApplicationLog();
                    break;
                case LogType.Error:
                    status = "Error";
                    APP_PATH_FileName = APP_PATH_ByDate + "\\" + status + "_Log.xml";
                    WriteToErrorLog();
                    break;
            }
        }
        catch
        {
            // error msg
        }
    }
    #endregion

    #region Application Log
    private static void WriteToApplicationLog()
    {
        if (!Directory.Exists(APP_PATH))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH);
        }
        if (!Directory.Exists(APP_PATH_RMA_FileName))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_RMA_FileName);
        }
        if (!Directory.Exists(APP_PATH_ByMonth))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_ByMonth);
        }
        if (!Directory.Exists(APP_PATH_ByDate))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_ByDate);
        }
        if (File.Exists(APP_PATH_FileName))
        {
            XDocument doc1 = XDocument.Load(APP_PATH_FileName);
            XElement element1 = doc1.Root;
            element1.Add(new XElement("Info",
                       new XElement("Flow", flow),
                       new XElement("RMAId", RMAId),
                       new XElement("Date", DateTime.Now.ToString()),
                       new XElement("MethodName", methodName),
                       new XElement("Message", logMessage)));
            doc1.Save(APP_PATH_FileName);
        }
        else
        {
            XmlDocument doc = new XmlDocument();
            XmlElement logroot = doc.CreateElement("log");
            XmlElement Info = doc.CreateElement("Info");
            XmlElement flow = doc.CreateElement("Flow");
            flow.InnerText = Logger.flow;
            XmlElement RMAId = doc.CreateElement("RMAId");
            RMAId.InnerText = Logger.RMAId;
            XmlElement date = doc.CreateElement("Date");
            date.InnerText = DateTime.Now.ToString();
            XmlElement methodname = doc.CreateElement("MethodName");
            methodname.InnerText = methodName;
            XmlElement Message = doc.CreateElement("Message");
            Message.InnerText = logMessage;
            Info.AppendChild(flow);
            Info.AppendChild(RMAId);
            Info.AppendChild(date);
            Info.AppendChild(methodname);
            Info.AppendChild(Message);
            logroot.AppendChild(Info);
            doc.AppendChild(logroot);
            doc.Save(APP_PATH_FileName);
        }
    }
    #endregion

    #region Error Log
    private static void WriteToErrorLog()
    {
        if (!Directory.Exists(APP_PATH))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH);
        }
        if (!Directory.Exists(APP_PATH_RMA_FileName))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_RMA_FileName);
        }
        if (!Directory.Exists(APP_PATH_ByMonth))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_ByMonth);
        }
        if (!Directory.Exists(APP_PATH_ByDate))
        {
            DirectoryInfo di = Directory.CreateDirectory(APP_PATH_ByDate);
        }
        if (File.Exists(APP_PATH_FileName))
        {
            XDocument doc1 = XDocument.Load(APP_PATH_FileName);
            XElement element1 = doc1.Root;
            element1.Add(new XElement("Error",
                       new XElement("Flow", flow),
                       new XElement("RMAId", RMAId),
                       new XElement("Date", DateTime.Now.ToString()),
                       new XElement("MethodName", methodName),
                       new XElement("FieldChanges", historyTrack),
                       new XElement("Message", logMessage)));
            doc1.Save(APP_PATH_FileName);
        }
        else
        {
            XmlDocument doc = new XmlDocument();
            XmlElement logroot = doc.CreateElement("log");
            XmlElement Info = doc.CreateElement("Error");
            XmlElement flow = doc.CreateElement("Flow");
            flow.InnerText = Logger.flow;
            XmlElement RMAno = doc.CreateElement("RMAId");
            RMAno.InnerText = Logger.RMAId;
            XmlElement date = doc.CreateElement("Date");
            date.InnerText = DateTime.Now.ToString();
            XmlElement methodname = doc.CreateElement("MethodName");
            methodname.InnerText = methodName;
            XmlElement history = doc.CreateElement("FieldChanges");
            history.InnerText = historyTrack;
            XmlElement Message = doc.CreateElement("Message");
            Message.InnerText = logMessage;
            Info.AppendChild(flow);
            Info.AppendChild(RMAno);
            Info.AppendChild(date);
            Info.AppendChild(methodname);
            Info.AppendChild(history);
            Info.AppendChild(Message);
            logroot.AppendChild(Info);
            doc.AppendChild(logroot);
            doc.Save(APP_PATH_FileName);
        }
    }
    #endregion
}