package com.sysu.admin.site;

import com.sysu.admin.controller.city.*;
import com.sysu.admin.controller.crop.CropLandService;
import com.sysu.admin.controller.crop.range.LandRangeIndexService;
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.core.token.annotation.GoToken;
import com.xiesx.fastboot.core.token.handle.CurrentToken;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 城市
 */
@RequestMapping("/site/city")
@Controller
public class SiteCityController {

    @Autowired
    ProvinceRepository provinceRepository;
    @Autowired
    CityRepository cityRepository;
    @Autowired
    DistrictRepository districtRepository;
    @Autowired
    LandRangeIndexService mLandRangeIndexService;
    @Autowired
    CropLandService cropLandService;

    /**
     * 种植结构
     * @return
     */
    @RequestMapping(value = "/zzjg")
    public String zzjg(Model model){
        model.addAttribute("pac","441821");
        return "page/zzjg";
    }

    @RequestMapping(value = "/findJson")
    @ResponseBody
    public BaseResult findJson(String code, int level){
        Object res = null;
        switch (level){
            case 0:
                Province province = provinceRepository.findByCode(code);
                res = province;
                if(province != null){
                    province.setWkt(GeoCastUtil.geomToWkt(province.getGeom()));
                }
                break;
            case 1:
                City city = cityRepository.findByCode(code);
                res = city;
                if(city != null){
                    city.setWkt(GeoCastUtil.geomToWkt(city.getGeom()));
                }
                break;
            case 2:
                District district = districtRepository.findByCode(code);
                res = district;
                if(district != null){
                    district.setWkt(GeoCastUtil.geomToWkt(district.getGeom()));
                    district.setTableName(mLandRangeIndexService.getTableNameByCode(district.getCode()));
                }
                break;
        }
        return R.succ(res);
    }

    @RequestMapping(value = "/findBuffer")
    @ResponseBody
    public BaseResult findBuffer(@GoToken() CurrentToken currentToken, @RequestBody CommonVo commonVo){
        Object res = null;
        switch (commonVo.getLevel()){
            case 0:
                Province province = provinceRepository.findByCode(commonVo.getCode());
                res = province;
                if(province != null){
                    Point point = cropLandService.getCenterPoint(province.getGeom());
                    province.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                }
                break;
            case 1:
                City city = cityRepository.findByCode(commonVo.getCode());
                res = city;
                if(city != null){
                    Point point = cropLandService.getCenterPoint(city.getGeom());
                    city.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                }
                break;
            case 2:
                District district = districtRepository.findByCode(commonVo.getCode());
                res = district;
                if(district != null){
                    Point point = cropLandService.getCenterPoint(district.getGeom());
                    district.setWkt(cropLandService.getBuffer(new Double[]{point.getX(),point.getY()}, 2000));
                }
                break;
        }
        return R.succ(res);
    }




}
