using LC_AddCheckToDb.Models;
using Microsoft.VisualBasic.FileIO;

namespace LC_AddCheckToDb.Models
{

    internal class Program
    {
#if DEBUG
    private static void Main()
    {
        string[] args = ["172.24.12.211", "неактивен"];
#else
        private static void Main(string[] args)
        {
            if (args.Length == 0)
                return;
#endif

            List<Error> _error = new List<Error>();
            using (var db = new Db())
            {
                var status = statusDev.Unknown;
                var ip = args[0].Trim();
                if (args[1].Trim().Contains("не"))
                {
                    status = 0;
                    using (TextFieldParser parser = new TextFieldParser("spb_devices.csv"))
                    {
                        parser.TextFieldType = FieldType.Delimited;
                        parser.SetDelimiters(";");
                        while (!parser.EndOfData)
                        {
                            string[] fields = parser.ReadFields();
                            typeDev _type;
                            switch (fields[0])
                            {
                                case "Camera":
                                    _type = typeDev.Camera;
                                    break;
                                case "SABRA":
                                    _type = typeDev.SABRA;
                                    break;
                                case "vServer":
                                    _type = typeDev.vServer;
                                    break;
                                case "Server":
                                    _type = typeDev.Server;
                                    break;
                                case "Switch":
                                    _type = typeDev.Switch;
                                    break;
                                case "DVR":
                                    _type = typeDev.DVR;
                                    break;
                                case "ARM":
                                    _type = typeDev.ARM;
                                    break;
                                default:
                                    _type = typeDev.Unknown;
                                    break;
                            }
                            var I = new Error()
                            {
                                Id = 0,
                                Status = statusDev.On,
                                Name = fields[3],
                                Ip = fields[1],
                                MsgId = 0,
                                TypeDev = _type,
                                DataError = DateTime.Now.ToUniversalTime()
                            };
                            _error.Add(I);
                        }
                    }
                    var dev = _error.Where(d => d.Ip == args[0]).First();
                    dev.Status = status;
                    db.Error.Add(dev);
                    db.SaveChanges();
                }
                else
                {
                    status = statusDev.On;
                    var dev = db.Error.Where(d => d.Ip == args[0] && d.Status == statusDev.Off).FirstOrDefault();
                    if (dev != null)
                    {
                        dev.Status = status;
                        db.Error.Update(dev);
                        db.SaveChanges();
                    }
                }
            }
            Console.WriteLine("0");
        }
    }
}