using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CounterDrone.Core;
using CounterDrone.Core.Algorithms;
using CounterDrone.Core.Models;
using CounterDrone.Core.Repository;
using CounterDrone.Core.Services;
using CounterDrone.Core.Simulation;
using Xunit;

namespace CounterDrone.Core.Tests
{
    /// <summary>仿真引擎基础单元测试（不含集成场景，集成场景见 FullPipelineTests）</summary>
    public class SimulationEngineTests : IDisposable
    {
        private readonly string _testDir;
        private readonly SimulationEngine _engine;
        private readonly IScenarioService _scenarioService;
        private readonly SQLite.SQLiteConnection _mainDb;
        private string _taskId = string.Empty;

        public SimulationEngineTests()
        {
            _testDir = Path.Combine(Path.GetTempPath(), $"cd_test_{Guid.NewGuid():N}");
            var paths = new TestPathProvider(_testDir);
            var dbManager = new DatabaseManager(paths);
            _mainDb = dbManager.OpenMainDb();
            foreach (var a in DefaultAmmunition.GetAll()) _mainDb.Insert(a);

            _scenarioService = new ScenarioService(
                new SimTaskRepository(_mainDb), new CombatSceneRepository(_mainDb),
                new ControlZoneRepository(_mainDb), new TargetConfigRepository(_mainDb),
                new EquipmentDeploymentRepository(_mainDb), new CloudDispersalRepository(_mainDb),
                new RoutePlanRepository(_mainDb), new WaypointRepository(_mainDb));

            _engine = new SimulationEngine(_scenarioService, new FrameDataStore(paths),
                new DamageModelRouter(), paths);
        }

        public void Dispose()
        {
            _engine.Stop();
            _mainDb?.Close();
            if (Directory.Exists(_testDir)) Directory.Delete(_testDir, true);
        }

        private void SetupScenario()
        {
            var task = _scenarioService.CreateTask("Test", "");
            _taskId = task.Id;
            _scenarioService.SaveScene(_taskId, new CombatScene { WindSpeed = 0 });
            _scenarioService.SaveTarget(_taskId, new TargetConfig
            {
                GroupId = "default",
                TargetType = (int)TargetType.Piston,
                Quantity = 1,
                PowerType = (int)PowerType.Piston,
                TypicalSpeed = 60,
            });
            _scenarioService.SaveDeployment(_taskId, new List<EquipmentDeployment>
            {
                new EquipmentDeployment
                {
                    EquipmentRole = (int)EquipmentRole.LaunchPlatform,
                    PlatformType = (int)PlatformType.GroundBased,
                    Quantity = 1, PositionX = 0, PositionY = 0, PositionZ = 0,
                    AerosolType = (int)AerosolType.InertGas, MunitionCount = 1, Cooldown = 5,
                },
            });
            _scenarioService.SaveCloudDispersal(_taskId, new CloudDispersal
            {
                AerosolType = (int)AerosolType.InertGas,
                DisperseHeight = 300,
                Duration = 60,
                RecommendedTiming = 10,
                PositionX = 1000,
                PositionY = 300,
                PositionZ = 0,
                PositionMode = (int)PositionMode.AlgorithmRecommended,
            });
            _scenarioService.SaveRoute(_taskId, "default", new RoutePlan { FormationMode = (int)FormationMode.Single },
                new List<Waypoint>
                {
                    new Waypoint { PosX = 0, PosY = 300, PosZ = 0, Altitude = 300, Speed = 60 },
                    new Waypoint { PosX = 10000, PosY = 300, PosZ = 0, Altitude = 300, Speed = 60 },
                });
        }

        [Fact]
        public void Initialize_LoadsEntities()
        {
            SetupScenario();
            _engine.Initialize(_taskId);
            Assert.Single(_engine.Drones);
            Assert.Equal(SimulationState.Running, _engine.State);
        }

        [Fact]
        public void Tick_DroneMoves()
        {
            SetupScenario();
            _engine.Initialize(_taskId);
            _engine.Tick(1f / 20f);
            Assert.True(_engine.Drones[0].PosX > 0);
        }

        [Fact]
        public void PauseAndResume()
        {
            SetupScenario();
            _engine.Initialize(_taskId);
            _engine.Pause();
            Assert.Equal(SimulationState.Paused, _engine.State);

            var result = _engine.Tick(1f);
            Assert.Equal(SimulationState.Paused, result.State);

            _engine.Resume();
            Assert.Equal(SimulationState.Running, _engine.State);
        }

        [Fact]
        public void Stop_ClosesGracefully()
        {
            SetupScenario();
            _engine.Initialize(_taskId);
            _engine.Tick(1f / 20f);
            _engine.Stop();
            Assert.Equal(SimulationState.Stopped, _engine.State);
        }

        [Fact]
        public void FrameData_IsWritten()
        {
            SetupScenario();
            _engine.Initialize(_taskId);
            for (int i = 0; i < 10; i++) _engine.Tick(1f / 20f);

            var framePath = Path.Combine(_testDir, "frames", $"{_taskId}.db");
            Assert.True(File.Exists(framePath));
        }
    }
}
