package com.bonus.canteen.core.allocation.api;

import cn.hutool.core.util.StrUtil;
import com.bonus.canteen.core.allocation.alloc.model.AllocMetadata;
import com.bonus.canteen.core.allocation.canteen.dto.AllocCanteenDeliveryDTO;
import com.bonus.canteen.core.allocation.canteen.dto.AllocCanteenDeliveryModel;
import com.bonus.canteen.core.allocation.canteen.model.AllocDeliveryCostModel;
import com.bonus.canteen.core.allocation.util.MetadataUtil;
import com.bonus.canteen.core.common.enums.DeliveryTypeEnum;
import com.bonus.canteen.core.common.enums.MetadataModelTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

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

@Component
public class AllocDeliveryApi {
   @Autowired
   @Lazy
   private GlobalMetadataApi globalMetadataApi;

   public AllocCanteenDeliveryModel queryDeliveryModel(Long canteenId, Long stallId) {
      List<AllocMetadata> availableList = this.globalMetadataApi.getAvailableList(MetadataModelTypeEnum.DELIVERY, canteenId, stallId);
      return MetadataUtil.meteDataListToDelivery(availableList);
   }

   public BigDecimal getTakeOutPackingFee(Long canteenId, Long stallId) {
      AllocCanteenDeliveryModel canteenDeliveryModel = this.queryDeliveryModel(canteenId, stallId);
      if (StrUtil.isBlank(canteenDeliveryModel.getDeliveries())) {
         return BigDecimal.ZERO;
      } else {
         Integer deliveryType = DeliveryTypeEnum.TAKE_OUT.getKey();
         Iterator var5 = canteenDeliveryModel.getDeliveryList().iterator();

         AllocCanteenDeliveryDTO deliveryDTO;
         do {
            if (!var5.hasNext()) {
               return BigDecimal.ZERO;
            }

            deliveryDTO = (AllocCanteenDeliveryDTO)var5.next();
         } while(!deliveryType.equals(deliveryDTO.getDeliveryType()));

         return deliveryDTO.getPackingFee();
      }
   }

   public List<AllocDeliveryCostModel> queryDeliveryCostModel(Long stallId) {
      AllocCanteenDeliveryModel canteenDeliveryModel = this.queryDeliveryModel((Long)null, stallId);
      List<AllocDeliveryCostModel> deliveryCostModelList = new ArrayList();
      if (StrUtil.isBlank(canteenDeliveryModel.getDeliveries())) {
         return deliveryCostModelList;
      } else {
         String[] deliveryTypeArr = canteenDeliveryModel.getDeliveries().split(",");
         String[] var5 = deliveryTypeArr;
         int var6 = deliveryTypeArr.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            String s = var5[var7];
            Integer deliveryType = Integer.parseInt(s);
            if (DeliveryTypeEnum.SELF_TAKE.getKey().equals(deliveryType)) {
               AllocDeliveryCostModel deliveryCostModel = new AllocDeliveryCostModel(deliveryType);
               deliveryCostModelList.add(deliveryCostModel);
            } else {
               Iterator var10 = canteenDeliveryModel.getDeliveryList().iterator();

               while(var10.hasNext()) {
                  AllocCanteenDeliveryDTO deliveryDTO = (AllocCanteenDeliveryDTO)var10.next();
                  if (deliveryType.equals(deliveryDTO.getDeliveryType())) {
                     AllocDeliveryCostModel deliveryCostModel = new AllocDeliveryCostModel(deliveryDTO);
                     deliveryCostModelList.add(deliveryCostModel);
                     break;
                  }
               }
            }
         }

         return deliveryCostModelList;
      }
   }
}
