package com.tmerclub.cloud.systemtest.test.amount;

import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.PayApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.ShopCartApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.api.store.TransportApi;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.order.OrderDtoData;
import com.moyuer.cloud.systemtest.data.order.ShopCartData;
import com.moyuer.cloud.systemtest.data.order.SubmitOrderDtoData;
import com.moyuer.cloud.systemtest.data.platform.CategoryDtoData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lpj
 */
public class FreightTest {
    private static List<Long> getCategoryId(Boolean isMainShop) {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(isMainShop);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(isMainShop).get(0).getCategoryId();
        CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @Test(description = "按件计算运费，购买多件相同商品，运费是否正确")
    public void sameProductsFreightWithCount() {
        //运费模板-首件2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        sameProductFreight(transportId);
    }

    @Test(description = "按件计算运费，购买多件不同商品，运费是否正确")
    public void differentProductsFreightWithCount() {
        //运费模板-首件2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        differentProducts(transportId);
    }

    @Test(description = "按重量计算运费，购买多件相同商品，运费是否正确")
    public void sameProductsFreightWithWeight() {
        //运费模板-首千克2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(1, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        sameProductFreight(transportId);
    }

    @Test(description = "按重量计算运费，购买多件不同商品，运费是否正确")
    public void differentProductsFreightWithWeight() {
        //运费模板-首千克2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(1, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        differentProducts(transportId);
    }

    @Test(description = "按体积计算运费，购买多件相同商品，运费是否正确")
    public void sameProductsFreightWithVolume() {
        //运费模板-首立方2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(2, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        sameProductFreight(transportId);
    }

    @Test(description = "按体积计算运费，购买多件不同商品，运费是否正确")
    public void differentProductsFreightWithVolume() {
        //运费模板-首立方2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(2, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        differentProducts(transportId);
    }

    @Test(description = "同一运费模板，不同商品，运费是否正确")
    public void differentProductsWithSameTransport() {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-首件2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportId = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);

        //第一笔订单，购买2件不同的商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportId, true);

        //验证订单运费是否正确
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 1000);
    }

    @Test(description = "多商品多运费模板，运费是否正确")
    public void differentProductsWithDifferentTransport() throws InterruptedException {
        List<Long> categoryIds = getCategoryId(true);
        //运费模板-首件2运费10，续件1运费5
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportIdA = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdA, 10000L), true);
        //运费模板2-首件2运费10，续件1运费5
        TimeUnit.SECONDS.sleep(2L);
        TransportApi.addTransport(DeliveryData.getTransportDTO(0, 2D, 1000L, 1D, 500L, 0, null, null, null), true);
        Long transportIdB = TransportApi.infoTransport(true).get(0).getTransportId();
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportIdB, 10000L), true);

        //第一笔订单，购买2件不同的商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNum);

        ProductApi.delete(productIdB, true);
        TransportApi.deleteTransport(transportIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportIdA, true);

        //验证订单运费是否正确
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNum, true).getFreightAmount(), 2000);
    }

    private static void sameProductFreight(Long transportId) {
        List<Long> categoryIds = getCategoryId(true);
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);

        //第一笔订单，购买1件商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNumFirst = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNumFirst);

        //第二笔订单，购买2件商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 2));
        Long orderNumSec = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNumSec);

        //第三笔订单，购买3件商品
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 3));
        Long orderNumThird = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNumThird);

        ProductApi.delete(productId, true);
        TransportApi.deleteTransport(transportId, true);

        //第一笔订单运费验证
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumFirst, true).getFreightAmount(), 1000);
        //第二笔订单运费验证
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumSec, true).getFreightAmount(), 1000);
        //第三笔订单运费验证
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumThird, true).getFreightAmount(), 1500);
    }

    private static void differentProducts(Long transportId) {
        List<Long> categoryIds = getCategoryId(true);
        Long productIdA = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        Long productIdB = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);
        Long productIdC = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 10000L), true);

        //第一笔订单，购买2件不同的商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNumFirst = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNumFirst);

        //第一笔订单，购买3件不同的商品
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdC));
        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productIdA));
        Long orderNumSec = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA)).get(0);
        PayApi.orderPay(orderNumSec);

        ProductApi.delete(productIdC, true);
        ProductApi.delete(productIdB, true);
        ProductApi.delete(productIdA, true);
        TransportApi.deleteTransport(transportId, true);

        //第一笔订单运费验证
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumFirst, true).getFreightAmount(), 1000);
        //第二笔订单运费验证
        Assert.assertEquals(ShopWalletApi.getOrderDetailVO(orderNumSec, true).getFreightAmount(), 1500);
    }
}
