package com.sagacloud.endpoints;
/*
 * Author: Jxing
 * Create Time: 2018/7/17
 */

import com.alibaba.fastjson.*;
import com.sagacloud.utils.*;
import com.sagacloud.utils.objects.ObjectIdName;
import com.sagacloud.utils.objects.ObjectType;
import com.sagacloud.utils.tables.HBaseTable;
import com.sagacloud.utils.tables.ObjectTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("shaft")
public class VerticalShaft {
    private static final Logger log = LoggerFactory.getLogger(Floor.class);
    @Path("/create")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String create(@QueryParam(Const.PROJECT_ID) String projectId, String jsonStr){
        try {
            JSONObject insertJson = JSONObject.parseObject(jsonStr);
            if(!insertJson.containsKey(Const.JSON_FIELD_JSON_DATA_INFOS)){
                return DpUtils.errorJsonMsg("缺少必要字段");
            }
//            String building_id = null;
//            if(insertJson.get("building_id") != null){
//                building_id = insertJson.get("building_id").toString();
//                // check if building exist
//                JSONObject checkCriteria = new JSONObject();
//                checkCriteria.put(ObjectTable.KEY_PJ_ID, projectId.substring(2));
//                checkCriteria.put(ObjectTable.KEY_OBJ_TYPE, Const.OBJ_TYPE_BUILDING);
//                checkCriteria.put(ObjectTable.KEY_OBJ_ID, building_id.substring(12));
//                checkCriteria.put(Const.TABLE_KEY_NAME_VALID, true);
//                if(CommonUtil.getCount(HBaseTable.objects, checkCriteria) <= 0)
//                    throw new Exception("建筑体不存在.");
//                // check end.
//            }
            String assignedObjId = null;
            if(insertJson.get("id")!= null )
            {
                if(insertJson.get("id").toString().startsWith(Const.OBJ_TYPE_VERTICAL_SHAFT)) {
                    String flId = insertJson.get("id").toString();
                    assignedObjId = flId.substring(12);
                    if (!DpUtils.verifyAssignedObjID(assignedObjId)) {
                        return DpUtils.errorJsonMsg("指定的id错误.");
                    }
                }else
                    return DpUtils.errorJsonMsg("指定的id错误.");
            }

            ObjectIdName name = new ObjectIdName();
            String id = ObjectUtil.addObj(projectId.substring(2), Const.OBJ_TYPE_VERTICAL_SHAFT,"",
                    (JSONObject) insertJson.get(Const.JSON_FIELD_JSON_DATA_INFOS), ObjectType.shaft, name, assignedObjId, true);
//            if(building_id != null){
//                //insert building-floor relation
//                SubsetUtil.addRel(projectId, building_id, id);
//            }
            return DpUtils.successJsonMsgWithId("", id, name.getNameKey(), name.getNameValue());
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return DpUtils.errorJsonMsg(ex.getMessage() == null? "添加失败!": ex.getMessage());
        }
    }

    @Path("/delete")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String delete(@QueryParam(Const.PROJECT_ID) String projectId, String jsonStr){
        try
        {
            JSONObject criteria = JSONObject.parseObject(jsonStr);
            criteria = (JSONObject) criteria.get("criteria");
            if(!criteria.containsKey("id") || criteria.get("id") == null || !criteria.get("id").toString().startsWith(Const.OBJ_TYPE_VERTICAL_SHAFT))
                throw new Exception("id错误!");

            // check if id is valid.
            JSONObject validCriteria = new JSONObject();
            validCriteria.put(ObjectTable.KEY_PJ_ID, criteria.get("id").toString().substring(2,12));
            validCriteria.put(ObjectTable.KEY_OBJ_TYPE, Const.OBJ_TYPE_VERTICAL_SHAFT);
            validCriteria.put(ObjectTable.KEY_OBJ_ID, criteria.get("id").toString().substring(12));
            validCriteria.put(Const.TABLE_KEY_NAME_VALID, true);
            if(CommonUtil.getCount(HBaseTable.objects, validCriteria) <= 0)
                throw new Exception("不存在或者已被删除!");

            CommonUtil.deleteObject(criteria.get("id").toString(), criteria.get("id").toString().substring(2,12),
                    criteria.get("id").toString().substring(0,2), criteria.get("id").toString().substring(12), ObjectType.shaft,true);
            return DpUtils.successJsonMsg("");
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return DpUtils.errorJsonMsg(ex.getMessage());
        }
    }
}
