using ApiTerceiroSimulador.Code.Integration.Models;
using ApiTerceiroSimulador.Models.Integration.v1;

namespace ApiTerceiroSimulador.Code.Integration
{
    public static class Creator
    {
        public static OrderCreationSettings CreateSettings()
        {
            DateTime now = DateTime.UtcNow;
            Guid orderId = Guid.NewGuid();
            //string merchantId = "2eff44c8-ff06-4507-8233-e3f72c4e59af",

            OrderCreationSettings settings = new()
            {
                OrderId = orderId,
                Moment = now//,
                //CreateUnmapped = createUnmapped,
                //OrderType = OrderTypeOptions.Indoor
            };

            return settings;
        }

        public static NewEventsResponse CreateEvent(OrderCreationSettings settings)
        {
            NewEventsResponse ev = new()
            {
                code = EventCodes.Placed,
                fullCode = EventFullCodes.Placed,
                createdAt = settings.Moment,
                id = Guid.NewGuid().ToString(),
                //merchantId = merchantId,
                orderId = settings.OrderId.ToString()
            };
            return ev;
        }

        public static NewEventsResponse CreateEvent(EventCreationSettings settings)
        {
            DateTime now = DateTime.UtcNow;

            NewEventsResponse ev = new()
            {
                code = settings.EventCode,
                fullCode = settings.EventFullCode,
                createdAt = now,
                id = Guid.NewGuid().ToString(),
                orderId = settings.OrderId.ToString()
            };
            return ev;
        }

        public static IntegrationItem CreateItem(OrderCreationSettings settings)
        {
            //NewEventsResponse ev = CreateEventInternal(settings);

            OrderDetailsResponse order = CreateOrderInternal(settings);

            IntegrationItem result = new()
            {
                CreatedAt = settings.Moment,
                //Event = ev,
                OrderDetail = order
            };

            return result;
        }

        private static OrderDetailsResponse CreateOrderInternal(OrderCreationSettings settings)
        {
            OrderDetailsResponse order = new()
            {
                id = settings.OrderId.ToString(),
                displayId = "8078",
                //isTest = true,
                createdAt = settings.Moment,
                orderTiming = OrderTimmings.Immediate,
                //orderType = OrderTypes.Indoor,
                preparationStartDateTime = settings.Moment,
                salesChannel = "PARTNER"
            };

            if (settings.OrderType == OrderTypeOptions.Delivery)
                order.orderType = OrderTypes.Delivery;
            else if (settings.OrderType == OrderTypeOptions.Indoor)
                order.orderType = OrderTypes.Indoor;

            order.merchant = new()
            {
                id = "2eff44c8-ff06-4507-8233-e3f72c4e59af",
                name = "Teste - Consumer Soluções em Tecnologia Ltda"
            };

            order.customer = new()
            {
                id = "910a258a-bcd3-4564-a62b-d29c61c94985",
                name = "PEDIDO DE TESTE - Fulano de Tal",
                phone = new()
                {
                    localizer = "54639728",
                    localizerExpiration = settings.Moment.AddMinutes(60),
                    number = "0800 012 3456"
                },
                segmentation = "Cliente"
            };

            if (settings.OrderType == OrderTypeOptions.Indoor)
            {
                order.indoor = new()
                {
                    mode = "TABLE",
                    deliveryDateTime = settings.Moment.AddMinutes(60),
                    table = "5",
                    observations = "Teste de Observação"
                };

                if (settings.IndoorTableNumer != null)
                    order.indoor.table = settings.IndoorTableNumer.Value.ToString();
            }
            else
            {
                order.delivery = new()
                {
                    deliveredBy = "Partner",
                    deliveryAddress = new()
                    {
                        city = "TESTE",
                        complement = "Complemento TESTE",
                        coordinates = new()
                        {
                            latitude = 0.0,
                            longitude = 0.0
                        },
                        country = "XX",
                        formattedAddress = "Rua TESTE, 999999",
                        neighborhood = "Bairro TESTE",
                        postalCode = "99999999",
                        reference = "TESTE",
                        state = "XX",
                        streetName = "Rua TESTE",
                        streetNumber = "999999"
                    },
                    deliveryDateTime = settings.Moment,
                    mode = "DEFAULT",
                    pickupCode = "1492",
                    observations = "Teste de Observação Delivery"
                };
            }

            order.items = CreateStandardItems();
            if (settings.CreateUnmapped)
            {
                List<Item> unmappeds = CreateUnmappedItems();
                order.items.AddRange(unmappeds);
            }

            /* Definição dos descontos */
            order.benefits =
            [
                new Benefit()
                {
                    target = "CART",
                    targetId = "CART",
                    value = 10.00m,
                    sponsorshipValues =
                    [
                        new SponsorshipValue
                        {
                            name = "MERCHANT",
                            value = 10.00,
                            description = "Cupom de desconto"
                        }
                    ]
                }
            ];

            order.payments = new()
            {
                methods =
                [
                    new Method()
                    {
                        card = new()
                        {
                            brand = "AMEX"
                        },
                        currency = "BRL",
                        method = "CREDIT",
                        prepaid = false,
                        type = "OFFLINE",
                        value = 51.97
                    }
                ],
                pending = 51.97,
                prepaid = 0
            };

            order.total = new()
            {
                additionalFees = 0.0,
                benefits = 10,
                deliveryFee = 3.99,
                orderAmount = 41.97,
                subTotal = 47.98
            };

            order.total.subTotal = order.items.Sum(i => i.totalPrice);
            order.total.benefits = (double)order.benefits.Sum(b => b.value);

            order.total.orderAmount =
                order.total.subTotal +
                order.total.deliveryFee +
                order.total.additionalFees -
                order.total.benefits;

            return order;
        }

        private static List<Item> CreateStandardItems()
        {
            List<Item> result = new List<Item>
            {
                //Produto normal
                new Item
                {
                    externalCode = "112",
                    id = Guid.NewGuid().ToString(),
                    imageUrl = "https://seusite.com.br/image/e078d2e1-0af0-49c3-ba0d-ea4effffbd54/202305121137_XIHJ_i.jpg",
                    index = 1,
                    name = "Alabama Apimentado",
                    unit = "UN",
                    optionsPrice = 0,
                    quantity = 2,
                    unitPrice = 22.00,
                    price = 44.00,
                    observations = "Observação produto principal",
                    totalPrice = 44.00,
                    uniqueId = Guid.NewGuid().ToString()
                },

                //Produto com pergunta exemplo 1
                new Item
                {
                    unitPrice = 30.00,
                    quantity = 1,
                    externalCode = "107",
                    totalPrice = 30.00,
                    index = 2,
                    unit = "UN",
                    price = 30.00,
                    observations = "Observação produto principal",
                    imageUrl = "https = //static-images.ifood.com.br/image/upload/t_high/pratos/e078d2e1-0af0-49c3-ba0d-ea4effffbd54/202305121137_XIHJ_i.jpg",
                    name = "Alabama Filé (exemplo perguntas)",
                    options = new List<Option>
                    {
                        new Option
                        {
                            unitPrice = 0.0,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.0,
                            name = "Ao Ponto",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.0,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.0,
                            name = "Maionese Temperada",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.0,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.0,
                            name = "Sim, quero ketchup",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 3.50,
                            unit = "UN",
                            quantity = 2,
                            externalCode = "7",
                            price = 3.50,
                            name = "Bacon",
                            id = Guid.NewGuid().ToString(),
                        },
                        new Option
                        {
                            unitPrice = 0.50,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "142",
                            price = 0.50,
                            name = "Alface",
                            id = Guid.NewGuid().ToString()
                        }
                    },
                    id = Guid.NewGuid().ToString(),
                    uniqueId = Guid.NewGuid().ToString(),
                    optionsPrice = 36,
                    addition = 0
                },

                //Produto com pergunta exemplo 2
                new Item
                {
                    unitPrice = 35.00,
                    quantity = 1,
                    externalCode = "110",
                    totalPrice = 35.00,
                    index = 2,
                    unit = "UN",
                    price = 35.00,
                    observations = "Observação produto principal",
                    imageUrl = "https = //static-images.ifood.com.br/image/upload/t_high/pratos/e078d2e1-0af0-49c3-ba0d-ea4effffbd54/202305121137_XIHJ_i.jpg",
                    name = "Alabama Bacon Duplo",
                    options = new List<Option>
                    {
                        new Option
                        {
                            unitPrice = 0.0,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.0,
                            name = "Ao Ponto",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.0,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.0,
                            name = "Maionese Temperada",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 3.50,
                            unit = "UN",
                            quantity = 2,
                            externalCode = "7",
                            price = 3.50,
                            name = "Bacon",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.50,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "143",
                            price = 0.50,
                            name = "Cebola",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 5.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "114",
                            price = 5.00,
                            name = "Coca-Cola 350ml",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.00,
                            name = "Copo com limão e gelo",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 15.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "113",
                            price = 15.00,
                            name = "Batata Frita Tradicional",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 0.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "WO",
                            price = 0.00,
                            name = "Dividir a porção em dois",
                            id = Guid.NewGuid().ToString()
                        },
                        new Option
                        {
                            unitPrice = 7.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "7",
                            price = 7.00,
                            name = "Bacon",
                            id = Guid.NewGuid().ToString()
                        }
                    },
                    id = Guid.NewGuid().ToString(),
                    uniqueId = Guid.NewGuid().ToString(),
                    optionsPrice = 36,
                    addition = 0
                },

                //Produto por tamanho dois sabores
                new Item
                {
                    unitPrice = 0,
                    quantity = 1,
                    externalCode = "T8",
                    totalPrice = 36,
                    index = 2,
                    unit = "UN",
                    price = 0,
                    observations = "Observação produto principal",
                    imageUrl = "https = //static-images.ifood.com.br/image/upload/t_high/pratos/e078d2e1-0af0-49c3-ba0d-ea4effffbd54/202305121137_XIHJ_i.jpg",
                    name = "Pizza Grande",
                    options = new List<Option>
                    {
                        new Option
                        {
                            unitPrice = 18.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "55",
                            price = 18.0,
                            name = "Calabresa",
                            index = 3,
                            id = Guid.NewGuid().ToString(),
                            addition = 0.5
                        },
                        new Option
                        {
                            unitPrice = 17.5,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "133",
                            price = 18,
                            name = "Baiana",
                            index = 4,
                            id = Guid.NewGuid().ToString(),
                            addition = 0.5
                        },
                        new Option
                        {
                            unitPrice = 5.00,
                            unit = "UN",
                            quantity = 1,
                            externalCode = "4",
                            price = 5.00,
                            name = "Borda Catupiry",
                            id = Guid.NewGuid().ToString()
                        }
                    },
                    id = Guid.NewGuid().ToString(),
                    uniqueId = Guid.NewGuid().ToString(),
                    optionsPrice = 36,
                    addition = 0,
                }
            };

            return result;
        }

        private static List<Item> CreateUnmappedItems()
        {
            List<Item> result = new List<Item>
            {
                //Produto com PDV incorreto
                new Item
                {
                    externalCode = "X94B",
                    id = Guid.NewGuid().ToString(),
                    imageUrl = "https://seusite.com.br/image/e078d2e1-0af0-49c3-ba0d-ea4effffbd54/202305121137_XIHJ_i.jpg",
                    index = 1,
                    name = "Alabama Kids",
                    unit = "UN",
                    optionsPrice = 0,
                    quantity = 2,
                    unitPrice = 25.00,
                    price = 50.00,
                    totalPrice = 50.00,
                    uniqueId = Guid.NewGuid().ToString()
                }
            };

            return result;
        }
    }
}
