using Back.Common;
using Back.Data.Contracts;
using Back.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace Back.Services
{
    public class CheckPermission
    {
        private readonly IAsyncRepository<PermissionPeriod> _repoPermissionPeriod;
        private readonly IAsyncRepository<PermissionUser> _repoPermissionUser;
        public CheckPermission(IAsyncRepository<PermissionPeriod> repoPermissionPeriod
                                ,IAsyncRepository<PermissionUser> repoPermissionUser)
        {
            _repoPermissionPeriod = repoPermissionPeriod;
            _repoPermissionUser = repoPermissionUser;
        }
        private async Task<bool> AllowPermissionInCompany(int CompanyID,int PermissionID,int Allowednumber = 1)
        {
               PermissionPeriod permissionPeriod =await _repoPermissionPeriod
                .TrackingGet(w => w.CompanyID == CompanyID && w.PermissionID == PermissionID && (!w.IsLocked.HasValue || !w.IsLocked.Value)
                )
                .FirstOrDefaultAsync();

            if (permissionPeriod == null)
                return false;

            //تعداد
            if (permissionPeriod.CalculationTypeID == 1)
            {
                if (permissionPeriod.RemainingAmount < Allowednumber)
                    return false;

                //permissionPeriod.RemainingAmount -= 1;

            }
            //تا تاریخ
            else if (permissionPeriod.CalculationTypeID == 3)
            {
           
                string date = $"{permissionPeriod.RemainingAmount.ToString().Substring(0, 4)}/{permissionPeriod.RemainingAmount.ToString().Substring(4, 2)}/{permissionPeriod.RemainingAmount.ToString().Substring(6, 2)}";
                DateTime dateTime = date.ToMiladi();
                if (DateTime.Now > dateTime)
                    return false;

            }
          return await  _repoPermissionPeriod.UpdateAsync(permissionPeriod);

        }
        public async Task<bool> AllowPermission(int UserID,int CompanyID, int PermissionID)
        {

           return await _repoPermissionUser
                .TrackingGet(w => w.RolUser.UserID == UserID && w.RolUser.CompanyID == CompanyID && w.PermissionID==PermissionID
                )
                .AnyAsync();

           

        }
        public async Task<bool> ExtensionofAccess(int CompanyID, int PermissionID, string value)
        {
            PermissionPeriod permissionPeriod = await _repoPermissionPeriod
                .TrackingGet(w => w.CompanyID == CompanyID && w.PermissionID == PermissionID
                && (!w.IsLocked.HasValue || !w.IsLocked.Value))
                .FirstOrDefaultAsync();

           
            //تعداد
            if (permissionPeriod.CalculationTypeID == 1)
            {
                if (permissionPeriod.RemainingAmount < 0)
                    permissionPeriod.RemainingAmount = Convert.ToInt32(value);

                permissionPeriod.RemainingAmount += Convert.ToInt32(value);

            }
            try
            {
                var res= await _repoPermissionPeriod.UpdateAsync(permissionPeriod); 
                return res;
                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/ExtensionofAccess",
                //    Value = $"{permissionPeriod.RemainingAmount - Convert.ToInt32(value)}+({value})={permissionPeriod.RemainingAmount}",
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "User"
                //};
                //_contextMongodb.InsertItem(log);
            }
            catch (Exception ex)
            {
                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/ExtensionofAccess",
                //    Value = ex.Message,
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "catch"
                //};
                //_contextMongodb.InsertItem(log);
                return false;
            }
            
        }

        //--------Customer---------
        #region Customer
        public async Task<bool> AllowAddCustomerInCompany(int CompanyID, int Allowednumber = 1)
        {
            //مشتری
            int PermissionID = 5;
          return  await AllowPermissionInCompany(CompanyID, PermissionID,Allowednumber);
            
        }
        public async Task<bool> AllowSYSGetCustomer(int UserID, int CompanyID)
        {
            //مشتری
            int PermissionID = 5;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSAddCustomer(int UserID, int CompanyID)
        {
            //مشتری
            int PermissionID = 7;
            return await AllowPermission(UserID,CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSDeleteCustomer(int UserID, int CompanyID)
        {
            //مشتری
            int PermissionID = 9;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSUpdateCustomer(int UserID, int CompanyID)
        {
            //مشتری
            int PermissionID = 8;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        #endregion
        //-----------COD-----------
        #region COD
        public async Task<bool> AllowAddCODInCompany(int CompanyID,int Allowednumber=1)
        {
            //کالا
            int PermissionID = 4;
            return await AllowPermissionInCompany(CompanyID, PermissionID,Allowednumber);

        }
        public async Task<bool> AllowSYSGetCOD(int UserID, int CompanyID)
        {
            //کالا
            int PermissionID = 4;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSAddCOD(int UserID, int CompanyID)
        {
            //کالا
            int PermissionID = 10;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSDeleteCOD(int UserID, int CompanyID)
        {
            //کالا
            int PermissionID = 12;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSUpdateCOD(int UserID, int CompanyID)
        {
            //کالا
            int PermissionID = 11;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        #endregion
        //--------Invoice---------
        #region Invoice
        public async Task<bool> AllowAddInvoiceInCompany(int CompanyID, int Allowednumber = 1)
        {
            int PermissionID = 3;
            return await AllowPermissionInCompany(CompanyID, PermissionID, Allowednumber);

        }
        public async Task<bool> AllowSYSGetInvoice(int UserID, int CompanyID)
        {
            int PermissionID = 3;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSAddInvoice(int UserID, int CompanyID)
        {
            int PermissionID = 13;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSDeleteInvoice(int UserID, int CompanyID)
        {
            int PermissionID = 15;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSUpdateInvoice(int UserID, int CompanyID)
        {
            int PermissionID = 14;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        #endregion
        #region TaxPayer
        public async Task<bool> AllowSendTaxPayerInCompany(int CompanyID)
        {
            int PermissionID = 16;
            return await AllowPermissionInCompany(CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSGetTaxPayer(int UserID, int CompanyID)
        {
            int PermissionID = 16;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        public async Task<bool> AllowSYSSendTaxPayer(int UserID, int CompanyID)
        {
            int PermissionID = 16;
            return await AllowPermission(UserID, CompanyID, PermissionID);

        }
        #endregion
    }
}
