namespace Tiobon.Core.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Other)]
    public class DepartmentController : BaseApiController
    {
        private readonly IDepartmentServices _departmentServices;
        private readonly IWebHostEnvironment _env;

        public DepartmentController(IDepartmentServices departmentServices, IWebHostEnvironment env)
        {
            _departmentServices = departmentServices;
            _env = env;
        }

        [HttpGet]
        public async Task<ServiceResult<PageModel<Department>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<Department, bool>> whereExpression = a => true;

            return new ServiceResult<PageModel<Department>>()
            {
                Message = "获取成功",
                Success = true,
                Data = await _departmentServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<ServiceResult<Department>> Get(string id)
        {
            return new ServiceResult<Department>()
            {
                Message = "获取成功",
                Success = true,
                Data = await _departmentServices.QueryById(id)
            };
        }

        /// <summary>
        /// 查询树形 Table
        /// </summary>
        /// <param name="f">父节点</param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ServiceResult<List<Department>>> GetTreeTable(long f = 0, string key = "")
        {
            List<Department> departments = new List<Department>();
            var departmentList = await _departmentServices.Query(d => d.IsDeleted == false);
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            if (key != "")
            {
                departments = departmentList.Where(a => a.Name.Contains(key)).OrderBy(a => a.OrderSort).ToList();
            }
            else
            {
                departments = departmentList.Where(a => a.Pid == f).OrderBy(a => a.OrderSort).ToList();
            }

            foreach (var item in departments)
            {
                List<long> pidarr = new() { };
                var parent = departmentList.FirstOrDefault(d => d.Id == item.Pid);

                while (parent != null)
                {
                    pidarr.Add(parent.Id);
                    parent = departmentList.FirstOrDefault(d => d.Id == parent.Pid);
                }

                pidarr.Reverse();
                pidarr.Insert(0, 0);
                item.PidArr = pidarr;

                item.hasChildren = departmentList.Where(d => d.Pid == item.Id).Any();
            }


            return Success(departments, "获取成功");
        }

        /// <summary>
        /// 获取部门树
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ServiceResult<DepartmentTree>> GetDepartmentTree(long pid = 0)
        {
            var departments = await _departmentServices.Query(d => d.IsDeleted == false);
            var departmentTrees = (from child in departments
                                   where child.IsDeleted == false
                                   orderby child.Id
                                   select new DepartmentTree
                                   {
                                       value = child.Id,
                                       label = child.Name,
                                       Pid = child.Pid,
                                       order = child.OrderSort,
                                   }).ToList();
            DepartmentTree rootRoot = new DepartmentTree
            {
                value = 0,
                Pid = 0,
                label = "根节点"
            };

            departmentTrees = departmentTrees.OrderBy(d => d.order).ToList();


            RecursionHelper.LoopToAppendChildren(departmentTrees, rootRoot, pid);

            return Success(rootRoot, "获取成功");
        }

        [HttpPost]
        public async Task<ServiceResult<string>> Post([FromBody] Department request)
        {
            var data = new ServiceResult<string>();

            var id = await _departmentServices.Add(request);
            data.Success = id > 0;
            if (data.Success)
            {
                data.Data = id.ObjToString();
                data.Message = "添加成功";
            }

            return data;
        }

        [HttpPut]
        public async Task<ServiceResult<string>> Put([FromBody] Department request)
        {
            var data = new ServiceResult<string>();
            data.Success = await _departmentServices.Update(request);
            if (data.Success)
            {
                data.Message = "更新成功";
                data.Data = request?.Id.ObjToString();
            }

            return data;
        }

        [HttpDelete]
        public async Task<ServiceResult<string>> Delete(long id)
        {
            var data = new ServiceResult<string>();
            var model = await _departmentServices.QueryById(id);
            model.IsDeleted = true;
            data.Success = await _departmentServices.Update(model);
            if (data.Success)
            {
                data.Message = "删除成功";
                data.Data = model?.Id.ObjToString();
            }


            return data;
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<ServiceResult<string>> SaveData2Tsv()
        {
            var data = new ServiceResult<string>() { Success = true, Message = "" };
            if (_env.IsDevelopment())
            {

                JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                };

                var rolesJson = JsonConvert.SerializeObject(await _departmentServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
                FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "TiobonCore.Data.json", "Department_New.tsv"), rolesJson, Encoding.UTF8);

                data.Success = true;
                data.Message = "生成成功！";
            }
            else
            {
                data.Success = false;
                data.Message = "当前不处于开发模式，代码生成不可用！";
            }

            return data;
        }
    }
}