using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.Policy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using CloudBuilder.Core.Service;
using CloudBuilder.Security.Service;
            using CloudBuilder.Core.DatabaseAccessor.Entity;
            using Microsoft.AspNetCore.Http;
            using CloudBuilder.Core.Policy;
            using CloudBuilder.Core.DependencyInjection.Request;
            using CloudBuilder.Security.Data;
            using CloudBuilder.Core.Authorization;
            using CloudBuilder.Security.Entity;
            using CloudBuilder.Security.SearchCriteria;
            using CloudBuilder.Security;
            using System;
            
namespace CloudBuilder.Web.Entry.Controllers.Security
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class SecurityLockController : ControllerBase
{
private readonly IApplicationService service;
private readonly ISecurityLockService securityLockService;

public SecurityLockController(IApplicationService service, ISecurityLockService securityLockService)
{
this.service = service;
this.securityLockService= securityLockService;
}

        [PermissionOperate(name: "SecurityLock", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByKey(SecurityLockEntityKey key)       
                    {
                    try
                    {            

                    var result = securityLockService.FindByKey(key);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityLock", operate: "Insert")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Insert(SecurityLockEntity data)       
                    {
                    try
                    {            

                    var result = securityLockService.Insert(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityLock", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Update(SecurityLockEntity data)       
                    {
                    try
                    {            

                    var result = securityLockService.Update(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityLock", operate: "Delete")]
                            [HttpPost]
                            public dynamic Delete(SecurityLockEntityKey key){
                    try
                    {            
                securityLockService.Delete(key);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class IsLockedCompositionData
                    { 
                     public string entityName { get; set; }
                         public string key { get; set; }
                        
                    }
                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic IsLocked(IsLockedCompositionData compositionData)       
                    {
                    try
                    {            

                    var result = securityLockService.IsLocked(compositionData.entityName,compositionData.key);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                    
                    public class LockCompositionData
                    { 
                     public string entityName { get; set; }
                         public string key { get; set; }
                         public string username { get; set; }
                        
                    }
                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic Lock(LockCompositionData compositionData){
                    try
                    {            
                securityLockService.Lock(compositionData.entityName,compositionData.key,compositionData.username);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class ForceLockCompositionData
                    { 
                     public string entityName { get; set; }
                         public string key { get; set; }
                         public string username { get; set; }
                        
                    }
                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic ForceLock(ForceLockCompositionData compositionData){
                    try
                    {            
                securityLockService.ForceLock(compositionData.entityName,compositionData.key,compositionData.username);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class ConsumeLockCompositionData
                    { 
                     public string entityName { get; set; }
                         public string key { get; set; }
                         public string username { get; set; }
                        
                    }
                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic ConsumeLock(ConsumeLockCompositionData compositionData){
                    try
                    {            
                securityLockService.ConsumeLock(compositionData.entityName,compositionData.key,compositionData.username);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
                    public class ClearLockCompositionData
                    { 
                     public string entityName { get; set; }
                         public string key { get; set; }
                        
                    }
                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic ClearLock(ClearLockCompositionData compositionData){
                    try
                    {            
                securityLockService.ClearLock(compositionData.entityName,compositionData.key);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                    
}
}