using System.Collections.Generic;
using Sockeye.Biz;
using Sockeye.Models;

namespace Sockeye.DataList
{
    internal class InsideUserDataList : DataListProcessingBase, IDataListInternalCriteria
    {

        public InsideUserDataList(long translationId)
        {
            DefaultListAType = SockType.User;
            SQLFrom = "from auser "
            + "left join auseroptions on auser.id=auseroptions.userid "
            + "left join atranslation on auseroptions.translationid = atranslation.id";
            var RoleSet = BizRoles.GetRoleSet(DefaultListAType);
            AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change;
            DefaultColumns = new List<string>() { "name", "employeenumber", "active", "usertype", "lastlogin" };
            DefaultSortBy = new Dictionary<string, string>() { { "name", "+" } };

            FieldDefinitions = new List<DataListFieldDefinition>();
            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "User",
                FieldKey = "name",
                SockType = (int)SockType.User,
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "auser.id",
                SqlValueColumnName = "auser.name",
                IsRowId = true
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "UserEmployeeNumber",
                FieldKey = "employeenumber",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "auser.employeenumber"
            });


            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Active",
                FieldKey = "active",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "auser.active"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AllowLogin",
                FieldKey = "allowlogin",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "auser.allowlogin"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "UserType",
                FieldKey = "usertype",
                UiFieldDataType = (int)UiFieldDataType.Enum,
                EnumType = Sockeye.Util.StringUtil.TrimTypeName(typeof(UserType).ToString()),
                SqlValueColumnName = "auser.usertype"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AuthorizationRoles",
                FieldKey = "roles",
                UiFieldDataType = (int)UiFieldDataType.Roles,
                //NOTE: not technically an enum list but this will trigger datagrid at client to fetch roles for special handling
                EnumType = Sockeye.Util.StringUtil.TrimTypeName(typeof(AuthorizationRoles).ToString()),
                SqlValueColumnName = "auser.roles"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "LastLogin",
                FieldKey = "lastlogin",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "auser.lastlogin"
            });

          

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AuthTwoFactor",
                FieldKey = "AuthTwoFactor",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "auser.twofactorenabled"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Translation",
                FieldKey = "Translation",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SockType = (int)SockType.Translation,
                SqlIdColumnName = "atranslation.id",
                SqlValueColumnName = "atranslation.name"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Tags",
                FieldKey = "Tags",
                UiFieldDataType = (int)UiFieldDataType.Tags,
                SqlValueColumnName = "auser.tags"
            });

            //META COLUMNS
            FieldDefinitions.Add(new DataListFieldDefinition
            {

                FieldKey = "metausertype",
                UiFieldDataType = (int)UiFieldDataType.Enum,
                EnumType = Sockeye.Util.StringUtil.TrimTypeName(typeof(UserType).ToString()),
                SqlValueColumnName = "auser.usertype",
                IsMeta = true
            });
        }

        public List<DataListFilterOption> DataListInternalCriteria(long currentUserId, AuthorizationRoles userRoles, string clientCriteria)
        {
            List<DataListFilterOption> ret = new List<DataListFilterOption>();

            DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metausertype" };
            FilterOption.Items.Add(new DataListColumnFilter() { value = ((int)UserType.Customer).ToString(), op = DataListFilterComparisonOperator.NotEqual });
            FilterOption.Items.Add(new DataListColumnFilter() { value = ((int)UserType.HeadOffice).ToString(), op = DataListFilterComparisonOperator.NotEqual });

            ret.Add(FilterOption);
            return ret;
        }

    }//eoc
}//eons