using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Dto.Common;
using CncModels.Dto.Dashboard;
using CncModels.Dto.Screen;
using CncModels.Entity;
using CncRepository.Interface;
using CncService.Interface;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 大屏看板控制器（无需认证）
    /// </summary>
    [RoutePrefix("api/screen")]
    public class ScreenController : ApiController
    {
        private readonly IDashboardService _dashboardService;
        private readonly IScreenService _screenService;
        private readonly ISysConfigRepository _sysConfigRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ScreenController(IDashboardService dashboardService, IScreenService screenService, ISysConfigRepository sysConfigRepository)
        {
            _dashboardService = dashboardService;
            _screenService = screenService;
            _sysConfigRepository = sysConfigRepository;
        }

        /// <summary>
        /// 大屏汇总统计
        /// GET /api/screen/summary
        /// </summary>
        [HttpGet]
        [Route("summary")]
        public IHttpActionResult GetSummary()
        {
            var result = _screenService.GetSummary();
            return Ok(ApiResponse<ScreenSummaryResponse>.Success(result));
        }

        /// <summary>
        /// 采集服务状态
        /// GET /api/screen/collector-status
        /// </summary>
        [HttpGet]
        [Route("collector-status")]
        public IHttpActionResult GetCollectorStatus()
        {
            var result = _dashboardService.GetCollectorStatus();
            return Ok(ApiResponse<object>.Success(result));
        }

        /// <summary>
        /// 各车间产量
        /// GET /api/screen/workshop-production
        /// </summary>
        [HttpGet]
        [Route("workshop-production")]
        public IHttpActionResult GetWorkshopProduction()
        {
            var result = _dashboardService.GetWorkshopProduction(null, null);
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 7天产量趋势
        /// GET /api/screen/production-trend
        /// </summary>
        [HttpGet]
        [Route("production-trend")]
        public IHttpActionResult GetProductionTrend(int days = 7)
        {
            var result = _dashboardService.GetProductionTrend(days);
            return Ok(ApiResponse<object>.Success(result));
        }

        /// <summary>
        /// 机床产量排行
        /// GET /api/screen/machine-rank
        /// </summary>
        [HttpGet]
        [Route("machine-rank")]
        public IHttpActionResult GetMachineRank(int top = 10)
        {
            var result = _dashboardService.GetMachineRank(null, null, top);
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 工人产量排行
        /// GET /api/screen/worker-rank
        /// </summary>
        [HttpGet]
        [Route("worker-rank")]
        public IHttpActionResult GetWorkerRank(int top = 10)
        {
            var result = _dashboardService.GetWorkerRank(null, null, top);
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 机床状态总览
        /// GET /api/screen/machine-status
        /// </summary>
        [HttpGet]
        [Route("machine-status")]
        public IHttpActionResult GetMachineStatus()
        {
            var result = _dashboardService.GetMachineStatusDistribution();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 大屏筛选条件
        /// GET /api/screen/filters
        /// </summary>
        [HttpGet]
        [Route("filters")]
        public IHttpActionResult GetFilters(string screenKey = "main_screen")
        {
            var filters = _screenService.GetFilters(screenKey);
            var items = filters.Select(f => new CncModels.Dto.Common.SimpleOption
            {
                Value = f.FilterValue,
                Label = f.FilterType
            }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }

        /// <summary>
        /// 刷新间隔配置
        /// GET /api/screen/refresh-interval
        /// </summary>
        [HttpGet]
        [Route("refresh-interval")]
        public IHttpActionResult GetRefreshInterval()
        {
            var cfg = _sysConfigRepository.GetByKey("screen_refresh_interval");
            int interval = 30;
            if (cfg != null && int.TryParse(cfg.ConfigValue, out int val))
                interval = val;
            return Ok(ApiResponse<object>.Success(new { interval }));
        }
    }
}
