
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using FMSAdmin.Data;
using FMSAdmin.Helpers;
using FMSAdmin.Entities;
using FMSAdmin.Services;
using Microsoft.AspNetCore.Authorization;

namespace FMSAdmin.Controllers {
    [Authorize]
    [ApiController]
    [ApiVersion("1")]
    [Route("api/[controller]")]
    public class SitemapAuthController : Controller {
        private readonly ILogger<SitemapAuthController> _logger;
        private readonly FMSContext _context;
        private readonly SitemapAuthService _service;

        public SitemapAuthController(
            ILogger<SitemapAuthController> logger,
            FMSContext context,
            SitemapAuthService service
            ) {
            _logger = logger;
            _context = context;
            _service = service;
        }

        /// <summary>
        /// 권한 일괄 수정
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="userGroupId"></param>
        /// <param name="type"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPost("{type}")]
        public IActionResult Edit(int siteId, int userGroupId, SitemapType type, [FromBody]SitemapAuth[] list) {
            _logger.LogInformation(list.Dump());

            if (list == null) {
                return BadRequest("데이터를 입력해주세요");
            }
            foreach (var data in list) {
                if (data.SiteId != siteId) {
                    return BadRequest("현장정보가 잘못되었습니다");
                }
                if (data.UserGroupId != userGroupId) {
                    return BadRequest("사용자그룹이 잘못되었습니다");
                }
                if (data.SitemapType != type) {
                    return BadRequest("메뉴종류가 잘못되었습니다");
                }
            }

            _service.Save(siteId, userGroupId, type, list);

            return Ok(list.Count());
        }

        /// <summary>
        /// 자식 노드 리스트
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("childs/{type}/{parentId?}")]
        public IActionResult Childs(int siteId, int userGroupId, SitemapType type, int? parentId) {
            var query = _service.SitemapAll().Where(x => x.Type == type && x.ParentId == parentId);

            // 최대 4뎁스
            var list = query.OrderBy(x => x.Position).Select(c => new {
                key = c.SitemapId,
                title = c.Name,
                action1 = c.Action1,
                action2 = c.Action2,
                action3 = c.Action3,
                action4 = c.Action4,
                action5 = c.Action5,
                auth = c.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
                    x.Access,
                    x.Update,
                    x.Delete,
                    x.Business,
                    x.Action1,
                    x.Action2,
                    x.Action3,
                    x.Action4,
                    x.Action5
                }).FirstOrDefault(),
                children = c.Childs.OrderBy(x => x.Position).Select(c1 => new {
                    key = c1.SitemapId,
                    title = c1.Name,
                    action1 = c1.Action1,
                    action2 = c1.Action2,
                    action3 = c1.Action3,
                    action4 = c1.Action4,
                    action5 = c1.Action5,
                    auth = c1.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
                        x.Access,
                        x.Update,
                        x.Delete,
                        x.Business,
                        x.Action1,
                        x.Action2,
                        x.Action3,
                        x.Action4,
                        x.Action5
                    }).FirstOrDefault(),
                    children = c1.Childs.OrderBy(x => x.Position).Select(c2 => new {
                        key = c2.SitemapId,
                        title = c2.Name,
                        action1 = c2.Action1,
                        action2 = c2.Action2,
                        action3 = c2.Action3,
                        action4 = c2.Action4,
                        action5 = c2.Action5,
                        auth = c2.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
                            x.Access,
                            x.Update,
                            x.Delete,
                            x.Business,
                            x.Action1,
                            x.Action2,
                            x.Action3,
                            x.Action4,
                            x.Action5
                        }).FirstOrDefault(),
                        children = c2.Childs.OrderBy(x => x.Position).Select(c3 => new {
                            key = c3.SitemapId,
                            title = c3.Name,
                            action1 = c3.Action1,
                            action2 = c3.Action2,
                            action3 = c3.Action3,
                            action4 = c3.Action4,
                            action5 = c3.Action5,
                            auth = c3.SitemapAuths.Where(x => x.SiteId == siteId && x.UserGroupId == userGroupId).Select(x => new {
                                x.Access,
                                x.Update,
                                x.Delete,
                                x.Business,
                                x.Action1,
                                x.Action2,
                                x.Action3,
                                x.Action4,
                                x.Action5
                            }).FirstOrDefault(),
                        })
                    })
                })
            });

            return Ok(list);
        }

    }
}