package com.sysu.admin.api.crop;

import com.sysu.admin.controller.crop.class_item.CropClassItem;
import com.sysu.admin.controller.crop.class_item.CropClassItemRepository;
import com.sysu.admin.controller.crop.class_item_period.CropClassItemPeriod;
import com.sysu.admin.controller.crop.class_item_period.CropClassItemPeriodRepository;
import com.sysu.admin.controller.crop.clazz.CropClass;
import com.sysu.admin.controller.crop.clazz.CropClassRepository;
import com.sysu.admin.controller.crop.period.CropPeriod;
import com.sysu.admin.controller.crop.period.CropPeriodRepository;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/api/crop_class")
@RestController
public class ApiCropClassController {

    @Autowired
    private CropClassItemPeriodRepository cropClassItemPeriodRepository;
    @Autowired
    private CropClassItemRepository cropClassItemRepository;
    @Autowired
    private CropClassRepository cropClassRepository;
    @Autowired
    private CropPeriodRepository cropPeriodRepository;

    @RequestMapping("/list")
    public BaseResult list(){
        List<CropClass> cropClassList = cropClassRepository.findAll(Sort.by(CropClass.FIELDS.id));
        List<CropClassItem> cropClassItemList = cropClassItemRepository.findAll(Sort.by(CropClassItem.FIELDS.classId));
        setItems(cropClassList, cropClassItemList);

        List<CropClassItemPeriod> cropClassItemPeriods = cropClassItemPeriodRepository.findAllOrderByCropItemId();
        CropPeriod[] cropPeriodArray = cropPeriodRepository.findAll(Sort.by(CropPeriod.FIELDS.id)).toArray(new CropPeriod[0]);
        cropClassItemList.sort((o1, o2) -> {
            return o1.getId().compareTo(o2.getId());
        });
        setPeriods(cropClassItemList, cropClassItemPeriods, cropPeriodArray);
        return R.succ(cropClassList);
    }

    private CropPeriod getPeriod(CropPeriod cropPeriod, CropPeriod[] cropPeriodArray){
        return cropPeriodArray[Arrays.binarySearch(cropPeriodArray, cropPeriod, (o1, o2) -> {
            return o1.getId().compareTo(o2.getId());
        })];
    }

    private void setPeriods(List<CropClassItem> cropClassItemList, List<CropClassItemPeriod> cropClassItemPeriods, CropPeriod[] cropPeriodArray){
        int i = 0;
        for(CropClassItem cropClassItem : cropClassItemList) {
            List<CropPeriod> periods = new ArrayList<>();
            while (i < cropClassItemPeriods.size()) {
                CropClassItemPeriod cropClassItemPeriod = cropClassItemPeriods.get(i);
                if (cropClassItemPeriod.getPk().cropItemId() > cropClassItem.getId()) {
                    break;
                }
                if (cropClassItemPeriod.getPk().cropItemId().equals(cropClassItem.getId())) {
                    periods.add(getPeriod(new CropPeriod().setId(cropClassItemPeriod.getPk().periodId()), cropPeriodArray));
                }
                i++;
            }
            cropClassItem.setPeriods(periods);
        }
    }

    private void setItems(List<CropClass> cropClassList, List<CropClassItem> cropClassItemList){
        int i = 0;
        for(CropClass cropClass : cropClassList){
            List<CropClassItem> items = new ArrayList<>();
            while (i < cropClassItemList.size()){
                CropClassItem cropClassItem = cropClassItemList.get(i);
                if(cropClassItem.getClassId() > cropClass.getId()){
                    break;
                }
                if(cropClassItem.getClassId().equals(cropClass.getId())){
                    items.add(cropClassItem);
                }
                i++;
            }
            cropClass.setItems(items);
        }
    }
}























