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 ImageController : Base
    {
        #region Public Constructors

        public ImageController(IUserImageService imageService,
                               IMapper mapper)
        {
            _imageService = imageService;
            _mapper = mapper;
        }

        #endregion Public Constructors

        #region Public Methods

        // DELETE: api/Image/5
        [HttpDelete("{id}")]
        [EnableCors]
        public void Delete(int id)
        {
            _imageService.DeleteImageById(id);
        }

        // GET: api/Image
        [HttpGet]
        [EnableCors]
        public IEnumerable<ImageResp> Get()
        {
            var result = _imageService.GetAllImages();
            return _mapper.Map<IEnumerable<ImageResp>>(result);
        }

        // GET: api/Image/5
        [HttpGet("{id}")]
        [EnableCors]
        public ImageResp Get(int id)
        {
            var result = _imageService.GetImageById(id);
            return _mapper.Map<ImageResp>(result);
        }

        // POST: api/Image
        [HttpPost]
        [EnableCors]
        public void Post([FromBody] ImageReq value)
        {
            _imageService.AddNewImage(_mapper.Map<UserImage>(value));
        }

        // PUT: api/Image/5
        [HttpPut("{id}")]
        [EnableCors]
        public void Put(int id, [FromBody] ImageReq value)
        {
            _imageService.UpdateImage(id, _mapper.Map<UserImage>(value));
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IUserImageService _imageService;
        private readonly IMapper _mapper;

        #endregion Private Fields
    }
}