package com.sysu.admin.api.crop;

import com.alibaba.fastjson.JSON;
import com.sysu.admin.controller.aland.*;
import com.sysu.admin.controller.crop.CropLand;
import com.sysu.admin.controller.crop.images.CropImage;
import com.sysu.admin.controller.crop.images.ImageType;
import com.sysu.admin.site.CommonVo;
import com.sysu.admin.support.base.BaseComponent;
import com.sysu.admin.utils.shape.GeoCastUtil;
import com.xiesx.fastboot.base.pagination.PaginationHelper;
import com.xiesx.fastboot.base.pagination.PaginationResult;
import com.xiesx.fastboot.base.pagination.PaginationVo;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
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.*;

@RequestMapping("/api/land")
@RestController("ApiLandController2")
public class ApiLandController extends BaseComponent {

    @Autowired
    LandService landService;

    @RequestMapping("/land_area_stat")
    public BaseResult getLandAreaStat(@RequestBody CommonVo commonVo){
        return R.succ(landService.getLandAreaStat(commonVo.getMonth(), commonVo.getDistrict(), commonVo.getCity()));
    }

    @RequestMapping("/crop_area_stat")
    public BaseResult getCropAreaStat(@RequestBody CommonVo commonVo){
        return R.succ(landService.getCropAreaStat(commonVo.getMonth(),commonVo.getDistrict(), commonVo.getCity()));
    }

    @RequestMapping("/soil_stat")
    public BaseResult getStatSoil(@RequestBody CommonVo commonVo){
        return R.succ(landService.getStatSoil(commonVo.getDistrict(), commonVo.getCity(), commonVo.getStatType()));
    }

    @RequestMapping("/soil_cutline")
    public BaseResult getSoilCutline(){
        Set<String> stringSet = StatItemData.typeMap.keySet();
        Map<String, Map> res= new HashMap<>();
        stringSet.stream().forEach(s -> {
            String[] names = StatItemData.get(s,"Names");
            String[] colors = StatItemData.get(s,"Colors");
            Map<String, String[]> item = new HashMap<>();
            item.put("names", names);
            item.put("colors", colors);
            res.put(s, item);
        });
        return R.succ(res);
    }

    @RequestMapping("/crop_cutline")
    public BaseResult getCropCutline(){
        List<Map<String,String>> list = new ArrayList<>();
        for(int i=0;i<CropAreaStat.cropTypes.length;i++){
            Map<String,String> map = new HashMap<>();
            map.put("name", CropAreaStat.name2s[i]);
            map.put("type", CropAreaStat.cropTypes[i]);
            map.put("color", CropAreaStat.colors[i]);
            list.add(map);
        }
        return R.succ(list);
    }

    @RequestMapping("/land_cutline")
    public BaseResult getLandCutline(){
        List<Map<String,String>> list = new ArrayList<>();
        for(int i=0;i<LandAreaStat.name2s.length;i++){
            Map<String,String> map = new HashMap<>();
            map.put("id", i+"");
            map.put("name", LandAreaStat.name2s[i]);
            map.put("color", LandAreaStat.colors[i]);
            list.add(map);
        }
        return R.succ(list);
    }

    @RequestMapping("/estimate_stat")
    public BaseResult getEstimate(@RequestBody CommonVo commonVo){
        Double p = landService.getPercent(commonVo.getMonth());
        List<StatEstimate> list = landService.getEstimate(commonVo.getDistrict(), commonVo.getCity(), commonVo.getCrop_type());
        list.forEach(statEstimate -> {
            statEstimate.setValue(statEstimate.getValue() * p);
        });
        return R.succ(list);
    }


    @RequestMapping("/list")
    public PaginationResult list(@RequestBody CommonVo commonVo){
        PaginationVo paginationVo = new PaginationVo(commonVo.getPage(), commonVo.getLimit(), commonVo.getLimit());
        PaginationResult result  = landService.list(commonVo, paginationVo);
        ((List<Land>)result.data()).stream().forEach(land -> {
            land.setCropTypeName(CropAreaStat.castName(land.getCrop_type()));
            land.setParcelTypeName(LandAreaStat.castName(land.getParcel_type()));
            //计算总产量
            if(land.getDanChan() != null) {
                land.setZongChan(land.getDanChan() * land.getArea());
            }
            land.setGeojson(GeoCastUtil.gjson.toString(land.getGeom()));
        });
        return result;
    }

    @RequestMapping("/allLandStat")
    public BaseResult allLandStat(@RequestBody CommonVo commonVo){
        return R.succ(landService.allLandStat(commonVo));
    }

    @RequestMapping("/info")
    public BaseResult info(@RequestBody CommonVo commonVo){
        Land bean = null;
        if(commonVo.getId() != null){
            bean = landService.findOne(commonVo.getId());
        }else if(commonVo.getPoint() != null){
            bean = landService.findByPoint(commonVo.getPoint());
        }
        return R.succ(bean);
    }

}
