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

namespace ConfigMaker
{
    public class User
    {
        public int Uid;
        public String Username;
        public String Ip;
        public String Planname;
        public DateTime Dts;
        public DateTime Dte;

        public User()
        {
            Uid = 0;
            Username = "";
            Ip = "";
            Planname = "";
            Dts = new DateTime(2000, 01, 01, 00, 00, 00);
            Dte = new DateTime(2099, 12, 31, 23, 59, 59);
        }

        public static void ReadUserConfig(String fileusers, ref List<User> users)
        {
            IFormatProvider fp = new System.Globalization.CultureInfo("ru-RU");
            String s = "";

            int i = 0;

            using (StreamReader sr = new StreamReader(fileusers, Encoding.GetEncoding("windows-1251")))
            {
                while ((s = sr.ReadLine()) != null)
                {
                    i++;

                    s = s.TrimEnd();
                    if (s.StartsWith(";"))
                        continue;

                    String[] u = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (u.Length != 6)
                        continue;

                    User us = new User();

                    try
                    {
                        UInt32 ip = Ipaddr.FromString(u[2]);

                        us.Uid = Int32.Parse(u[0]);
                        us.Username = u[1];
                        us.Ip = u[2];
                        us.Planname = u[3];

                        DateTime dts = DateTime.ParseExact(u[4], "dd.MM.yyyy", fp);
                        DateTime dte = DateTime.ParseExact(u[5], "dd.MM.yyyy", fp);

                        us.Dts = new DateTime(dts.Year, dts.Month, dts.Day, 00, 00, 00);
                        us.Dte = new DateTime(dte.Year, dte.Month, dte.Day, 23, 59, 59);

                        if (us.Dts > new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 00, 00, 00))
                        {
                            continue;
                        }

                        if (us.Dte < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59))
                        {
                            continue;
                        };

                        bool found = false;
                        foreach (User uu in users)
                        {
                            if (uu.Uid == us.Uid)
                            {
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            users.Add(us);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }
    }
}
