using System;
using System.Collections.Generic;
using CounterDrone.Core.Models;
using CounterDrone.Core.Repository;

namespace CounterDrone.Core.Services
{
    public class DataService : IDataService
    {
        private readonly AmmunitionSpecRepository _ammoRepo;
        private readonly FireUnitSpecRepository _fireUnitRepo;
        private readonly DroneSpecRepository _droneRepo;
        private readonly SensorSpecRepository _sensorRepo;
        private readonly EnvironmentSpecRepository _envRepo;
        private readonly FormationTemplateRepository _formationRepo;
        private readonly RouteTemplateRepository _routeRepo;

        public DataService(AmmunitionSpecRepository ammoRepo, FireUnitSpecRepository fireUnitRepo,
            DroneSpecRepository droneRepo, SensorSpecRepository sensorRepo,
            EnvironmentSpecRepository envRepo, FormationTemplateRepository formationRepo,
            RouteTemplateRepository routeRepo)
        {
            _ammoRepo = ammoRepo;
            _fireUnitRepo = fireUnitRepo;
            _droneRepo = droneRepo;
            _sensorRepo = sensorRepo;
            _envRepo = envRepo;
            _formationRepo = formationRepo;
            _routeRepo = routeRepo;
        }

        // ═══ AmmunitionSpec ═══
        public List<AmmunitionSpec> GetAllAmmo() => _ammoRepo.GetAll();
        public AmmunitionSpec GetAmmo(string id) => _ammoRepo.GetById(id);
        public void SaveAmmo(AmmunitionSpec spec) { if (_ammoRepo.GetById(spec.Id) != null) _ammoRepo.Update(spec); else _ammoRepo.Insert(spec); }
        public void DeleteAmmo(string id) => _ammoRepo.Delete(id);

        // ═══ FireUnitSpec ═══
        public List<FireUnitSpec> GetAllFireUnits() => _fireUnitRepo.GetAll();
        public FireUnitSpec GetFireUnit(string id) => _fireUnitRepo.GetById(id);
        public void SaveFireUnit(FireUnitSpec spec) { if (_fireUnitRepo.GetById(spec.Id) != null) _fireUnitRepo.Update(spec); else _fireUnitRepo.Insert(spec); }
        public void DeleteFireUnit(string id) => _fireUnitRepo.Delete(id);

        // ═══ DroneSpec ═══
        public List<DroneSpec> GetAllDrones() => _droneRepo.GetAll();
        public DroneSpec GetDrone(string id) => _droneRepo.GetById(id);
        public void SaveDrone(DroneSpec spec) { if (_droneRepo.GetById(spec.Id) != null) _droneRepo.Update(spec); else _droneRepo.Insert(spec); }
        public void DeleteDrone(string id) => _droneRepo.Delete(id);

        // ═══ SensorSpec ═══
        public List<SensorSpec> GetAllSensors() => _sensorRepo.GetAll();
        public SensorSpec GetSensor(string id) => _sensorRepo.GetById(id);
        public void SaveSensor(SensorSpec spec) { if (_sensorRepo.GetById(spec.Id) != null) _sensorRepo.Update(spec); else _sensorRepo.Insert(spec); }
        public void DeleteSensor(string id) => _sensorRepo.Delete(id);

        // ═══ EnvironmentSpec ═══
        public List<EnvironmentSpec> GetAllEnvironments() => _envRepo.GetAll();
        public EnvironmentSpec GetEnvironment(string id) => _envRepo.GetById(id);
        public void SaveEnvironment(EnvironmentSpec spec) { if (_envRepo.GetById(spec.Id) != null) _envRepo.Update(spec); else _envRepo.Insert(spec); }
        public void DeleteEnvironment(string id) => _envRepo.Delete(id);

        // ═══ FormationTemplate ═══
        public List<FormationTemplate> GetAllFormations() => _formationRepo.GetAll();
        public FormationTemplate GetFormation(string id) => _formationRepo.GetById(id);
        public void SaveFormation(FormationTemplate spec) { if (_formationRepo.GetById(spec.Id) != null) _formationRepo.Update(spec); else _formationRepo.Insert(spec); }
        public void DeleteFormation(string id) => _formationRepo.Delete(id);

        // ═══ RouteTemplate ═══
        public List<RouteTemplate> GetAllRoutes() => _routeRepo.GetAll();
        public RouteTemplate GetRoute(string id) => _routeRepo.GetById(id);
        public void SaveRoute(RouteTemplate spec) { if (_routeRepo.GetById(spec.Id) != null) _routeRepo.Update(spec); else _routeRepo.Insert(spec); }
        public void DeleteRoute(string id) => _routeRepo.Delete(id);
    }
}
