using System;
using System.Linq.Expressions;
using Mapster.Models;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Entities.Accounting;
using Netina.Domain.Entities.Orders;
using Netina.Domain.Entities.Users;

namespace Netina.Domain.Mappers
{
    public static partial class PaymentMapper
    {
        public static Payment AdaptToPayment(this PaymentSDto p1)
        {
            return p1 == null ? null : new Payment()
            {
                FactorNumber = p1.FactorNumber,
                Amount = p1.Amount,
                Description = p1.Description,
                TransactionCode = p1.TransactionCode,
                CardPan = p1.CardPan,
                Authority = p1.Authority,
                Type = p1.Type,
                Status = p1.Status,
                OrderId = p1.OrderId,
                Order = new Order() {Id = p1.OrderId},
                CustomerId = p1.CustomerId,
                Customer = new Customer() {Id = p1.CustomerId},
                Id = p1.Id,
                CreatedAt = p1.CreatedAt
            };
        }
        public static Payment AdaptTo(this PaymentSDto p2, Payment p3)
        {
            if (p2 == null)
            {
                return null;
            }
            Payment result = p3 ?? new Payment();
            
            result.FactorNumber = p2.FactorNumber;
            result.Amount = p2.Amount;
            result.Description = p2.Description;
            result.TransactionCode = p2.TransactionCode;
            result.CardPan = p2.CardPan;
            result.Authority = p2.Authority;
            result.Type = p2.Type;
            result.Status = p2.Status;
            result.OrderId = p2.OrderId;
            result.Order = funcMain1(new Never(), result.Order, p2);
            result.CustomerId = p2.CustomerId;
            result.Customer = funcMain2(new Never(), result.Customer, p2);
            result.Id = p2.Id;
            result.CreatedAt = p2.CreatedAt;
            return result;
            
        }
        public static PaymentSDto AdaptToSDto(this Payment p8)
        {
            return p8 == null ? null : new PaymentSDto()
            {
                FactorNumber = p8.FactorNumber,
                Amount = p8.Amount,
                Description = p8.Description,
                TransactionCode = p8.TransactionCode,
                CardPan = p8.CardPan,
                Authority = p8.Authority,
                Type = p8.Type,
                Status = p8.Status,
                OrderId = p8.OrderId,
                CustomerId = p8.CustomerId,
                CustomerFullName = p8.Customer != null && p8.Customer.User != null ? p8.Customer.User.FirstName + " " + p8.Customer.User.LastName : string.Empty,
                CustomerPhoneNumber = p8.Customer != null && p8.Customer.User != null ? p8.Customer.User.PhoneNumber : string.Empty,
                Id = p8.Id,
                CreatedAt = p8.CreatedAt
            };
        }
        public static PaymentSDto AdaptTo(this Payment p9, PaymentSDto p10)
        {
            if (p9 == null)
            {
                return null;
            }
            PaymentSDto result = p10 ?? new PaymentSDto();
            
            result.FactorNumber = p9.FactorNumber;
            result.Amount = p9.Amount;
            result.Description = p9.Description;
            result.TransactionCode = p9.TransactionCode;
            result.CardPan = p9.CardPan;
            result.Authority = p9.Authority;
            result.Type = p9.Type;
            result.Status = p9.Status;
            result.OrderId = p9.OrderId;
            result.CustomerId = p9.CustomerId;
            result.CustomerFullName = p9.Customer != null && p9.Customer.User != null ? p9.Customer.User.FirstName + " " + p9.Customer.User.LastName : string.Empty;
            result.CustomerPhoneNumber = p9.Customer != null && p9.Customer.User != null ? p9.Customer.User.PhoneNumber : string.Empty;
            result.Id = p9.Id;
            result.CreatedAt = p9.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Payment, PaymentSDto>> ProjectToSDto => p11 => new PaymentSDto()
        {
            FactorNumber = p11.FactorNumber,
            Amount = p11.Amount,
            Description = p11.Description,
            TransactionCode = p11.TransactionCode,
            CardPan = p11.CardPan,
            Authority = p11.Authority,
            Type = p11.Type,
            Status = p11.Status,
            OrderId = p11.OrderId,
            CustomerId = p11.CustomerId,
            CustomerFullName = p11.Customer != null && p11.Customer.User != null ? p11.Customer.User.FirstName + " " + p11.Customer.User.LastName : string.Empty,
            CustomerPhoneNumber = p11.Customer != null && p11.Customer.User != null ? p11.Customer.User.PhoneNumber : string.Empty,
            Id = p11.Id,
            CreatedAt = p11.CreatedAt
        };
        
        private static Order funcMain1(Never p4, Order p5, PaymentSDto p2)
        {
            Order result = p5 ?? new Order();
            
            result.Id = p2.OrderId;
            return result;
            
        }
        
        private static Customer funcMain2(Never p6, Customer p7, PaymentSDto p2)
        {
            Customer result = p7 ?? new Customer();
            
            result.Id = p2.CustomerId;
            return result;
            
        }
    }
}