using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Entity;
using CncService.Interface;
using CncWebApi.Infrastructure;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 大屏配置控制器
    /// </summary>
    [RoutePrefix("api/admin")]
    [JwtAuthFilter]
    public class ScreenConfigController : ApiController
    {
        private readonly IScreenService _screenService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ScreenConfigController(IScreenService screenService)
        {
            _screenService = screenService;
        }

        #region 卡片配置

        /// <summary>
        /// 卡片配置列表
        /// GET /api/admin/screen-config
        /// </summary>
        [HttpGet]
        [Route("screen-config")]
        public IHttpActionResult GetConfigs()
        {
            var result = _screenService.GetConfigs();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 编辑卡片
        /// PUT /api/admin/screen-config/{id}
        /// </summary>
        [HttpPut]
        [Route("screen-config/{id:int}")]
        public IHttpActionResult UpdateConfig(int id, [FromBody] ScreenConfig entity)
        {
            if (entity == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.BadRequest, "请求参数不能为空");
            entity.Id = id;
            var result = _screenService.UpdateConfig(entity);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 删除卡片
        /// DELETE /api/admin/screen-config/{id}
        /// </summary>
        [HttpDelete]
        [Route("screen-config/{id:int}")]
        public IHttpActionResult DeleteConfig(int id)
        {
            var result = _screenService.DeleteFilter(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 启停卡片
        /// PUT /api/admin/screen-config/{id}/toggle
        /// </summary>
        [HttpPut]
        [Route("screen-config/{id:int}/toggle")]
        public IHttpActionResult ToggleConfig(int id)
        {
            var configs = _screenService.GetConfigs();
            var config = configs.FirstOrDefault(c => c.Id == id);
            if (config == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.NotFound, "配置不存在");
            config.IsEnabled = config.IsEnabled == 1 ? 0 : 1;
            _screenService.UpdateConfig(config);
            return Ok(ApiResponse<object>.Success(null));
        }

        #endregion

        #region 筛选配置

        /// <summary>
        /// 筛选配置列表
        /// GET /api/admin/screen-filter
        /// </summary>
        [HttpGet]
        [Route("screen-filter")]
        public IHttpActionResult GetFilters(string screenKey = null)
        {
            var result = _screenService.GetFilters(screenKey ?? "main_screen");
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 新增筛选项
        /// POST /api/admin/screen-filter
        /// </summary>
        [HttpPost]
        [Route("screen-filter")]
        public IHttpActionResult CreateFilter([FromBody] ScreenFilter entity)
        {
            if (entity == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.BadRequest, "请求参数不能为空");
            var id = _screenService.CreateFilter(entity);
            return Ok(ApiResponse<object>.Success(new { id }));
        }

        /// <summary>
        /// 编辑筛选项
        /// PUT /api/admin/screen-filter/{id}
        /// </summary>
        [HttpPut]
        [Route("screen-filter/{id:int}")]
        public IHttpActionResult UpdateFilter(int id, [FromBody] ScreenFilter entity)
        {
            if (entity == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.BadRequest, "请求参数不能为空");
            entity.Id = id;
            var result = _screenService.UpdateFilter(entity);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 删除筛选项
        /// DELETE /api/admin/screen-filter/{id}
        /// </summary>
        [HttpDelete]
        [Route("screen-filter/{id:int}")]
        public IHttpActionResult DeleteFilter(int id)
        {
            var result = _screenService.DeleteFilter(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        #endregion
    }
}
