using System.Threading.Tasks;
using Sockeye.Biz;
using System.Collections.Generic;
using Sockeye.DataList;
using Newtonsoft.Json;
using EnumsNET;
namespace Sockeye.Models
{
    //Used to drive processes that rely on selections made at client from a datalist
    //either a preselected list of id's or a datalist key and filterid that can 
    //be used to rehydrate a list of id's
    public class DataListSelectedProcessingOptions : DataListProcessingBase
    {
        internal DataListSelectedProcessingOptions(
                 DataListSelectedRequest request,
                 IDataListProcessing dataList,
                 DataListColumnView savedView,
                 DataListSavedFilter savedFilter,
                 long userId,
                 AuthorizationRoles userRoles)
        {
            //set some values from request            
            base.ClientCriteria = request.ClientCriteria;
            base.DataListKey = request.DataListKey;
            base.ClientTimeStamp = request.ClientTimeStamp;

            //SET SORTBY
            base.SortBy = JsonConvert.DeserializeObject<Dictionary<string, string>>(savedView.Sort);

            //SET FILTER
            if (request.FilterId != 0 && savedFilter != null)
                base.Filter = JsonConvert.DeserializeObject<List<DataListFilterOption>>(savedFilter.Filter);

            //ADD STATIC SERVER FILTERS
            List<DataListFilterOption> StaticServerFilterOptions = new List<DataListFilterOption>();
            if (dataList is IDataListInternalCriteria)
                StaticServerFilterOptions = ((IDataListInternalCriteria)dataList).DataListInternalCriteria(userId, userRoles, request.ClientCriteria);

            //Add the internal filters into the listoptions existing filters
            //NOTE: There is currently no overlap between internal filtered columns and filters coming from the client
            foreach (DataListFilterOption dfo in StaticServerFilterOptions)
                base.Filter.Add(dfo);

        }


        public static async Task<long[]> RehydrateIdList(DataListSelectedRequest selectedRequest, AyContext ct, AuthorizationRoles userRoles, Microsoft.Extensions.Logging.ILogger log, long userId, long userTranslationId)
        {
            DataListColumnViewBiz viewbiz = new DataListColumnViewBiz(ct, userId, userTranslationId, userRoles);
            var SavedView = await viewbiz.GetAsync(userId, selectedRequest.DataListKey, true);

            DataListSavedFilter SavedFilter = null;
            if (selectedRequest.FilterId != 0)
            {
                DataListSavedFilterBiz filterbiz = new DataListSavedFilterBiz(ct, userId, userTranslationId, userRoles);
                SavedFilter = await filterbiz.GetAsync(selectedRequest.FilterId);
            }
            var DataList = DataListFactory.GetAyaDataList(selectedRequest.DataListKey, userTranslationId);
            if (DataList == null)
                throw new System.ArgumentOutOfRangeException($"DataList \"{selectedRequest.DataListKey}\" specified does not exist");
           
            //check rights
            if (!userRoles.HasAnyFlags(DataList.AllowedRoles))
                throw new System.UnauthorizedAccessException($"DataList \"{selectedRequest.DataListKey}\" required Roles not found for this user");

            DataListSelectedProcessingOptions d = new DataListSelectedProcessingOptions(selectedRequest, DataList, SavedView, SavedFilter, userId, userRoles);

            return await Sockeye.DataList.DataListFetcher.GetIdListResponseAsync(ct, d, DataList, userRoles, log, userId, selectedRequest.ReportDesignerSample);
        }


    }
}

