﻿using System;
using System.Reflection;
using RulesEngine.AspNetCore;
using RulesEngine.Console.Models;

namespace RulesEngine.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            DependencyInjection.RegisterDependencies();

            Engine.Initialize(cfg =>
            {
                cfg.ConfigFactory = new ConfigFactory();
                cfg.AddConfigsFromAssembly(Assembly.GetEntryAssembly());
                //or: cfg.AddConfig(typeof(UserConfig));
                //or: cfg.AddConfig(DependencyInjection.Container.GetService<IConfig<User>>());
            });

            var validResult = Engine.Validate(CreateValidUser());
            WriteOutput(validResult);

            var invalidResult = Engine.Validate(CreateInvalidUser());
            WriteOutput(invalidResult);

            System.Console.ReadLine();
        }

        private static User CreateInvalidUser()
        {
            return new User
            {
                FirstName = "Jos",
                LastName = "De Weger",
                DateOfBirth = new DateTime(1983, 3, 19),
                Gender = Gender.Female,
                HasDriversLicense = false,
                Password = "MyP4ssw0rd",
                UserName = "SamuraiPizzaCat"
            };
        }

        private static User CreateValidUser()
        {
            return new User
            {
                FirstName = "Joe",
                LastName = "Smith",
                DateOfBirth = new DateTime(1980, 1, 11),
                Gender = Gender.Male,
                HasDriversLicense = true,
                Password = "54321",
                UserName = "User1"
            };
        }

        private static void WriteOutput(ValidationResult validationResult)
        {
            System.Console.WriteLine($"Rules validation result: {validationResult.IsValid}");

            foreach (var validationMessage in validationResult.Messages)
            {
                System.Console.WriteLine(
                    $"Rule: {validationMessage?.RuleName} | Code: {validationMessage?.Code} | Message: {validationMessage?.Message}");
            }
            System.Console.WriteLine("----------------------------------------------------");
        }
    }
}