using System;
using System.Threading;
using System.Threading.Tasks;
using Autolabor.Benz.ObstacleDetection.DataType;
using Autolabor.Robobase;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Autolabor.Benz.ObstacleDetection
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        private readonly RectRoiSettings _roiSettings;

        public Worker(ILogger<Worker> logger, IOptions<RectRoiSettings> roiSettings)
        {
            _logger = logger;
            _roiSettings = roiSettings.Value;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            foreach (var roi in _roiSettings.RectRois)
            {
                _logger.LogInformation("RectRoi: leftUp({0},{1}) - rightDown({2},{3})",
                    roi.LeftUpX, roi.LeftUpY, roi.RightDownX, roi.RightDownY);
            }
            
            // 创建用户配置文件
            var app = Kits.GetAutolaborDataPath();
            var path = System.IO.Path.Combine(app, "BenzObstacleDetection.bin");
            UserDataManager.Initialize(path);
            
            // 输出日志
            TestKit.Logger.Trace("BenzObstacleDetection start");
            
            // 启动顶层组件
            var detection = new Detection(_roiSettings);
            detection.Enable = true;
        }
    }
}