using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

using APT.Utility;
using APT.BaseData.Domain.Entities.BD;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Services.Services.FM;
using APT.BaseData.Domain.Enums;

namespace APT.PF.WebApiControllers.Api.PF
{
    [Route("api/PF/Menu")]
    public class MenuController : AuthorizeTreeApiController<T_PF_MENU>
    {
        IPFMenuService MenuService { get; set; }
        public MenuController(IPFMenuService menuService)
        {
            MenuService = menuService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_PF_MENU>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_MENU>> OrderEntities([FromBody] KeywordFilter filter)
        {
            filter.FilterGroup.Rules.Add(new FilterRule() { Field = "PLATFORM_CATEGORY", Operate = FilterOperate.Equal, Value = 0 });
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("NWOrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_MENU>> NWOrderEntities([FromBody] KeywordFilter filter)
        {
            filter.FilterGroup.Rules.Add(new FilterRule() { Field = "PLATFORM_CATEGORY", Operate = FilterOperate.Equal, Value = 1 });
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_MENU> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_MENU> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_PF_MENU entity)
        {
            return SafeExecute<bool>(() =>
            {
                //查所有下级菜单
                //List<Guid> parendIds = new List<Guid>();
                //parendIds.Add(entity.ID);
                //List<Guid> childIds = new List<Guid>() { entity.ID };
                //var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId.Value;
                //GetChildIds(parendIds, childIds);
                //var menuList = this.GetEntities<T_PF_MENU>(t => childIds.Contains(t.ID), new BaseFilter(orgId)).ToList();
                //menuList.ForEach(t => t.MineType = entity.MineType);
                //T_PF_MENU parentMenu = null;
                //if (entity.PARENT_ID != null)
                //{
                //	parentMenu = this.GetEntity<T_PF_MENU>(entity.PARENT_ID.ToString(),true);
                //	parentMenu.HAS_CHILDREN = true;
                //	entity.MENU_LEVEL = parentMenu.MENU_LEVEL + 1;
                //	parentMenu.IS_LEAF = false;
                //}
                //else
                //	entity.MENU_LEVEL = 1;
                //UnifiedCommit(() =>
                //{
                //	this.TreeUpdateEntityNoCommit(entity);
                //	if (parentMenu != null)
                //		this.TreeUpdateEntityNoCommit(parentMenu);
                //});
                //return true;
                entity.IS_PERMISSION_MENU = true;
                //this.UnifiedCommit(() =>
                //{
                //    if (entity != null)
                //        this.UpdateEntityNoCommit(entity);
                //    if (menuList != null && menuList.Any())
                //        this.BantchSaveEntityNoCommit(menuList);
                //});
                return this.UpdateEntity(entity); ;
            });
        }
        [HttpPost, Route("NWUpdate")]
        public JsonActionResult<bool> NWUpdate([FromBody] T_PF_MENU entity)
        {
            return SafeExecute<bool>(() =>
            {
                entity.PLATFORM_CATEGORY = 1;
                entity.IS_PERMISSION_MENU = true;
                return this.UpdateEntity(entity);
            });
        }
        /// <summary>
        /// 获取子菜单ID
        /// </summary>
        /// <param name="parendIds"></param>
        /// <param name="childIds"></param>
        private void GetChildIds(List<Guid> parendIds, List<Guid> childIds)
        {
            var tmp = this.GetEntities<T_PF_MENU>(t => parendIds.Contains(t.PARENT_ID ?? Guid.Empty), new BaseFilter()).Select(t => t.ID).ToList();
            if (tmp != null && tmp.Count > 0)
            {
                childIds.AddRange(tmp);
                GetChildIds(tmp, childIds);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(id)) return false;
                List<Guid> deleteIds = new List<Guid>();
                deleteIds.Add(new Guid(id));
                List<Guid> parendIds = new List<Guid>();
                parendIds.Add(new Guid(id));
                GetChildIds(parendIds, deleteIds);
                this.TreeDeleteEntity<T_PF_MENU>(t => deleteIds.Contains(t.ID));
                return true;
            });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(ids)) return false;
                string[] tmp = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (tmp == null || tmp.Length == 0) return false;
                List<Guid> deleteIds = new List<Guid>();
                List<Guid> parendIds = new List<Guid>();
                tmp.ForEach(t =>
                {
                    deleteIds.Add(new Guid(t));
                    parendIds.Add(new Guid(t));
                });
                GetChildIds(parendIds, deleteIds);
                this.TreeDeleteEntity<T_PF_MENU>(t => deleteIds.Contains(t.ID));
                return true;
            });
        }
        /// <summary>
        /// 根据ID获得实体数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_PF_MENU> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }

        /// <summary>
        /// 获取菜单图片图标
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedIcon")]
        public PagedActionResult<T_BD_PICTURE> OrderPagedIcon([FromBody] KeywordPageFilter filter)
        {
            return SafeGetPagedData<T_BD_PICTURE>((result) =>
            {
                var enumCode = "MENUICON";
                var enumId = GetEntity<T_FM_ENUM>(x => x.CODE == enumCode);
                var data = GetOrderPageEntities<T_BD_PICTURE>(x => x.ENUM_ID == enumId.ID, filter, "Nav_PictureFiles");
                result.Data = data.Data;
                result.TotalCount = data.TotalCount;
            });
        }

        /// <summary>
        /// 获取菜单图片图标
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("AppMenuPic")]
        public PagedActionResult<T_BD_PICTURE> AppMenuPic([FromBody] KeywordPageFilter filter)
        {
            return SafeGetPagedData<T_BD_PICTURE>((result) =>
            {
                var enumCode = "APPPIC";
                var enumId = GetEntity<T_FM_ENUM>(x => x.CODE == enumCode);
                var data = GetOrderPageEntities<T_BD_PICTURE>(x => x.ENUM_ID == enumId.ID, filter, "Nav_PictureFiles");
                result.Data = data.Data;
                result.TotalCount = data.TotalCount;
            });
        }
    }
}
