package com.yc.ship.module.trade.convert.ota;

import cn.hutool.core.util.ObjectUtil;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.module.ota.api.dto.DistributorProductRespDTO;
import com.yc.ship.module.trade.api.dto.TradeOrderCreateReqDTO;
import com.yc.ship.module.trade.enums.SellMethodEnum;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.yc.ship.module.trade.enums.TravelhubDictTypeConstants.PRODUCT_BASE_TYPE_SELF;


@Mapper
public interface OtaConvert {

    OtaConvert INSTANCE = Mappers.getMapper(OtaConvert.class);


   default TradeOrderCreateReqDTO.OrderProperty convert(String distributorId, String distributorName){
       TradeOrderCreateReqDTO.OrderProperty orderProperty = new TradeOrderCreateReqDTO.OrderProperty();

       orderProperty.setVisitorType(0);  // 散客
       orderProperty.setSellMethod(SellMethodEnum.OTA.getType()); // OTA售票
       orderProperty.setSourceId(distributorId);
       orderProperty.setSourceName(distributorName);
       orderProperty.setSellerId(distributorId);
       return orderProperty;
   }

    default TradeOrderCreateReqDTO.OrderDetail convert(DistributorProductRespDTO distributorProduct, Integer count, List<DistributorProductRespDTO.DistributorProjectDTO> distributorProjectDTOS, BigDecimal price){
        TradeOrderCreateReqDTO.OrderDetail orderDetail = new TradeOrderCreateReqDTO.OrderDetail();
        orderDetail.setCount(count);
        orderDetail.setProductId(distributorProduct.getProductId()+"");
        if (price != null){
            orderDetail.setPrice(price);
            orderDetail.setOriginPrice(price);
        }else {
            orderDetail.setPrice(distributorProduct.getSalePrice());
            orderDetail.setOriginPrice(distributorProduct.getSalePrice());
        }

        orderDetail.setProductType(PRODUCT_BASE_TYPE_SELF);
        List<TradeOrderCreateReqDTO.OrderDetailBase> detailBaseList = convertOrderDetailBaseList(distributorProduct.getDistributorProjectList());
        if(distributorProjectDTOS!=null && distributorProjectDTOS.size()>0) {
            for (DistributorProductRespDTO.DistributorProjectDTO distributorProjectDTO : distributorProjectDTOS) {
                for (TradeOrderCreateReqDTO.OrderDetailBase orderDetailBase : detailBaseList) {
                    if(ObjectUtil.equal(distributorProjectDTO.getProductBaseId(),distributorProjectDTO.getProductBaseId())){
                        detailBaseList.remove(orderDetailBase);
                        break;
                    }
                }
            }
        }
        
        orderDetail.setDetailBaseList(detailBaseList);
        orderDetail.setSpecType(distributorProduct.getSpecType());
        return orderDetail;
    }



    default List<TradeOrderCreateReqDTO.OrderDetailBase> convertOrderDetailBaseList(List<DistributorProductRespDTO.DistributorProjectDTO> distributorProjectList) {
        return CollectionUtils.convertList(distributorProjectList, item -> {
            TradeOrderCreateReqDTO.OrderDetailBase orderDetailBase = convert(item);
            return orderDetailBase;
        });
    }

    TradeOrderCreateReqDTO.OrderDetailBase convert(DistributorProductRespDTO.DistributorProjectDTO projectDTO);


   default List<TradeOrderCreateReqDTO.Visitor> convertOtaVisitorList (Integer ticketCount){
       List<TradeOrderCreateReqDTO.Visitor> visitors = new ArrayList<>();
       for (int i = 1; i <= ticketCount; i++) {
           TradeOrderCreateReqDTO.Visitor visitor = new TradeOrderCreateReqDTO.Visitor();
           visitor.setCredentialNo("");
           visitor.setName("");
           visitors.add(visitor);
       }
       return visitors;

   }
}
