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 SecurityUserController : ControllerBase
{
private readonly IApplicationService service;
private readonly ISecurityUserService securityUserService;

public SecurityUserController(IApplicationService service, ISecurityUserService securityUserService)
{
this.service = service;
this.securityUserService= securityUserService;
}

        [PermissionOperate(name: "SecurityUser", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByStaffId(string staffid)       
                    {
                    try
                    {            

                    var result = securityUserService.FindByStaffId(staffid);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

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

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

                            [PermissionOperate(name: "SecurityUser", operate: "Find")]
                            [HttpPost]
                            public dynamic FindAll()
                    {
                    try
                    {

                    var result = securityUserService.FindAll();
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityUser", operate: "Find")]
                            [HttpPost]
                            public dynamic FindBySearchCriteria(SecurityUserSearchCriteria criteria)       
                    {
                    try
                    {            

                    var result = securityUserService.FindBySearchCriteria(criteria);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityUser", operate: "Print")]
                            [HttpPost]
                            public dynamic PrintBySearchCriteria(SecurityUserSearchCriteria criteria)       
                    {
                    try
                    {            

                    var result = securityUserService.PrintBySearchCriteria(criteria);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

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

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

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

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

                            [PermissionOperate(name: "SecurityUser", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Delete(string key){
                    try
                    {            
                securityUserService.Delete(key);

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

                            [PermissionOperate(name: "SecurityUser", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Deletes(string[] keys){
                    try
                    {            
                securityUserService.Deletes(keys);

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

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic FindMenuByUsername()
                    {
                    try
                    {

                    var result = securityUserService.FindMenuByUsername();
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic UpdateLocalName(string localName){
                    try
                    {            
                securityUserService.UpdateLocalName(localName);

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

                    
}
}