using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GcDevicePc.Common
{
    class LogHelper
    {
        private enum ErrorType
        {
            W = 0,      //警告信息
            E = 1,      //错误信息
            I = 2,      //普通信息
        }

        private static string filePath = System.Windows.Forms.Application.StartupPath;
        private static string LogOutputFile = filePath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; 
        private static ErrorType errType = LogHelper.ErrorType.I;

        private static bool WriteLogToFile(string module, string strlog)
        {
            try
            {
                if (File.Exists(LogHelper.LogOutputFile) == false)
                {
                    FileStream fsm = File.Create(LogHelper.LogOutputFile);
                    if (fsm != null)
                        fsm.Close();
                }

                DateTime dt = File.GetCreationTime(LogHelper.LogOutputFile);
                dt = dt.AddDays(3);

                //删除三天前log信息
                if (dt.CompareTo(DateTime.Now) < 0)
                {
                    File.Delete(LogHelper.LogOutputFile);
                    //File.Create(Log.LogOutputFile);
                }
                //时间
                string inputstr = DateTime.Now.ToString();
                inputstr += "  ";

                //错误类型
                inputstr += errType.ToString();
                inputstr += "  ";

                //模块名,只保留20个字符，文本统一
                string modulename = "";
                if (module.Length >= 20)
                    modulename = module.Substring(0, 20);
                else
                {
                    modulename = module;
                    do
                    {
                        modulename = modulename.Insert(modulename.Length, "-");
                    }
                    while (modulename.Length < 20);
                }
                //modulename = module;
                inputstr += modulename;
                inputstr += "  ";

                //log信息
                inputstr += strlog;

                //FileStream ot = File.OpenWrite(LogOutputFile);

                //ot.w
                StreamWriter output = File.AppendText(LogHelper.LogOutputFile);
                output.WriteLine(inputstr);
                //output.
                output.Close();

            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool I(string module, string strlog)
        {
            LogHelper.errType = ErrorType.I;
            return WriteLogToFile(module, strlog);
        }

        public static bool W(string module, string strlog)
        {
            LogHelper.errType = ErrorType.W;
            return WriteLogToFile(module, strlog);
        }

        public static bool E(string module, string strlog)
        {
            LogHelper.errType = ErrorType.E;
            return WriteLogToFile(module, strlog);
        }

        public static void Clear()
        {
            if (File.Exists(LogHelper.LogOutputFile) == true)
                File.Delete(LogHelper.LogOutputFile);
        }
    }
}
