using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class VMsg
    {
        public static List<Msg> msg = new List<Msg>();
        public string msgtxt;
        public MsgLevel msgLevel;
        public ErrInfo errInfo;
        public DateTime datetime;

        public static List<Msg> get()
        {
            if (_setlogones)
            {
                _setlogones = false;
                foreach (var m in msg)
                    LogUtil.info(m.msgtxt);
            }
            List<Msg> mm = new List<Msg>(msg);
            return mm;
        }
        public static void add(string m, MsgLevel ml, ErrInfo errInfo = ErrInfo.Empty)
        {
            lock (msg)
            {
                var fm = msg.Find((x) => x.msgtxt == m);
                if (fm == null)
                    msg.Add(new Msg { msgtxt = m, msgLevel = ml, datetime = DateTime.Now, errInfo = errInfo });
            }
        }
        static bool _setlogones = false;
        internal static void setlogones()
        {
            _setlogones = true;
        }

        public static void clear()
        {
            msg.Clear();
        }
    }
}