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;
using System.Linq;

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

        public JumpController(IJumpService jumpService,
                              IMapper mapper)
        {
            _jumpService = jumpService;
            _mapper = mapper;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Deletes a jump by its ID.
        /// </summary>
        /// <param name="id">The jump ID to delete.</param>
        [HttpDelete("{id}")]
        [EnableCors]
        public void Delete(int id)
        {
            _jumpService.DeleteJumpById(id);
        }

        /// <summary>
        /// Retrieves a list of all jumps.
        /// </summary>
        /// <returns>A JumpListResp containing all jumps and their count.</returns>
        [HttpGet]
        [EnableCors]
        public JumpListResp Get()
        {
            var tmp = _jumpService.GetAllJumps();
            var result = new JumpListResp
            {
                Rows = _mapper.Map<IEnumerable<JumpResp>>(tmp),
                Count = tmp.Count()
            };

            return result;
        }

        /// <summary>
        /// Retrieves a page of jumps with pagination.
        /// </summary>
        /// <param name="beginJumpIndex">The starting index for pagination.</param>
        /// <param name="endJumpIndex">The ending index for pagination.</param>
        /// <returns>A JumpListResp containing the requested page and total count.</returns>
        [HttpGet("{beginJumpIndex}/{endJumpIndex}")]
        [EnableCors]
        public JumpListResp Get(int beginJumpIndex, int endJumpIndex)
        {
            var totalJumps = _jumpService.GetJumpCount();
            var tmp = _jumpService.GetJumpsByIndexes(beginJumpIndex, endJumpIndex);
            var result = new JumpListResp
            {
                Rows = _mapper.Map<IEnumerable<JumpResp>>(tmp),
                Count = totalJumps
            };

            return result;
        }

        /// <summary>
        /// Retrieves a jump by its ID.
        /// </summary>
        /// <param name="id">The jump ID to retrieve.</param>
        /// <returns>A JumpResp containing the jump details.</returns>
        [HttpGet("{id}")]
        [EnableCors]
        public JumpResp Get(int id)
        {
            var result = _jumpService.GetJumpById(id);
            return _mapper.Map<JumpResp>(result);
        }

        /// <summary>
        /// Adds a new jump to the system.
        /// </summary>
        /// <param name="value">JumpReq object containing the new jump data.</param>
        [HttpPost]
        [EnableCors]
        public void Post([FromBody] JumpReq value)
        {
            _jumpService.AddNewJump(value.AircraftId,
                                    value.DropZoneId,
                                    value.JumpTypeId,
                                    value.GearId,
                                    _mapper.Map<Jump>(value));
        }

        /// <summary>
        /// Updates an existing jump.
        /// </summary>
        /// <param name="id">The jump ID to update.</param>
        /// <param name="value">JumpReq object containing the new jump data.</param>
        [HttpPut("{id}")]
        [EnableCors]
        public void Put(int id, [FromBody] JumpReq value)
        {
            _jumpService.UpdateJump(id, _mapper.Map<Jump>(value));
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IJumpService _jumpService;
        private readonly IMapper _mapper;

        #endregion Private Fields
    }
}
