package com.sysu.admin.site.lz;

import com.sysu.admin.controller.lz.area.LzArea;
import com.sysu.admin.controller.lz.area.LzAreaService;
import com.sysu.admin.controller.lz.clazz.LzClazz;
import com.sysu.admin.controller.lz.clazz.LzClazzService;
import com.sysu.admin.controller.lz.everyday.LzEveryday;
import com.sysu.admin.controller.lz.everyday.LzEverydayService;
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.site.CommonVo;
import com.sysu.admin.utils.shape.GeoCastUtil;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import com.xiesx.fastboot.utils.DateUtils;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RequestMapping("/site/lz_tree")
@RestController
public class LzTreeController {

    @Autowired
    private LzTreeService lzTreeService;
    @Autowired
    private LzAreaService lzAreaService;
    @Autowired
    private LzClazzService lzClazzService;
    @Autowired
    private LzEverydayService everydayService;

    @RequestMapping("/list")
    public BaseResult list(@RequestBody(required = false) CommonVo commonVo){
        List<LzTree> list = lzTreeService.findAll(commonVo);
        List<LzTree> res = new ArrayList<>();
        int i=0;
        for(LzTree lzTree : list){
            LzEveryday lzEveryday = lzTree.getLzEveryday();
            lzTree.setWkt(GeoCastUtil.geomToWkt(lzTree.getPoint()));
            Integer warning = WarningCondition.getWarning(lzEveryday.getLzPeriod().getId(), lzEveryday.getWhitePoint());
            if(warning == 1 || i % 10 == 0) {
                res.add(lzTree);
            }
            lzTree.setPartImgUrl(lzEveryday.getPartImgUrl());
            lzTree.setWarning(warning);
            i++;
        }
        return R.succ(res);
    }

    @RequestMapping("/info")
    public BaseResult info(@RequestBody CommonVo commonVo){
        LzTree bean = null;
        if(commonVo.getId() != null) {
            bean = lzTreeService.findOne(commonVo.getId().intValue());
        }else{
            bean = lzTreeService.findByPoint(commonVo.getPoint(), 5);
            if(bean == null){
                return R.succ();
            }
        }
        Date now = new Date();
        Date start = getMonthBegin(DateUtils.addMonths(now, -1));

        List<LzEveryday> lzEverydayList = everydayService.findListByTreeId(bean.getId(),start, now);
        bean.setLzEverydays(lzEverydayList);
        return R.succ(bean);
    }

    @RequestMapping("/save")
    public BaseResult save(@RequestBody LzTree bean){
        bean.setPoint((Point) GeoCastUtil.wktToGeom(bean.getWkt()));
        LzArea lzArea = lzAreaService.findByPoint(bean.getWkt());
        LzClazz clazz = lzClazzService.findOne(bean.getClazzId());
        bean.setClazz(clazz);
        bean.setCode(lzArea.getId() + clazz.getCode() +  bean.getCode());
        bean.setLzArea(lzArea);
        lzTreeService.save(bean);
        return R.succ();
    }

    /**
     * 获取指定时间戳所在月份开始的时间戳/秒
     * @return
     */
    public static Date getMonthBegin(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTime();
    }


}
