using System.Collections.Generic;
using Sockeye.Models;
using Sockeye.Biz;
using System.Linq;
namespace Sockeye.DataList
{
    internal class ReviewDataList : DataListProcessingBase, IDataListInternalCriteria
    {
        public ReviewDataList(long translationId)
        {

            DefaultListAType = SockType.Review;
            SQLFrom = "from areview "
                      + "left join auser uassto on (areview.userid=uassto.id) "
                      + "left join auser uassby on (areview.assignedbyuserid=uassby.id)";
            var RoleSet = BizRoles.GetRoleSet(DefaultListAType);
            AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change;
            DefaultColumns = new List<string>() { "ReviewCompletedDate", "ReviewDate", "ReviewName", "Object", "SockType", "ReviewNotes", "ReviewUserId" };
            DefaultSortBy = new Dictionary<string, string>() { { "ReviewCompletedDate", "-" }, { "ReviewDate", "+" } };
            FieldDefinitions = new List<DataListFieldDefinition>();

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Object",
                FieldKey = "Object",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "areview.objectid",
                SqlValueColumnName = $"AYGETNAME(areview.objectid, areview.aType,{translationId})",
                SqlATypeColumnName = "areview.aType",
                Translate=true
            });

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

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ReviewName",
                FieldKey = "ReviewName",
                SockType = (int)SockType.Review,
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "areview.id",
                SqlValueColumnName = "areview.name",
                IsRowId = true
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ReviewNotes",
                FieldKey = "ReviewNotes",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "areview.notes"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ReviewDate",
                FieldKey = "ReviewDate",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "areview.reviewdate"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ReviewCompletedDate",
                FieldKey = "ReviewCompletedDate",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "areview.completeddate"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ReviewCompletionNotes",
                FieldKey = "ReviewCompletionNotes",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "areview.completionnotes"
            });

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

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

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



            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom1", FieldKey = "ReviewCustom1", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom2", FieldKey = "ReviewCustom2", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom3", FieldKey = "ReviewCustom3", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom4", FieldKey = "ReviewCustom4", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom5", FieldKey = "ReviewCustom5", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom6", FieldKey = "ReviewCustom6", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom7", FieldKey = "ReviewCustom7", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom8", FieldKey = "ReviewCustom8", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom9", FieldKey = "ReviewCustom9", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom10", FieldKey = "ReviewCustom10", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom11", FieldKey = "ReviewCustom11", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom12", FieldKey = "ReviewCustom12", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom13", FieldKey = "ReviewCustom13", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom14", FieldKey = "ReviewCustom14", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom15", FieldKey = "ReviewCustom15", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "ReviewCustom16", FieldKey = "ReviewCustom16", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "areview.customfields" });



            //META COLUMNS
            FieldDefinitions.Add(new DataListFieldDefinition
            {
                FieldKey = "metareviewuser",
                UiFieldDataType = (int)UiFieldDataType.InternalId,
                SqlIdColumnName = "areview.userid",
                SqlValueColumnName = "areview.userid",
                IsMeta = true
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                FieldKey = "metaobjectid",
                UiFieldDataType = (int)UiFieldDataType.InternalId,
                SqlIdColumnName = "areview.objectid",
                SqlValueColumnName = "areview.objectid",
                IsMeta = true
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {

                FieldKey = "metaobjecttype",
                UiFieldDataType = (int)UiFieldDataType.Enum,
                EnumType = Sockeye.Util.StringUtil.TrimTypeName(typeof(SockType).ToString()),
                SqlValueColumnName = "areview.aType",
                IsMeta = true
            });
        }


        public List<DataListFilterOption> DataListInternalCriteria(long currentUserId, AuthorizationRoles userRoles, string clientCriteria)
        {
            List<DataListFilterOption> ret = new List<DataListFilterOption>();
            bool HasSupervisorRole =
                    userRoles.HasFlag(AuthorizationRoles.BizAdmin)
                    || userRoles.HasFlag(AuthorizationRoles.Service)
                    || userRoles.HasFlag(AuthorizationRoles.Inventory)
                    || userRoles.HasFlag(AuthorizationRoles.Sales)
                    || userRoles.HasFlag(AuthorizationRoles.Accounting);

            if (!HasSupervisorRole)
            {
                DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metareviewuser" };
                FilterOption.Items.Add(new DataListColumnFilter() { value = currentUserId.ToString(), op = DataListFilterComparisonOperator.Equality });

                ret.Add(FilterOption);
            }


            //ClientCriteria format for this list is "OBJECTID,AYATYPE" 
            var crit = (clientCriteria ?? "").Split(',').Select(z => z.Trim()).ToArray();
            if (crit.Length > 1)
            {
                //OBJECTID criteria
                if (crit[0] != "0")
                {
                    DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metaobjectid" };
                    FilterOption.Items.Add(new DataListColumnFilter() { value = crit[0], op = DataListFilterComparisonOperator.Equality });
                    ret.Add(FilterOption);
                }

                //AYATYPE criteria
                if (!string.IsNullOrWhiteSpace(crit[1]))
                {
                    DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metaobjecttype" };
                    FilterOption.Items.Add(new DataListColumnFilter() { value = crit[1], op = DataListFilterComparisonOperator.Equality });
                    ret.Add(FilterOption);
                }
            }
            return ret;
        }

    }//eoc
}//eons