package com.sysu.admin.controller.lz.everyday;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sysu.admin.controller.lz.period.LzPeriod;
import com.sysu.admin.controller.lz.period.WarningCondition;
import com.sysu.admin.controller.lz.tree.LzTree;
import com.sysu.admin.controller.lz.tree.LzTreeService;
import com.sysu.admin.utils.TextUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class LzEveryExcelListener extends AnalysisEventListener<LzEverydayExcel> {
    private final static SimpleDateFormat formatDate = new SimpleDateFormat("yyyy/MM/dd");
    List<LzEveryday> list = new ArrayList<>();
    private LzTreeService lzTreeService;
    private LzEverydayService everydayService;
    private LzEverydayImgService LzEverydayImgService;

    public LzEveryExcelListener(LzTreeService lzTreeService, LzEverydayService everydayService, LzEverydayImgService lzEverydayImgService) {
        this.lzTreeService = lzTreeService;
        this.everydayService = everydayService;
        LzEverydayImgService = lzEverydayImgService;
    }



    @Override
    public void invoke(LzEverydayExcel everydayExcel, AnalysisContext analysisContext) {
        if(StringUtils.isBlank(everydayExcel.getCode()))
            return;

        LzEveryday bean = new LzEveryday();
        LzTree lzTree = lzTreeService.findByCode(everydayExcel.getCode());
        bean.setCreateDate(everydayExcel.getDate());
        String fileNameBase = dateToDir(bean.getCreateDate()) + "/"+ lzTree.getCode() + "/";
        bean.setPartImgUrl(fileNameBase + "part.JPG");
        bean.setTreeId(lzTree.getId());
        LzPeriod lzPeriod = new LzPeriod();
        lzPeriod.setId(everydayExcel.getPeriod_id());
        bean.setLzPeriod(lzPeriod);
        bean.setFlowerLength(everydayExcel.getFlowerLength());
        bean.setFlowerPercent(everydayExcel.getFlowerPercent());
        bean.setYield(everydayExcel.getYield());
        bean.setWhitePoint(everydayExcel.getWhitePoint());
        bean.setFlowerAreaPercent(everydayExcel.getFlowerAreaPercent());
        bean.setRedLeafPercent(everydayExcel.getRedLeafPercent());
        everydayService.saveAndFlush(bean);
        saveImages(bean.getId(), everydayExcel.getImgCount(), fileNameBase);
        saveTreeNewEveryDay(lzTree, bean);
    }

    public String dateToDir(Date date) {
        String ds = formatDate.format(date);
        String s = TextUtil.rightSubstring("/",ds);
        if(s.charAt(0) == '0'){
            return TextUtil.leftSubstring("/",ds) + "/" + s.substring(1);
        }
        return TextUtil.leftSubstring("/",ds) + "/" + s;
    }

    public List<LzEverydayImage> saveImages(Integer everydayId, Integer imgCount, String fileNameBase){

        List<LzEverydayImage> res = new ArrayList<>(imgCount);
        for(int i=1;i<=5;i++){
            LzEverydayImage lzEverydayImage = new LzEverydayImage();
            lzEverydayImage.setEverydayId(everydayId);
            lzEverydayImage.setFilename(fileNameBase + i+".JPG");
            res.add(lzEverydayImage);
        }
        LzEverydayImgService.saveAll(res);
        return res;
    }

    public void saveTreeNewEveryDay(LzTree lzTree, LzEveryday lzEveryday){
        if(lzTree.getLzEveryday() == null || lzTree.getLzEveryday().getCreateDate().compareTo(lzEveryday.getCreateDate())  < 0) {
            lzTree.setLzEveryday(lzEveryday);
            //有预警就以后都要显示
//            if (lzTree.getShow() == 0){
//                Integer warning = WarningCondition.getWarning(lzEveryday.getLzPeriod().getId(), lzEveryday.getWhitePoint());
//                lzTree.setShow(warning);
//            }
        }
        lzTreeService.save(lzTree);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
