package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imdroid.beidou.common.HttpResult;
import com.imdroid.secapi.client.RtcmClient;
import com.imdroid.secapi.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class GnssGroupController extends BasicController {
    @Autowired
    GnssGroupMapper gnssGroupMapper;
    @Autowired
    GnssGroupCalcMapper gnssGroupCalcMapper;
    @Autowired
    GnssDeviceMapper deviceMapper;
    @Autowired
    RtcmClient rtcmClient;

    /********* 推送页面 *********/
    @RequestMapping("/page/table/gnss_add_group")
    public String gnssAddGroup(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/table/gnss_add_group";
    }

    @RequestMapping("/page/table/gnss_add_group_calc")
    public String gnssAddCalcGroup(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/table/gnss_add_group_calc";
    }

    @RequestMapping("/page/gnss_group_cfg")
    public String gnssGroupCfg(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/gnss_group_cfg";
    }

    /********* 基本参数组 *********/
    @RequestMapping("/gnss/group/list")
    @ResponseBody
    public JSONObject list(int page, int limit) {
        Page<GnssGroup> pageable = new Page<>(page, limit);
        IPage<GnssGroup> cs = gnssGroupMapper.selectPage(pageable, null);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", cs.getTotal());
        jsonObject.put("data", cs.getRecords());
        return jsonObject;
    }

    @PostMapping("/gnss/group/update")
    @ResponseBody
    public String update(@RequestBody JSONObject object) throws Exception {
        int num = 0;
        GnssGroup group = JSONObject.toJavaObject(object,GnssGroup.class);
        GnssGroup oldGroup = gnssGroupMapper.selectById(group.getId());

        // no changed
        if(oldGroup != null && oldGroup.equals(group)) return HttpResult.ok();

        if(null != oldGroup) {
            num = gnssGroupMapper.updateById(group);
        }
        else{
            num = gnssGroupMapper.insert(group);
        }
        if (num == 0) {
            return HttpResult.failed();
        } else{
            // 更新所有的device同步标志
            deviceMapper.setSynFlagByGroupId(group.getId());
            return HttpResult.ok();
        }
    }

    @PostMapping("/gnss/group/delete")
    @ResponseBody
    public String delete(@RequestParam int del_id) throws Exception {
        GnssGroup group = gnssGroupMapper.selectById(del_id);
        if(group == null) return HttpResult.failed();

        if(group.getDevice_num() >0){
            return  HttpResult.result(HttpResult.HTTP_RSP_FAILED,"this group is in used");
        }

        int num = gnssGroupMapper.deleteById(del_id);
        if (num == 0) {
            return HttpResult.failed();
        } else return HttpResult.ok();
    }

    /********* 解算参数组 *********/
    @RequestMapping("/gnss/group/list_calc")
    @ResponseBody
    public JSONObject listCalc(int page, int limit) {
        Page<GnssGroupCalc> pageable = new Page<>(page, limit);
        IPage<GnssGroupCalc> cs = gnssGroupCalcMapper.selectPage(pageable, null);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", cs.getTotal());
        jsonObject.put("data", cs.getRecords());
        return jsonObject;
    }

    @PostMapping("/gnss/group/update_calc")
    @ResponseBody
    public String updateCalc(@RequestBody JSONObject object) {
        int num = 0;
        GnssGroupCalc group = JSONObject.toJavaObject(object,GnssGroupCalc.class);
        if(null != gnssGroupCalcMapper.selectById(group.getId())) {
            num = gnssGroupCalcMapper.updateById(group);
        }
        else{
            num = gnssGroupCalcMapper.insert(group);
        }
        if (num == 0) {
            return HttpResult.failed();
        } else{
            rtcmClient.groupParamChanged();
            return HttpResult.ok();
        }
    }

    @PostMapping("/gnss/group/delete_calc")
    @ResponseBody
    public String deleteCalc(@RequestParam int del_id) {
        GnssGroupCalc group = gnssGroupCalcMapper.selectById(del_id);
        if(group == null) return HttpResult.failed();

        if(group.getDevice_num() >0){
            return  HttpResult.result(HttpResult.HTTP_RSP_FAILED,"this group is in used");
        }

        int num = gnssGroupCalcMapper.deleteById(del_id);
        if (num == 0) {
            return HttpResult.failed();
        } else{
            rtcmClient.groupParamChanged();
            return HttpResult.ok();
        }
    }

}
