package com.huimv.environ.eco.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.eggvo.BaseResultEntity;
import com.huimv.environ.eco.entity.eggvo.DataVo;
import com.huimv.environ.eco.entity.eggvo.HistoryEntity;
import com.huimv.environ.eco.entity.eggvo.HousVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/eggData")
public class EggDataCotroller {
    private static  final  String BASE_URL= "https://iot1.bigherdsman.com/api/v2/";
    private static  final  String TOKEN= "72438b180e9168fe90db126e3310cd0b20a7196d";


    private static  final  String ACCOUNT_LOGIN_URL= "account/login/";
    private static  final  String ACCOUNT_INFO_URL= "account/info/";
    private static  final  String ENTITY_CORPORATION_URL= "entity/corporation/";
    private static  final  String ENTITY_FARM_URL= "entity/farm/";
    private static  final  String CURRENT_STATUS_URL= "current/status/?code=";
    private static  final  String LIST_ORIGIN_HOUSE= "hardware/list-origin-house/?code=";
    private static  final  String HOUSE_LIST_NAME= "entity/house/list-name/?farm=";
    private static  final  String CURRENT_LIST= "current/list/?code=";
    private static  final  String CURRENT_DETAIL= "current/detail/?code=";
    private static  final  String HISTORY_LIST= "history/list/?code=";
    private static  final  String META_LIST= "meta/list-field/?category=alarm";
    private static  final  String CURRENT_ALARMING= "current/alarming/?code=";
    private static  final  String ALARM_RECORD= "alarm/record/?code=";

    private static final String FARM_CODE = "2022011710";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("current/list")
    public Result list(){
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Token "+TOKEN);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<BaseResultEntity> forEntity = restTemplate.exchange(BASE_URL + CURRENT_LIST+FARM_CODE+"&position=all", HttpMethod.GET, httpEntity,BaseResultEntity.class );

        List<HousVo> data = forEntity.getBody().getData();
        for (HousVo datum : data) {
            datum.setName(datum.getI()+"号舍");
        }

        return new Result(ResultCode.SUCCESS,data);
    }

    @GetMapping("history/list")
    public Result list(@RequestParam(name = "i") String i,
                       @RequestParam(name = "start",required = false) Long start,
                       @RequestParam(name = "end",required = false) Long end){

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Token "+TOKEN);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(headers);
        if (start ==null){
             start = DateUtil.beginOfDay(new Date()).getTime();
        }
        if (end ==null){
            end = System.currentTimeMillis();
        }
        ResponseEntity<HistoryEntity> forEntity = restTemplate.exchange(BASE_URL + HISTORY_LIST+FARM_CODE+"&i="+i+"&start="+start+"&end="+end, HttpMethod.GET, httpEntity,HistoryEntity.class );
        return new Result(ResultCode.SUCCESS,forEntity.getBody().getData());
    }

}
