using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Hncore.Pass.Manage.Request;
using Hncore.Pass.Manage.Response;
using Hncore.Pass.Manage.Service;
using Hncore.Infrastructure.Common;
using Hncore.Pass.Manage.Domain;
using Hncore.Pass.Manage.Repository;
using Hncore.Infrastructure.EF;

namespace Hncore.Pass.Manage.Controllers
{

    public class ManagerToPermissionController : ManageControllerBase
    {

        EfDbContext m_DbContext { get; set; }

        private ManagerPermissionService _managerPermissionService ;

        public ManagerToPermissionController(EfDbContext _DbContext,
            ManagerPermissionService managerPermissionService,
           IHttpContextAccessor hca) : base(hca)
        {
            m_DbContext = _DbContext;
            _managerPermissionService = managerPermissionService;

        }
        /// <summary>
        /// 创建管理员权限
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        
        public async Task<ApiResult> Post([FromBody] RequestBase<EditManagerToPermissionRequest> param)
        {
            var managerToPermission = await Domain.ManagerToPermission.Create(param, m_DbContext.Set<ManagerToPermission>().GetQueryable());
            await m_DbContext.Set<ManagerToPermission>().AddAsync(managerToPermission);
            return await UofCommitAsync(() => new CreateOrEditManagerToResponse().FromEntity(managerToPermission));

        }

        /// <summary>
        /// 编辑管理员权限
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<ApiResult> Put([FromBody] RequestBase<EditManagerToPermissionRequest> param)
        {
            var managerToPermission = await m_DbContext.Set<ManagerToPermission>().FindByIdAsync(param.Data.Id);

            await managerToPermission.Edit(param, m_DbContext.Set<ManagerToPermission>().GetQueryable());

            return await UofCommitAsync(() => new CreateOrEditManagerToResponse().FromEntity(managerToPermission));
        }


        /// <summary>
        /// 根据操作人获取权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetByOperator([FromQuery]QueryByIdRequest request)
        {
            List<QueryPermissionResponse> list = await _managerPermissionService.GetByOperator(request,true);
            return Success(list, "成功");
        }
        /// <summary>
        /// 根据登录人的权限权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetPermissions()
        {
            QueryByIdRequest request = new QueryByIdRequest() { Id = this.Request.GetManageUserInfo().OperaterId };
            List<QueryPermissionResponse> list = await _managerPermissionService.GetByOperator(request, false);
            return Success(list, "成功");
        }
    }
}