package cn.edu.whu.boot.allocation.controllers;

import cn.edu.whu.boot.allocation.controllers.dtos.TableField;
import cn.edu.whu.boot.allocation.entities.Draft;
import cn.edu.whu.boot.allocation.services.DraftService;
import cn.edu.whu.boot.common.utils.collection.OtherUtil;
import cn.edu.whu.boot.common.domain.ResultMessage;
import cn.edu.whu.boot.common.utils.collection.PostgreSqlUtil;
import cn.edu.whu.boot.xml.service.ITaskService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/draft")
public class DraftController {

    @Autowired
    private DraftService draftService;

    @Autowired
    private ITaskService taskService;

    private static Logger logger = LoggerFactory.getLogger(DraftController.class);

    @RequestMapping("/insert")
    @ResponseBody
    public ResultMessage insertDraft(@RequestParam("detail") @NotBlank(message = "数据不能为空") String detail)
    {
        JSONObject jsonObject = JSON.parseObject(detail);
        int taskId = jsonObject.getInteger("taskId");
        JSONObject draftJsonObj = jsonObject.getJSONObject("draft");
        Draft draft = JSON.toJavaObject(draftJsonObj, Draft.class);
        draft.setDate(OtherUtil.changeTimeString(draft.getDate()));
        byte[] shapeByte = Base64.getDecoder().decode(draft.getShape());
        draft.setShape(new String(shapeByte));
        draftService.insert(PostgreSqlUtil.transformString(taskService.getTaskById(taskId).getDatabaseName()), draft);
        return ResultMessage.success(null);
    }

    @RequestMapping("/fields")
    @ResponseBody
    public ResultMessage getDraftField() {

        Map<String, TableField> map = new HashMap<>();
        TableField tableField;

        tableField = new TableField();
        tableField.setName("upload");
        tableField.setType("int");
        tableField.setChinese("ID");
        map.put("upload", tableField);

        tableField = new TableField();
        tableField.setName("userName");
        tableField.setType("String");
        tableField.setChinese("采集人员");
        map.put("userName", tableField);

        tableField = new TableField();
        tableField.setName("date");
        tableField.setType("Date");
        tableField.setChinese("绘制时间");
        map.put("date", tableField);

        tableField = new TableField();
        tableField.setName("shape");
        tableField.setType("String");
        tableField.setChinese("草图shape");
        map.put("shape", tableField);

        tableField = new TableField();
        tableField.setName("uploadTime");
        tableField.setType("Date");
        tableField.setChinese("上传时间");
        map.put("uploadTime", tableField);

        return new ResultMessage<>(true, "成功获取草图表字段对照名称", map);
    }
}


