using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos.Interfaces;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Services;

namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;

public class ProductToAuctionDto : IProductToAuctionDto
{
    public int Id { get; set; }
    public int ProductId { get; set; }
    public int AuctionId { get; set; }
    public int WinnerCustomerId { get; set; }

    public AuctionStatus AuctionStatus { get; set; }
    public decimal StartingPrice { get; set; }
    public decimal CurrentPrice { get; set; }
    public int ProductAmount { get; set; }
    public int SortIndex { get; set; }
    public int? BiddingNumber { get; set; }
    public int BidsCount { get; set; }

    /// <summary>
    /// EGYELŐRE NE HASZNÁLD!!! - J.
    /// </summary>
    public decimal StepAmount { get; set; }

    public List<AuctionBidDto> AuctionBidDtos { get; } = [];

    public bool IsActiveItem => AuctionStatus is AuctionStatus.Active or AuctionStatus.FirstWarning or AuctionStatus.SecondWarning;

    //public decimal BidPrice { get; set; } //TODO: ez mire kell? - J.

    public ProductToAuctionDto()
    {
    }

    public ProductToAuctionDto(ProductToAuctionMapping productToAuction) : this(productToAuction, 0 /*AuctionService.GetStepAmount(productToAuction.BidPrice)*/)
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="productToAuction"></param>
    /// <param name="stepAmount">EGYELŐRE NE HASZNÁLD!!! - J.</param>
    public ProductToAuctionDto(ProductToAuctionMapping productToAuction, decimal stepAmount) : this()
    {
        if (productToAuction == null) return;

        if (stepAmount > 0) StepAmount = stepAmount;
        //else StepAmount = AuctionService.GetStepAmount(productToAuction.BidPrice);


        Id = productToAuction.Id;
        ProductId = productToAuction.ProductId;
        AuctionId = productToAuction.AuctionId;
        AuctionStatus = productToAuction.AuctionStatus;
        StartingPrice = productToAuction.StartingPrice;
        CurrentPrice = productToAuction.CurrentPrice;
        ProductAmount = productToAuction.ProductAmount;
        SortIndex = productToAuction.SortIndex;
        WinnerCustomerId = productToAuction.WinnerCustomerId;
        BidsCount = productToAuction.BidsCount;
        BiddingNumber = productToAuction.BiddingNumber;
    }

    public ProductToAuctionMapping CreateMainEntity()
    {
        var mainEntity = Activator.CreateInstance<ProductToAuctionMapping>();

        mainEntity.Id = Id;
        mainEntity.ProductId = ProductId;
        mainEntity.AuctionId = AuctionId;
        mainEntity.AuctionStatus = AuctionStatus;
        mainEntity.StartingPrice = StartingPrice;
        mainEntity.CurrentPrice = CurrentPrice;
        mainEntity.ProductAmount = ProductAmount;
        mainEntity.SortIndex = SortIndex;
        mainEntity.WinnerCustomerId = WinnerCustomerId;
        mainEntity.BidsCount = BidsCount;
        mainEntity.BiddingNumber = BiddingNumber;

        return mainEntity;
    }
}