﻿using System;
using System.Collections.Generic;
using System.Reflection;
using FunAggregator.DataTypes.Enums;

namespace FunAggregator.Providers
{
    public abstract class AuthProvider
    {
        private static AuthProvider _instance;

        /// <summary>
        /// This method determines whether the user is authorize to perform 
        /// the requested operation
        /// </summary>
        public abstract bool CheckAccess(string operation);

        /// <summary>
        /// This method determines whether the user is authorize to perform 
        /// the requested operation
        /// </summary>
        public abstract bool CheckAccess(object commandParameter);

        public static void Initialize<TProvider>() where TProvider : AuthProvider, new()
        {
            _instance = new TProvider();
        }

        public static void Initialize<TProvider>(List<RoleType> roleTypes)
        {
            ConstructorInfo constructor = typeof (TProvider).GetConstructor(new[] {typeof (List<RoleType>)});
            if (constructor == null)
            {
                throw new InvalidOperationException("AuthProvider.cs : ConstructorInfo not found");
            }
            _instance = (AuthProvider)constructor.Invoke(new object[] { roleTypes });
        }

        public static AuthProvider Instance
        {
            get { return _instance; }
        }
    }
}
