package com.ctrip.zeus.restful.resource;

import com.ctrip.zeus.exceptions.ValidationException;
import com.ctrip.zeus.restful.message.ResponseHandler;
import com.ctrip.zeus.tag.PropertyBox;
import com.ctrip.zeus.tag.PropertyService;
import com.ctrip.zeus.tag.entity.Property;
import com.ctrip.zeus.tag.entity.PropertyList;
import com.google.common.base.Joiner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Set;

/**
 * Created by zhoumy on 2015/7/20.
 */
@Component
@Path("/")
public class PropertyResource {
    @Resource
    private PropertyBox propertyBox;
    @Resource
    private PropertyService propertyService;
    @Resource
    private ResponseHandler responseHandler;

    @GET
    @Path("/properties")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response listProperties(@Context HttpHeaders hh,
                                   @Context HttpServletRequest request,
                                   @QueryParam("type") String type,
                                   @QueryParam("targetId") Long targetId) throws Exception {
        List<Property> list = null;
        if (type != null) {
            if (targetId != null) {
                list = propertyService.getProperties(type, targetId);
            } else {
                Set<Long> propIds = propertyService.queryByType(type);
                list = propertyService.getProperties(propIds.toArray(new Long[propIds.size()]));
            }
        }

        if (list == null) {
            list = propertyService.getAllProperties();
        }

        PropertyList propertyList = new PropertyList().setTotal(list.size());
        for (Property property : list) {
            propertyList.addProperty(property);
        }
        return responseHandler.handle(propertyList, hh.getMediaType());
    }

    @GET
    @Path("/property/set")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response addPropertyItem(@Context HttpHeaders hh,
                                    @Context HttpServletRequest request,
                                    @QueryParam("pname") String pname,
                                    @QueryParam("pvalue") String pvalue,
                                    @QueryParam("type") String type,
                                    @QueryParam("targetId") List<Long> targetIds) throws Exception {
        if (pname == null || pvalue == null || type == null || targetIds == null)
            throw new ValidationException("At least one parameter is missing.");
        propertyBox.set(pname, pvalue, type, targetIds.toArray(new Long[targetIds.size()]));
        return responseHandler.handle(Joiner.on(",").join(targetIds) + " is/are added to property " + pname + "/" + pvalue + ".", hh.getMediaType());
    }

    @GET
    @Path("/property/clear")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response deletePropertyItem(@Context HttpHeaders hh,
                                       @Context HttpServletRequest request,
                                       @QueryParam("pname") String pname,
                                       @QueryParam("pvalue") String pvalue,
                                       @QueryParam("type") String type,
                                       @QueryParam("targetId") List<Long> targetIds) throws Exception {
        if ((pname == null && pvalue == null) || type == null || targetIds == null)
            throw new ValidationException("At least one parameter is missing.");
        propertyBox.clear(pname, pvalue, type, targetIds.toArray(new Long[targetIds.size()]));
        return responseHandler.handle(Joiner.on(",").join(targetIds) + " is/are deleted from property " + pname + "/" + pvalue + ".", hh.getMediaType());
    }

    @GET
    @Path("/property/delete")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response deleteProperty(@Context HttpHeaders hh,
                                   @Context HttpServletRequest request,
                                   @QueryParam("pname") String pname,
                                   @QueryParam("force") Boolean force) throws Exception {
        if (pname == null) throw new ValidationException("Parameter pname is required.");
        boolean forceEnabled = force != null && force;
        propertyBox.removeProperty(pname, forceEnabled);
        return responseHandler.handle("Successfully deleted property " + pname + ".", hh.getMediaType());
    }

    @GET
    @Path(("/property/clear"))
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response clearProperty(@Context HttpHeaders hh,
                                  @Context HttpServletRequest request,
                                  @QueryParam("targetId") Long targetId,
                                  @QueryParam("type") String type) throws Exception {
        if (targetId == null || type == null) {
            throw new ValidationException("Query Param targetId and type is required.");
        }
        propertyBox.clear(type, targetId);
        return responseHandler.handle("Successfully clear property from " + type + " " + targetId + ".", hh.getMediaType());
    }
}
@@ -3736,70 +3736,495 @@
 if (
-(pname == null && pvalue == null) %7C%7C type == null %7C%7C targetIds
+targetIds == null && type == null) %7B%0A            throw new ValidationException(%22Query parameter targetId and type are required.%22);%0A        %7D%0A%0A        if (pname == null && pvalue == null) %7B%0A            for (Long tId : targetIds) %7B%0A                propertyBox.clear(type, tId);%0A            %7D%0A            return responseHandler.handle(%22Successfully clear property from %22 + type + %22 %22 + Joiner.on(%22,%22).join(targetIds) + %22.%22, hh.getMediaType());%0A        %7D%0A%0A        if (pname == null %7C%7C pvalue
  == 
@@ -4220,32 +4220,34 @@
  pvalue == null)
+ %7B
 %0A            thr
@@ -4278,45 +4278,55 @@
 on(%22
-At least one parameter is missing.%22);
+Both pname and pvalue is required.%22);%0A        %7D
 %0A   
@@ -5297,728 +5297,6 @@
    %7D
-%0A%0A    @GET%0A    @Path((%22/property/clear%22))%0A    @Produces(%7BMediaType.APPLICATION_JSON, MediaType.APPLICATION_XML%7D)%0A    public Response clearProperty(@Context HttpHeaders hh,%0A                                  @Context HttpServletRequest request,%0A                                  @QueryParam(%22targetId%22) Long targetId,%0A                                  @QueryParam(%22type%22) String type) throws Exception %7B%0A        if (targetId == null %7C%7C type == null) %7B%0A            throw new ValidationException(%22Query Param targetId and type is required.%22);%0A        %7D%0A        propertyBox.clear(type, targetId);%0A        return responseHandler.handle(%22Successfully clear property from %22 + type + %22 %22 + targetId + %22.%22, hh.getMediaType());%0A    %7D
 %0A%7D

fix duplicate path in property resource