using ConfigOperator;
using GeneralData;
using Org.BouncyCastle.Tsp;
using ORM;
using SqlSugarORM;
using Universal;

namespace UserTool
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (!BaseConfigFileLoader.Load(out BasicInfo? basicInfo) || basicInfo is null || string.IsNullOrEmpty(basicInfo.DBConnectionString))
            {
                Console.WriteLine("Connect to database failed");
                return;
            }

            IORM orm = new SqlSugarCustom();
            orm.Initialize();
            if (!orm.Open(basicInfo.DBConnectionString, DbType.PostgreSQL))
            {
                Console.WriteLine("Connect to database failed");
                return;
            }
            Dictionary<int, string> users = [];
            bool success;
            do
            {
                Console.WriteLine("Try Reading");
                success = orm.Query<UserInfo>("UserAuthority", userInfos =>
               {
                   Console.WriteLine("UserList:");

                   for (int i = 1; i <= userInfos.Count; i++)
                   {
                       string? name = userInfos[i - 1].UserName;
                       if (string.IsNullOrEmpty(name))
                           continue;
                       users[i] = name;
                       Console.WriteLine($"{i} : {name}");
                   }

               }).Result;

                if (!success || users.Count == 0)
                {
                    orm.CreateTable<UserInfo>("UserAuthority");
                    UserInfo userInfo = new()
                    {
                        UserName = "Engineer",
                        Password = "Aa123456".ToBase64(),
                        Authority = UserAuthority.Engineer
                    };
                    orm.Insert<UserInfo>("UserAuthority", userInfo);
                }
            } while (users.Count == 0);

        Operation:
            Console.WriteLine("Operation List:");
            Console.WriteLine("1 - Create User");
            Console.WriteLine("2 - Change Password");
            switch (Console.ReadLine())
            {
                case "1":
                    string? newUsername = Console.ReadLine();
                    break;
                case "2":
                    break;

                default:
                    goto Operation;
            }

            Thread.Sleep(-1);
        }
    }
}
