using SEI2.TestConsole.SEI2ReportProxy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SEI2.TestConsole
{
    public static class FactoryService
    {
        public static Action<ISEI2Report, object> CreateServiceActionOrException(string filePath)
        {
            Action<ISEI2Report, object> invokeService = null;
            string tmpName = Path.GetFileNameWithoutExtension(filePath).ToLower();

            invokeService = CreateFromFileName(tmpName);
            
            if (invokeService == null)
            {
                throw new ApplicationException($"Could not generate delegate to invoke service from file: {filePath}.");
            }

            return invokeService;
        }

        private static Action<ISEI2Report, object> CreateFromFileName(string name)
        {
            if (name.Contains("usermgmt"))
            {
                Console.WriteLine("OPERATION: UserMgmt");
                return UserMgmt.CreateServiceCall();
            }
            else if (name.Contains("report"))
            {
                Console.WriteLine("OPERATION: SchemaReport");
                return (ISEI2Report client, object _contract) =>
                {
                    var s = client.SchemaReport(_contract as SEI2SchemaBaseMembersContract);
                    Console.WriteLine("Error code: " + s.errorCode);
                    Console.WriteLine("Error message: " + s.errorMessage);
                    Console.WriteLine("Success: " + s.success);
                    Console.WriteLine("Schema guid: " + s.schemaGuid);
                    Console.WriteLine(string.Empty);
                };
            }
            else if (name.Contains("cancel"))
            {
                Console.WriteLine("OPERATION: SchemaCancel");
                return (ISEI2Report client, object _contract) =>
                {
                    var s = client.SchemaCancel(_contract as SEI2SchemaCancelContract);
                    Console.WriteLine("Error code: " + s.errorCode);
                    Console.WriteLine("Error message: " + s.errorMessage);
                    Console.WriteLine("Success: " + s.success);
                    Console.WriteLine(string.Empty);
                };
            }
            else if (name.Contains("usergetdetails"))
            {
                Console.WriteLine("OPERATION: UserGetDetails");
                return (ISEI2Report client, object _contract) =>
                {
                    var s = client.UserGetDetails((SEI2UserDetailsRequestContract)_contract);
                    Console.WriteLine("Error code: " + s.errorCode);
                    Console.WriteLine("Error message: " + s.errorMessage);
                    Console.WriteLine("Success: " + s.success);
                    if (s.success)
                    {
                        Console.WriteLine("Username: " + s.userName);
                        Console.WriteLine("SEI2 user id: " + s.sei2UserId);
                        Console.WriteLine("Certificate id: " + s.certificateId);

                        if (s.userGroups != null)
                        {
                            foreach (var group in s.userGroups)
                            {
                                Console.WriteLine("\tGroup: " + group.userGroupName);
                                Console.WriteLine("\t\tAdmin: " + group.administrator);
                                Console.WriteLine("\t\tUser: " + group.user);
                            }
                        }
                        if (s.userSecurityRoles != null)
                        {
                            foreach (var role in s.userSecurityRoles)
                                Console.WriteLine("\tRole: " + role.name);
                        }
                    }
                    Console.WriteLine(string.Empty);
                };
            }
            else if (name.Contains("usercreate"))
            {
                Console.WriteLine("OPERATION: UserCreate");
                return (ISEI2Report client, object _contract) =>
                {
                    var s = client.UserCreate(_contract as SEI2UserCreateRequestContract);
                    Console.WriteLine("Error code: " + s.errorCode);
                    Console.WriteLine("Error message: " + s.errorMessage);
                    Console.WriteLine("Success: " + s.success);
                    Console.WriteLine("SEI2 id: " + s.seiGuid);
                    Console.WriteLine("User id: " + s.userId);
                    Console.WriteLine(string.Empty);
                };
            }
            else if (name.Contains("fhir"))
            {
                Console.WriteLine("OPERATION: SchemaReportFhirBundle");
                return (ISEI2Report client, object _contract) =>
                {
                    var s = client.SchemaReportFhirBundle(_contract as SEI2BundleRequestContract);
                    Console.WriteLine("Return bundle count: " + s.BundleResponses.Length);
                    foreach (SEI2SingleBundleResponseContract bundle in s.BundleResponses)
                    {
                        Console.WriteLine("\t*** Bundle ***");
                        Console.WriteLine("\tBundle success: " + bundle.success);
                        Console.WriteLine("\tBundle schema guid: " + bundle.schemaGuid);
                        Console.WriteLine("\tBundle id: " + bundle.BundleId);
                        Console.WriteLine("\tBundle error code: " + bundle.errorCode);
                        Console.WriteLine("\tBundle error message: " + bundle.errorMessage);
                    }
                    Console.WriteLine(string.Empty);
                };
            }
            else
            {
                return null;
            }

        }
    }
}
