package com.sysu.admin.controller.city;

import com.sysu.admin.controller.crop.CropLandService;
import com.sysu.admin.controller.crop.range.LandRangeIndexService;
import com.sysu.admin.controller.geo.qyz.QyzService;
import com.sysu.admin.support.system.region.Region;
import com.sysu.admin.support.system.region.RegionRepository;
import com.sysu.admin.utils.shape.GeoCastUtil;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import org.locationtech.jts.geom.Geometry;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 镇区域
 */
@RequestMapping("/city")
@Controller
public class CityController {

    @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(String code, int level){
        Object res = null;
        switch (level){
            case 0:
                Province province = provinceRepository.findByCode(code);
                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(code);
                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(code);
                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);
    }




}
