package com.taxtelecom.chelnyedu.dropwizard.resources;

import javax.validation.ConstraintViolation;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.taxtelecom.chelnyedu.dropwizard.dao.ContactDAO;
import com.taxtelecom.chelnyedu.dropwizard.representations.Contact;
import javax.validation.Validator;


@Path("/contact")
@Produces(MediaType.APPLICATION_JSON)
public class ContactResources {

    private final ContactDAO contactDAO;
    private final Validator validator;

    public ContactResources(ContactDAO dao, Validator validator){
        contactDAO = dao;
        this.validator = validator;
    }

    @GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllContact(){
        List<Contact> list=contactDAO.getAllContact();
        return Response.ok(list).build();
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getContact(@PathParam("id") int id){
        Contact contact = contactDAO.getContactById(id);
        return Response.ok(contact).build();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response createContact(Contact contact)
    		throws URISyntaxException{
        Set<ConstraintViolation<Contact>> violations = validator.validate(contact);

        if (!violations.isEmpty()) {
            ArrayList<String> validationMessages = new ArrayList<>();
            for (ConstraintViolation<Contact> violation : violations) {
                validationMessages.add(violation.getPropertyPath().toString() +": " + violation.getMessage());
            }
            return Response
                    .status(Response.Status.BAD_REQUEST)
                    .entity(validationMessages)
                    .build();
        }
        else {
            int newContactId = contactDAO.createContact(contact.getFirstName(),
                    contact.getLastName(), contact.getPhone(),
                    contact.getMail(), contact.getComment());
            return Response.created(new
                    URI(String.valueOf(newContactId))).build();
        }
    }


    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteContact(@PathParam("id") int id){
        contactDAO.deleteContact(id);
        return Response.noContent().build();
    }

    @PUT
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response updateContact(@PathParam("id") int id, Contact contact){
        Set<ConstraintViolation<Contact>> violations = validator.validate(contact);

        if (!violations.isEmpty()) {
            ArrayList<String> validationMessages = new ArrayList<>();
            for (ConstraintViolation<Contact> violation : violations) {
                validationMessages.add(violation.getPropertyPath().toString() +": "
                        + violation.getMessage());
            }
            return Response
                    .status(Response.Status.BAD_REQUEST)
                    .entity(validationMessages)
                    .build();
        }
        else {
            contactDAO.updateContact(id, contact.getFirstName(),
                    contact.getLastName(), contact.getPhone(),
                    contact.getMail(), contact.getComment());
            return Response.ok(
                    new Contact(id, contact.getFirstName(),
                            contact.getLastName(), contact.getPhone(),
                            contact.getMail(), contact.getComment())).build();
        }

    }

}

@@ -774,50 +774,8 @@
 l%22)%0A
-    @Produces(MediaType.APPLICATION_JSON)%0A
     
@@ -807,16 +807,16 @@
 tact()%7B%0A
+
         
@@ -943,50 +943,8 @@
 %7D%22)%0A
-    @Produces(MediaType.APPLICATION_JSON)%0A
     
@@ -1101,24 +1101,24 @@
 d();%0A    %7D%0A%0A
+
     @POST%0A  
@@ -1119,50 +1119,8 @@
 OST%0A
-    @Produces(MediaType.APPLICATION_JSON)%0A
     
@@ -2138,50 +2138,8 @@
 %7D%22)%0A
-    @Produces(MediaType.APPLICATION_JSON)%0A
     
@@ -2316,50 +2316,8 @@
 %7D%22)%0A
-    @Produces(MediaType.APPLICATION_JSON)%0A
     

delete some produces(2)