using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sockeye.Util;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Models;
using System.Linq;

namespace Sockeye.Biz
{
    internal class NotifySubscriptionBiz : BizObject//, IJobObject, ISearchAbleObject
    {
        internal NotifySubscriptionBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
        {
            ct = dbcontext;
            UserId = currentUserId;
            UserTranslationId = userTranslationId;
            CurrentUserRoles = UserRoles;
            BizType = SockType.NotifySubscription;
        }

        internal static NotifySubscriptionBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
        {
            if (httpContext != null)
                return new NotifySubscriptionBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
            else
                return new NotifySubscriptionBiz(ct, 1, ServerBootConfig.SOCKEYE_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdmin);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        //EXISTS 
        internal async Task<bool> ExistsAsync(long id)
        {
            return await ct.NotifySubscription.AnyAsync(z => z.Id == id);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        //CREATE   
        //
        internal async Task<NotifySubscription> CreateAsync(NotifySubscription newObject)
        {
            await ValidateAsync(newObject);
            if (HasErrors)
                return null;
            else
            {
                newObject.Tags = TagBiz.NormalizeTags(newObject.Tags);

                await ct.NotifySubscription.AddAsync(newObject);
                await ct.SaveChangesAsync();
                await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, SockEvent.Created), ct);
                await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
                return newObject;
            }
        }

        // ////////////////////////////////////////////////////////////////////////////////////////////////
        // //DUPLICATE
        // //
        // internal async Task<NotifySubscription> DuplicateAsync(long id)
        // {

        //     var dbObject = await GetAsync(id, false);
        //     if (dbObject == null)
        //     {
        //         AddError(ApiErrorCode.NOT_FOUND, "id");
        //         return null;
        //     }
        //     NotifySubscription newObject = new NotifySubscription();
        //     CopyObject.Copy(dbObject, newObject);

        //     newObject.Id = 0;
        //     newObject.Concurrency = 0;
        //     await ct.NotifySubscription.AddAsync(newObject);
        //     await ct.SaveChangesAsync();
        //     await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
        //     //await SearchIndexAsync(newObject, true);
        //     await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
        //     return newObject;
        // }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        // GET 
        //           
        internal async Task<NotifySubscription> GetAsync(long id, bool logTheGetEvent = true)
        {
            var ret = await ct.NotifySubscription.AsNoTracking().SingleOrDefaultAsync(z => z.Id == id && z.UserId == UserId);
            if (logTheGetEvent && ret != null)
                await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, SockEvent.Retrieved), ct);
            return ret;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        //UPDATE
        //
        internal async Task<NotifySubscription> PutAsync(NotifySubscription putObject)
        {
            //TODO: Must remove all prior events and replace them 

            var dbObject = await GetAsync(putObject.Id, false);
            if (dbObject == null)
            {
                AddError(ApiErrorCode.NOT_FOUND, "id");
                return null;
            }
            if (dbObject.Concurrency != putObject.Concurrency)
            {
                AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
                return null;
            }
            putObject.Tags = TagBiz.NormalizeTags(putObject.Tags);
            await ValidateAsync(putObject);
            if (HasErrors) return null;
            ct.Replace(dbObject, putObject);
            if (HasErrors) return null;
            try
            {
                await ct.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await ExistsAsync(putObject.Id))
                    AddError(ApiErrorCode.NOT_FOUND);
                else
                    AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
                return null;
            }
            await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, SockEvent.Modified), ct);
            await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, putObject.Tags, dbObject.Tags);
            return putObject;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////
        //DELETE
        //
        internal async Task<bool> DeleteAsync(long id)
        {

            using (var transaction = await ct.Database.BeginTransactionAsync())
            {
                var dbObject = await GetAsync(id, false);
                if (dbObject == null)
                {
                    AddError(ApiErrorCode.NOT_FOUND);
                    return false;
                }
                //ValidateCanDelete(dbObject);
                if (HasErrors)
                    return false;
               {
                    var IDList = await ct.Review.AsNoTracking().Where(x => x.SockType == SockType.NotifySubscription && x.ObjectId == id).Select(x => x.Id).ToListAsync();
                    if (IDList.Count() > 0)
                    {
                        ReviewBiz b = new ReviewBiz(ct, UserId, UserTranslationId, CurrentUserRoles);
                        foreach (long ItemId in IDList)
                            if (!await b.DeleteAsync(ItemId, transaction))
                            {
                                AddError(ApiErrorCode.CHILD_OBJECT_ERROR, null, $"Review [{ItemId}]: {b.GetErrorsAsString()}");
                                return false;
                            }
                    }
                }
                ct.NotifySubscription.Remove(dbObject);
                await ct.SaveChangesAsync();

                //Log event 
                await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.EventType.ToString(), ct);
                // await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType, ct);
                await TagBiz.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);

                //TODO: DELETE RELATED RECORDS HERE

                //all good do the commit
                await transaction.CommitAsync();

                return true;
            }
        }



        ////////////////////////////////////////////////////////////////////////////////////////////////
        //VALIDATION
        //
        private async Task ValidateAsync(NotifySubscription proposedObj)
        {

            //###############################################################################
            //todo: validate subscription is valid
            //perhaps check if customer type user doesn't have non customer notification etc

            //todo: notifysubscriptionbiz	Check for duplicate before accepting new / edit in validator
            //DISALLOW entirely duplicate notifications (down to email address)
            //USE NAME DUPE CHECK PATTERN BELOW 
            //###############################################################################

            //ensure user exists and need it for other shit later
            var user = await ct.User.AsNoTracking().FirstOrDefaultAsync(z => z.Id == proposedObj.UserId);
            if (user == null)
            {
                AddError(ApiErrorCode.VALIDATION_REQUIRED, "UserId");
            }
            else
            {

                // //Validate user can see this subscription type
                // if (user.UserType == UserType.Customer || user.UserType == UserType.HeadOffice)
                // {
                //     //Outside users can't choose inside user type notification events
                //     switch (proposedObj.EventType)
                //     {
                //         case NotifyEventType.CSRAccepted:
                //         case NotifyEventType.CSRRejected:
                //         case NotifyEventType.CustomerServiceImminent:
                //         case NotifyEventType.WorkorderCompleted:
                //         case NotifyEventType.WorkorderCreatedForCustomer:
                //             break;
                //         default:
                //             AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "EventType");
                //             break;
                //     }
                // }
                // else
                // {
                //     //Inside users can't use Outside (customer) users notification types
                //     switch (proposedObj.EventType)
                //     {
                //         case NotifyEventType.CSRAccepted:
                //         case NotifyEventType.CSRRejected:
                //         case NotifyEventType.CustomerServiceImminent:
                //             AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "EventType");
                //             break;
                //         default:
                //             break;
                //     }

                // }
            }

            if (proposedObj.DeliveryMethod == NotifyDeliveryMethod.App && !string.IsNullOrEmpty(proposedObj.DeliveryAddress))
            {
                AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "DeliveryAddress", "In app delivery should not specify a delivery address");
            }

         


        }


        /////////////////////////////////////////////////////////////////////

    }//eoc


}//eons

