package org.ts.ddcs.Controller;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.Entity.CommandEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Worker.DataProcessHolder;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;

@RestController
@SuppressWarnings("all")
public class DataCollectController {

    @PostMapping(value = "DataCollectServer/Collect/Datagram/Supplement")
    @ResponseBody
    public JSONObject SupplementDatagram(@RequestBody JSONObject data) {
        JSONObject response = new JSONObject();
        try {
            String jsonBody = data.toJSONString();
            LogHelper.info("Supplement Datagram  : get body:" + jsonBody);
            String version = data.getString(GBConstants.BASE_KEY_AGREEMENT);
            if (null != version) {
                if (version.equals(GBConstants.AGREEMENT_KIND_SZY)) {
                    DataProcessHolder.newInstance().dataProcess.SubmitSzy2012SupplementDataGramTask(jsonBody);
                } else if (version.equals(GBConstants.AGREEMENT_KIND_SW)) {
                  //  DataProcessHolder.newInstance().dataProcess.SubmitSw2014DataGramTask(jsonBody);
                }
            }
            response.put(GBConstants.HTTP_REQUEST_KEY_CODE, GBConstants.HTTP_REQUEST_KEY_OK);
        } catch (Exception e) {
            LogHelper.error(e);
            response.put("msg", e.toString());
            response.put(GBConstants.HTTP_REQUEST_KEY_CODE, GBConstants.HTTP_REQUEST_KEY_ERROR);
        }
        return response;
    }

    @PostMapping(value = "DataCollectServer/Collect/Datagram")
    @ResponseBody
    public JSONObject PostDatagram(@RequestBody JSONObject data) {
        JSONObject response = new JSONObject();
        try {
            String jsonBody = data.toJSONString();
            LogHelper.info("http : get body:" + jsonBody);
            String version = data.getString(GBConstants.BASE_KEY_AGREEMENT);
            if (null != version) {
                if (version.equals(GBConstants.AGREEMENT_KIND_SZY)) {
                    DataProcessHolder.newInstance().dataProcess.SubmitSzy2012DataGramTask(jsonBody);
                } else if (version.equals(GBConstants.AGREEMENT_KIND_SW)) {
                    DataProcessHolder.newInstance().dataProcess.SubmitSw2014DataGramTask(jsonBody);
                }
            }
            response.put(GBConstants.HTTP_REQUEST_KEY_CODE, GBConstants.HTTP_REQUEST_KEY_OK);
        } catch (Exception e) {
            LogHelper.error(e);
            response.put("msg", e.toString());
            response.put(GBConstants.HTTP_REQUEST_KEY_CODE, GBConstants.HTTP_REQUEST_KEY_ERROR);
        }
        return response;
    }


    /**
     * 获取下发到rtu的数据包
     *
     * @param rtuCode
     * @return
     */
//    @GetMapping(value = "DataCollectServer/Download/Datagram/{rtuCode}")
//    public JSONObject getCommand(@PathVariable(name = "rtuCode") String rtuCode) {
//        JSONObject response = new JSONObject();
//        try {
//
//            if (DataProcessHolder.newInstance().dataProcess.relpaySettingCommandToRtu.containsKey(rtuCode)) {
//                LinkedBlockingQueue<CommandEntity> datagramlist = DataProcessHolder.newInstance().dataProcess.relpaySettingCommandToRtu.get(rtuCode);
//                if (null != datagramlist && !datagramlist.isEmpty()) {
//
//                    CommandEntity datagram = datagramlist.peek();
//                    if (null != datagram &&  (datagram.isNeedTry() || !datagram.isTry())) {
//
//                        response.put("datagram", datagram.getDatagram());
//                        response.put("code", "0");
//                        datagram.setLastSendTime(new Date());
//                        datagram.setTry(true);
//
//                        LogHelper.info("dwon setting command " + rtuCode + " " + datagram.getDatagram());
//
//                        if (!datagram.isWaittingBack()){
//                            datagramlist.poll();
//                        }
//                        return response;
//
//                    }
//                }
//            }
//
//            if (DataProcessHolder.newInstance().dataProcess.relpayCommandToRtu.containsKey(rtuCode)) {
//                LinkedBlockingQueue<CommandEntity> datagramlist = DataProcessHolder.newInstance().dataProcess.relpayCommandToRtu.get(rtuCode);
//                if (null != datagramlist && !datagramlist.isEmpty()) {
//                    CommandEntity datagram = datagramlist.peek();
//                    if (null != datagram &&  !datagram.isTry()) {
//
//                        response.put("datagram", datagram.getDatagram());
//                        response.put("code", "0");
//                        datagram.setLastSendTime(new Date());
//                        datagram.setTry(true);
//
//                        LogHelper.info("dwon command " + rtuCode + " " + datagram.getDatagram());
//                        return response;
//
//                    }
//                }
//            }
//
//            response.put("code", "1");
//        } catch (Exception e) {
//            e.printStackTrace();
//            response.put("code", "1");
//        }
//        return response;
//    }

}
