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

namespace Hncore.Pass.Manage.Controllers
{

    public class ManagerTagController : ManageControllerBase
    {
        EfDbContext m_DbContext { get; set; }

        private  ManagerTagService _managerTagService;


        public ManagerTagController(EfDbContext _DbContext,
            ManagerTagService managerTagService,
           IHttpContextAccessor hca) : base(hca)

        {
            m_DbContext = _DbContext;
            _managerTagService = managerTagService;
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="request">请求参数对象</param>
        /// <returns>响应结果对象</returns>
        [HttpGet]
        
        public async Task<ApiResult<List<ManagerTagResponse>>> GetList([FromQuery]ManagerTagRequest request)
        {                  
            (int total, List<ManagerTagResponse> list) res = await _managerTagService.GetTagList(request);
            return  SuccessPaged(res.total, res.list, "成功");           
        }

        /// <summary>
        /// 根据管理员标签获取管理员权限
        /// </summary>
        /// <param name="request">请求参数对象</param>
        /// <returns>响应结果对象</returns>
        [HttpGet]
        
        public async Task<ApiResult<List<ManagerTagToPermissionResponse>>> GetTagToPerssionList ([FromQuery]ManagerTagToPerRequest request)
        {          
                (int total, List<ManagerTagToPermissionResponse> list) res = await _managerTagService.GetTagToPerssionList(request);
                return  SuccessPaged(res.total, res.list, "成功");
         
        }

        /// <summary>
        /// 创建管理员标签
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        
        public async Task<ApiResult> Post([FromBody] RequestBase<EditManagerTagRequest> param)
        {

            var managerTag   = await Domain.ManagerTag.Create(param, m_DbContext.Set<ManagerTag>().GetQueryable());
            m_DbContext.Set<ManagerTag>().Add(managerTag);
            var res = UofCommit(() => new CreateOrEditManagerTagResponse().FromEntity(managerTag));

            List< ManagerTagToPermission> list =new List<ManagerTagToPermission>();
            foreach (var item in param.Data.Permissions)
            {
                ManagerTagToPermission mp =new ManagerTagToPermission();
                mp.OwnerId = param.TenantId;
                mp.ManagerTagId = managerTag.Id;
                mp.PermissionCode = item.PermissionCode; 
                mp.AllowView = item.AllowView;
                mp.AllowAdd = item.AllowAdd;
                mp.AllowEdit = item.AllowEdit;
                mp.AllowDel = item.AllowDel;
                mp.CreateTime = DateTime.Now;
                mp.UpdateTime = DateTime.Now;
                mp.CreatorId = param.OperaterId;
                list.Add(mp);
            }
            await m_DbContext.Set<ManagerTagToPermission>().AddRangeAsync(list);
            return await UofCommitAsync(() => new CreateOrEditManagerTagResponse().FromEntity(managerTag));
        }

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

        public async Task<ApiResult> Put([FromBody] RequestBase<EditManagerTagRequest> param)
        {
            var managerTag = await m_DbContext.Set<ManagerTag>().FindByIdAsync(param.Data.Id);
            await managerTag.Edit(param, m_DbContext.Set<ManagerTag>().GetQueryable());
            return await UofCommitAsync(() => new CreateOrEditManagerTagResponse().FromEntity(managerTag));
        }

        /// <summary>
        /// 删除管理员标签
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<ApiResult> Delete([FromBody] QueryByIdRequest param)
        {
            var managerTag   = await m_DbContext.Set<ManagerTag>().FindByIdAsync(param.Id);
            managerTag.Delete(param.OperaterId);
            return await UofCommitAsync(() => managerTag);
        }

    }
}