using System.Collections.Generic;
using System.IO;
using System.Text.Json;

using skydiveLogs_api.Business.Interfaces;
using skydiveLogs_api.Domain;


namespace skydiveLogs_api.Business
{
    public class InitDbService : IInitDbService
    {
        public InitDbService(IAircraftService aircraftService,
                             IJumpTypeService jumpTypeService,
                             IDropZoneService dropZoneService)
        {
            _aircraftService = aircraftService;
            _jumpTypeService = jumpTypeService;
            _dropZoneService = dropZoneService;
        }

        public void GenerateDb()
        {
            LoadAircrafts();
            LoadDropZones();
            LoadJumpTypes();
        }

        private void LoadDropZones()
        {
            var jsonString = File.ReadAllText("Init/dropZone.json");
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            };
            var jsonModel = JsonSerializer.Deserialize<List<DropZone>>(jsonString, options);

            foreach (var item in jsonModel)
            {
                _dropZoneService.AddNewDz(item);
            }
        }

        private void LoadJumpTypes()
        {
            var jsonString = File.ReadAllText("Init/jumpType.json");
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            };
            var jsonModel = JsonSerializer.Deserialize<List<JumpType>>(jsonString, options);

            foreach (var item in jsonModel)
            {
                _jumpTypeService.AddNewJumpType(item);
            }
        }

        private void LoadAircrafts()
        {
            var jsonString = File.ReadAllText("Init/aircraft.json");
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            };
            var jsonModel = JsonSerializer.Deserialize<List<Aircraft>>(jsonString, options);

            foreach (var item in jsonModel)
            {
                _aircraftService.AddNewAircraft(item);
            }
        }

        private readonly IAircraftService _aircraftService;

        private readonly IJumpTypeService _jumpTypeService;

        private readonly IDropZoneService _dropZoneService;
    }
}
