using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CRVM.Utility
{
    /// <summary>
    /// 画面日志实体类
    /// </summary>
    public sealed class EntityLog
    {
        private string projname = "";
        /// <summary>
        /// Gets or sets the name of the proj.
        /// </summary>
        /// <value>The name of the proj.</value>
        public string ProjName
        {
            get { return projname; }
            set { projname = value; }
        }

        private string information;
        /// <summary>
        /// Gets or sets the information.
        /// </summary>
        /// <value>The information.</value>
        public string Information
        {
            get { return information; }
            set { information = value; }
        }

        private string keyInfo="";
        public string KeyInfo
        {
            get { return keyInfo; }
            set { keyInfo = value; }
        }

        private string otherInfo;
        /// <summary>
        /// Gets or sets the other info.
        /// </summary>
        /// <value>The other info.</value>
        public string OtherInfo
        {
            get { return otherInfo; }
            set { otherInfo = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EntityLog"/> class.
        /// </summary>
        /// <param name="information">The information.</param>
        /// <param name="projname">The projname.</param>
        public EntityLog(string information, string projname)
        {
            this.information = information;
            this.projname = projname;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EntityLog"/> class.
        /// </summary>
        /// <param name="information">The information.</param>
        /// <param name="projname">The projname.</param>
        /// <param name="otherinfo">The otherinfo.</param>
        public EntityLog(string information, string projname, string otherinfo)
        {
            this.information = information;
            this.projname = projname;
            this.otherInfo = otherinfo;
        }
        /// <summary>
        /// initializes a new instance of < EntityLog > class
        /// </summary>
        /// <param name="information"> the information</param>
        /// <param name="projname"> the projname</param>
        /// <param name="keyInfo"> the key word, such as 'Error, Info, Start ,End'</param>
        /// <param name="otherinfo">the detail infomation </param>
        public EntityLog(string information, string projname, string keyInfo, string otherinfo)
        {
            this.information = information;
            this.projname = projname;
            this.keyInfo = keyInfo;
            this.otherInfo = otherinfo;
        }
    }

    public sealed class MLog
    {
        private const int projnameLen = 10;//项目名称20字符长
        private const int infoLen = 38;//项目名称30字符长
        private const int keywordLen = 12;//操作结果提示10字符长
        private string logfileName;
        public MLog(string logName)
        {
            logfileName = logName;
        }

        public int WriteLogFile(string projname, string information)
        {
            EntityLog ea = new EntityLog(information, projname);
            WriteLogFile(ea);
            return 0;
        }

        public int WriteLogFile(string projname, string information, string otherInfo)
        {
            EntityLog ea = new EntityLog(information, projname, otherInfo);
            WriteLogFile(ea);
            return 1;
        }

        public int WriteLogFile(string projname, string information,string keyInfo, string otherInfo)
        {
            EntityLog ea = new EntityLog(information, projname, keyInfo, otherInfo);
            WriteLogFile(ea);
            return 1;
        }

        private int WriteLogFile(EntityLog ealarm)
        {
        //    lock (this)
        //    {
        //        try
        //        {
        //            StreamWriter write;
        //            DateTime time = System.DateTime.Now;
        //            string path = Application.StartupPath + "\\log\\";
        //            int actprojLen = ealarm.ProjName.Length;
        //            String srtProj;
        //            if (actprojLen < projnameLen)
        //                srtProj = new String(' ', projnameLen - actprojLen);
        //            else
        //                srtProj = "";

        //            int actinfoLen = ealarm.Information.Length;
        //            String srtInfo;
        //            if (actinfoLen < infoLen)
        //                srtInfo = new String(' ', infoLen - actinfoLen);
        //            else
        //                srtInfo = "";

        //            int actkeywordLen = ealarm.KeyInfo.Length;
        //            String strKeyWord;
        //            if (actkeywordLen < keywordLen)
        //                strKeyWord = new string(' ', keywordLen - actkeywordLen);
        //            else
        //                strKeyWord = "";


        //            if (File.Exists(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"))
        //            {
        //                write = File.AppendText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log");
        //                string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo;
        //                write.WriteLine(s);
        //                write.Flush();
        //                write.Close();
        //            }
        //            else
        //            {
        //                if (!Directory.Exists(path))
        //                    Directory.CreateDirectory(path);
        //                write = File.CreateText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log");
        //                string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo;
        //                write.WriteLine(s);
        //                write.Flush();
        //                write.Close();
        //            }
        //            return 1;
        //        }
        //        catch (FieldAccessException fex)
        //        {
        //            throw new Exception(fex.Message);
        //        }
        //    }
            return 1;
        }
    }


    /// <summary>
    /// Project develop use this class for record the operatings database
    /// </summary>
    public sealed class DevelopLog
    {
        private MLog mLog;
        private static DevelopLog opLog = null;
        private const string logName = "devlog";
        private const int keywordLen = 12;//操作结果提示10字符长
        private const int projnameLen = 10;//项目名称20字符长
        private const int infoLen = 38;//项目名称30字符长

        private DevelopLog(string logName)
        {
            mLog = new MLog(logName);
        }

        public static DevelopLog DeBug
        {
            get
            {
                if (opLog == null)
                    opLog = new DevelopLog(logName);
                return opLog;
            }
        }

        public int WriteLogFile(string projname, string information)
        {
            EntityLog ea = new EntityLog(information, projname);
            WriteLogFile(ea);
            return 0;
        }

        public int WriteLogFile(string projname, string information, string otherInfo)
        {
            EntityLog ea = new EntityLog(information, projname, otherInfo);
            WriteLogFile(ea);
            return 1;
        }

        public int WriteLogFile(string projname, string information, string keyInfo, string otherInfo)
        {
            EntityLog ea = new EntityLog(information, projname, keyInfo, otherInfo);
            WriteLogFile(ea);
            return 1;
        }

        /// <summary>
        /// 根据类型进行选择写日志信息
        /// </summary>
        /// <param name="projname"></param>
        /// <param name="information"></param>
        /// <param name="keyInfo"></param>
        /// <param name="otherInfo"></param>
        /// <param name="recordType"></param>
        /// <returns></returns>
        public int WriteLog(string projname, string information, string keyInfo, string otherInfo,string recordType)
        {
            if (recordType == "All" || recordType == keyInfo.ToUpper())
            {
                return mLog.WriteLogFile(projname, information, keyInfo, otherInfo);
            }
            else
                return 1;
        }

        private int WriteLogFile(EntityLog ealarm)
        {
            lock (this)
            {
                try
                {
                    StreamWriter write;
                    DateTime time = System.DateTime.Now;
                    string path = Application.StartupPath + "\\log\\";
                    int actprojLen = ealarm.Information.Length;
                    String srtProj;
                    if (actprojLen < projnameLen)
                        srtProj = new String(' ', projnameLen - actprojLen);
                    else
                        srtProj = "";

                    int actinfoLen = ealarm.Information.Length;
                    String srtInfo;
                    if (actinfoLen < infoLen)
                        srtInfo = new String(' ', infoLen - actinfoLen);
                    else
                        srtInfo = "";

                    int actkeywordLen = ealarm.KeyInfo.Length;
                    String strKeyWord;
                    if (actkeywordLen < keywordLen)
                        strKeyWord = new string(' ', keywordLen - actkeywordLen);
                    else
                        strKeyWord = "";


                    if (File.Exists(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log"))
                    {
                        write = File.AppendText(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log");
                        string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo;
                        write.WriteLine(s);
                        write.Flush();
                        write.Close();
                    }
                    else
                    {
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                        write = File.CreateText(@path + logName + "." + time.ToString("yyyy-MM-dd") + ".log");
                        string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.KeyInfo + strKeyWord + " | " + ealarm.OtherInfo;
                        write.WriteLine(s);
                        write.Flush();
                        write.Close();
                    }
                    return 1;
                }
                catch (FieldAccessException fex)
                {
                    throw new Exception(fex.Message);
                }
            } 
        }
    }
    #region 原来的代码
    ///// <summary>
    ///// 画面日志实体类
    ///// </summary>
    //public sealed class EntityLog
    //{
    //    private string projname = "";
    //    /// <summary>
    //    /// Gets or sets the name of the proj.
    //    /// </summary>
    //    /// <value>The name of the proj.</value>
    //    public string ProjName
    //    {
    //        get { return projname; }
    //        set { projname = value; }
    //    }

    //    private string information;
    //    /// <summary>
    //    /// Gets or sets the information.
    //    /// </summary>
    //    /// <value>The information.</value>
    //    public string Information
    //    {
    //        get { return information; }
    //        set { information = value; }
    //    }
         
    //    private string otherInfo;
    //    /// <summary>
    //    /// Gets or sets the other info.
    //    /// </summary>
    //    /// <value>The other info.</value>
    //    public string OtherInfo
    //    {
    //        get { return otherInfo; }
    //        set { otherInfo = value; }
    //    }

    //    /// <summary>
    //    /// Initializes a new instance of the <see cref="EntityLog"/> class.
    //    /// </summary>
    //    /// <param name="information">The information.</param>
    //    /// <param name="projname">The projname.</param>
    //    public EntityLog(string information,string projname)
    //    {
    //        this.information = information;
    //        this.projname = projname;
    //    }

    //    /// <summary>
    //    /// Initializes a new instance of the <see cref="EntityLog"/> class.
    //    /// </summary>
    //    /// <param name="information">The information.</param>
    //    /// <param name="projname">The projname.</param>
    //    /// <param name="otherinfo">The otherinfo.</param>
    //    public EntityLog(string information, string projname, string otherinfo)
    //    {
    //        this.information = information;
    //        this.projname = projname;
    //        this.otherInfo = otherinfo;
    //    }
    //}

    //public sealed class MLog
    //{
    //    private const int projnameLen = 15;//项目名称20字符长
    //    private const int infoLen = 50;//项目名称30字符长
    //    private string logfileName;
    //    public MLog(string logName)
    //    {
    //        logfileName = logName;
    //    }

    //    public int WriteLogFile(string projname,string information)
    //    {
    //        EntityLog ea = new EntityLog(information, projname);
    //        WriteLogFile(ea);
    //        return 0;
    //    }

    //    public int WriteLogFile(string projname, string information, string otherInfo)
    //    {
    //        EntityLog ea = new EntityLog(information, projname, otherInfo);
    //        WriteLogFile(ea);
    //        return 1;
    //    }

    //    private int WriteLogFile(EntityLog ealarm)
    //    {
    //        try
    //        {
    //            StreamWriter write;
    //            DateTime time = System.DateTime.Now;
    //            string path = Application.StartupPath + "\\log\\";
    //            int actprojLen = ealarm.ProjName.Length;
    //            String srtProj;
    //            if (actprojLen < projnameLen)
    //                srtProj = new String(' ', projnameLen - actprojLen);
    //            else
    //                srtProj = "";

    //            int actinfoLen = ealarm.Information.Length;
    //            String srtInfo;
    //            if (actinfoLen < infoLen)
    //                srtInfo = new String(' ', infoLen - actinfoLen);
    //            else
    //                srtInfo = "";


    //            if (File.Exists(@path+ logfileName + "." + time.ToString("yyyy-MM-dd") + ".log"))
    //            {
    //                write = File.AppendText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log");
    //                string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.OtherInfo;
    //                write.WriteLine(s);
    //                write.Flush();
    //                write.Close();
    //            }
    //            else
    //            {
    //                if (!Directory.Exists(path))
    //                    Directory.CreateDirectory(path);
    //                write = File.CreateText(@path + logfileName + "." + time.ToString("yyyy-MM-dd") + ".log");
    //                string s = time.ToString("yyyy-MM-dd HH:mm:ss") + ":" + time.Millisecond.ToString("#000") + " | " + ealarm.ProjName + srtProj + " | " + ealarm.Information + srtInfo + " | " + ealarm.OtherInfo;
    //                write.WriteLine(s);
    //                write.Flush();
    //                write.Close();
    //            }
    //            return 1;
    //        }
    //        catch (FieldAccessException fex)
    //        {
    //            throw new Exception(fex.Message);
    //        }
    //    }
    //}

   
    ///// <summary>
    ///// Project develop use this class for record the operatings database
    ///// </summary>
    //public sealed class DevelopLog
    //{
    //    private MLog mLog;
    //    private static DevelopLog opLog = null;
    //    private const string logName = "devlog";

    //    private DevelopLog(string logName)
    //    {
    //        mLog = new MLog(logName);
    //    }

    //    public static DevelopLog DeBug
    //    {
    //        get
    //        {
    //            if (opLog == null)
    //                opLog = new DevelopLog(logName);
    //            return opLog;
    //        }
    //    }

    //    public int WriteLog(string projname, string information)
    //    {
    //        return mLog.WriteLogFile(projname, information);
    //    }

    //    public int WriteLog(string projname, string information, string otherInfo)
    //    {
    //        return mLog.WriteLogFile(projname, information, otherInfo);
    //    }
    //}
    #endregion
}