import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
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.class_item_period.PKRP;
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 org.apache.commons.lang3.StringUtils;

public class CropClassPeriodExcelListener extends AnalysisEventListener<CropClassPeriodExcel> {

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

    public CropClassPeriodExcelListener(CropClassItemPeriodRepository cropClassItemPeriodRepository, CropClassItemRepository cropClassItemRepository, CropClassRepository cropClassRepository, CropPeriodRepository cropPeriodRepository) {
        this.cropClassItemPeriodRepository = cropClassItemPeriodRepository;
        this.cropClassItemRepository = cropClassItemRepository;
        this.cropClassRepository = cropClassRepository;
        this.cropPeriodRepository = cropPeriodRepository;
    }

    @Override
    public void invoke(CropClassPeriodExcel cropClassPeriodExcel, AnalysisContext analysisContext) {
        if(StringUtils.isBlank(cropClassPeriodExcel.getCropItemName()) || StringUtils.isBlank(cropClassPeriodExcel.getCropClass())){
            return;
        }
        CropClass cropClass = getCropClass(cropClassPeriodExcel.getCropClass());
        CropClassItem cropClassItem = getCropClassItem(cropClassPeriodExcel.getCropItemName(), cropClass);
        String[] periods = {
                cropClassPeriodExcel.getPeriod1(),
                cropClassPeriodExcel.getPeriod2(),
                cropClassPeriodExcel.getPeriod3(),
                cropClassPeriodExcel.getPeriod4(),
                cropClassPeriodExcel.getPeriod5(),
                cropClassPeriodExcel.getPeriod6(),
                cropClassPeriodExcel.getPeriod7(),
                cropClassPeriodExcel.getPeriod8(),
                cropClassPeriodExcel.getPeriod9(),
                cropClassPeriodExcel.getPeriod10(),
                cropClassPeriodExcel.getPeriod11(),
                cropClassPeriodExcel.getPeriod12(),
        };
        for(String period : periods){
            if(StringUtils.isNotBlank(period)){
                CropPeriod cropPeriod = getCropPeriod(period);
                PKRP pkrp = new PKRP().cropItemId(cropClassItem.getId()).periodId(cropPeriod.getId());
                CropClassItemPeriod cropClassItemPeriod = new CropClassItemPeriod().setPk(pkrp);
                cropClassItemPeriodRepository.saveAndFlush(cropClassItemPeriod);
            }
        }
    }

    private CropClass getCropClass(String name){
        CropClass bean = cropClassRepository.findByName(name);
        if(bean == null){
            bean = new CropClass().setName(name);
            bean = cropClassRepository.saveAndFlush(bean);
        }
        return bean;
    }

    private CropClassItem getCropClassItem(String name,  CropClass cropClass){
        CropClassItem bean = cropClassItemRepository.findByName(name);
        if(bean == null){
            bean = new CropClassItem().setName(name).setClassId(cropClass.getId());
            bean = cropClassItemRepository.saveAndFlush(bean);
        }
        return bean;
    }

    private CropPeriod getCropPeriod(String name){
        CropPeriod bean = cropPeriodRepository.findByName(name);
        if(bean == null){
            bean = new CropPeriod().setName(name);
            bean = cropPeriodRepository.saveAndFlush(bean);
        }
        return bean;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
