using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DiplomovaPrace.Contracts.Customer;
using DiplomovaPrace.Contracts.Customer.Orders;
using DiplomovaPrace.Contracts.Customer.Orders.InputModels;
using DiplomovaPrace.Contracts.Customer.Orders.ViewModels;
using DiplomovaPrace.Facades.Customer.Orders;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DiplomovaPrace.Web.Server.Controllers
{
	[Route("api/order")]
	public class OrderController : ControllerBase
	{
		private readonly IOrderFacade orderFacade;

		public OrderController(IOrderFacade orderFacade)
		{
			this.orderFacade = orderFacade;
		}

		[HttpPost("addOrderItem/{userID}")]
		public async Task<IActionResult> AddOrderItemAsync([FromRoute] int userID, [FromBody] OrderItemIM orderItem)
		{
			ShoppingCartItemsAmountVM result = await orderFacade.AddOrderItemAsync(userID, orderItem);

			if (result is not null)
			{
				return Ok(result);
			}

			return BadRequest();
		}

		[HttpPost("createOrder")]
		public async Task<ActionResult<NewOrderVM>> CreateOrderAsync([FromBody] OrderIM inputModel)
		{
			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}

			//TODO: Vracet odpovídající chybové hlášky při objednání vyprodaného zboží nebo při vzniku jiné chyby (null exception atd.)
			NewOrderVM result = await orderFacade.CreateOrderAsync(inputModel);

			if (string.IsNullOrWhiteSpace(result.ErrorMessage))
			{
				return Ok(result);
			}

			return BadRequest(result);
		}

		[HttpPost("deleteOrderItem/{userID}")]
		public async Task<IActionResult> DeleteOrderItemAsync([FromRoute] int userID, [FromBody] OrderItemIM orderItem)
		{
			ShoppingCartItemsAmountVM result = await orderFacade.DeleteOrderItemAsync(userID, orderItem);

			if (result is not null)
			{
				return Ok(result);
			}

			return BadRequest();
		}

		[HttpGet("getDeliveryTypes")]
		public async Task<List<DeliveryTypeVM>> GetDeliveryTypesAsync()
		{
			return await orderFacade.GetDeliveryTypesAsync();
		}

		[HttpGet("getOrderDetail/{orderID}")]
		[Authorize(Roles = "Uživatel")]
		public async Task<OrderDetailVM> GetUserOrderDetailAsync([FromRoute] int orderID)
		{
			int userID = int.Parse(User.Identity.Name);

			return await orderFacade.GetUserOrderDetailAsync(orderID, userID);
		}

		[HttpGet("getPaymentTypes")]
		public async Task<List<PaymentTypeVM>> GetPaymentTypesAsync()
		{
			return await orderFacade.GetPaymentTypesAsync();
		}

		[HttpPost("getUserOrders")]
		[Authorize(Roles = "Uživatel")]
		public async Task<PagedQueryResult<OrderVM>> GetUserOrdersAsync([FromBody] PagedQueryRequest<OrderListQueryFilter> orderListRequest)
		{
			orderListRequest.Filter.UserID = int.Parse(User.Identity.Name);

			return await orderFacade.GetPagedOrdersAsync(orderListRequest);
		}

		[HttpGet("getUserShoppingCart/{userID}")]
		public async Task<IActionResult> GetUserShoppingCartItemsAmountAsync([FromRoute] int userID)
		{
			ShoppingCartItemsAmountVM result = await orderFacade.GetUserShoppingCartItemsAmountAsync(userID);

			if (result is not null)
			{
				return Ok(result);
			}

			return BadRequest();
		}

		[HttpGet("getUserShoppingCartItems/{userID}")]
		public async Task<List<ShoppingCartItemVM>> GetUserShoppingCartItemsAsync([FromRoute] int userID)
		{
			return await orderFacade.GetUserShoppingCartItemsAsync(userID);
		}

		[HttpPost("removeOrderItem/{userID}")]
		public async Task<IActionResult> RemoveOrderItemAsync([FromRoute] int userID, [FromBody] OrderItemIM orderItem)
		{
			ShoppingCartItemsAmountVM result = await orderFacade.RemoveOrderItemAsync(userID, orderItem);

			if (result is not null)
			{
				return Ok(result);
			}

			return BadRequest();
		}
	}
}
