using AutoMapper;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.DataContract;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;
using System.Collections.Generic;

namespace skydiveLogs_api.Controllers
{
    public class AircraftController : Base
    {
        #region Public Constructors

        public AircraftController(IAircraftService aircraftService,
                                  IMapper mapper)
        {
            _aircraftService = aircraftService;
            _mapper = mapper;
        }

        #endregion Public Constructors

        #region Public Methods

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        [EnableCors]
        public void Delete(int id)
        {
            _aircraftService.DeleteAircraftById(id);
        }

        // GET: api/Aircraft
        [HttpGet]
        [EnableCors]
        public IEnumerable<AircraftResp> Get()
        {
            var result = _aircraftService.GetAllAircrafts();
            return _mapper.Map<IEnumerable<AircraftResp>>(result);
        }

        // GET: api/Aircraft/5
        [HttpGet("{id}")]
        [EnableCors]
        public AircraftResp Get(int id)
        {
            var result = _aircraftService.GetAircraftById(id);
            return _mapper.Map<AircraftResp>(result);
        }

        [HttpGet("GetSimple")]
        [EnableCors]
        public IEnumerable<AircraftSimpleResp> GetSimple()
        {
            var result = _aircraftService.GetAllAircrafts();
            return _mapper.Map<IEnumerable<AircraftSimpleResp>>(result);
        }

        // POST: api/Aircraft
        [HttpPost]
        [EnableCors]
        public void Post([FromBody] AircraftReq value)
        {
            _aircraftService.AddNewAircraft(_mapper.Map<Aircraft>(value));
        }

        // PUT: api/Aircraft/5
        [HttpPut("{id}")]
        [EnableCors]
        public void Put(int id, [FromBody] AircraftReq value)
        {
            _aircraftService.UpdateAircraft(id, _mapper.Map<Aircraft>(value));
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IAircraftService _aircraftService;
        private readonly IMapper _mapper;

        #endregion Private Fields
    }
}