package com.dfyy.resource;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dfyy.bussiness.Contact;
import com.dfyy.service.ContactService;
import com.dfyy.service.TokenService;
import com.dfyy.util.TokenHelper;

@Component
@Path("contacts")
public class ContactsResource {

	@Autowired
	private ContactService contactService;

	@Autowired
	private TokenService tokenService;

	/**
	 * 获取某用户的所有联系人
	 * 
	 * @param uid
	 * @return
	 */
	@GET
	@Path("/{uid}")
	@Produces("application/json;charset=UTF-8")
	public Response getByUid(@PathParam("uid") String uid, @HeaderParam("X-Token") String token) {
		TokenHelper.verifyToken(tokenService, uid, token);

		try {
			List<Contact> contacts = contactService.getByUser(uid);
			return Response.status(Status.OK).entity(contacts).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN)
					.build();
		}

	}

	/**
	 * 创建联系人
	 * 
	 * @param uid
	 * @param name
	 * @param phone
	 * @param address
	 * @param postnumber
	 * @return
	 */
	@POST
	@Consumes("application/x-www-form-urlencoded")
	public Response create(@FormParam("uid") String uid, @FormParam("name") String name,
			@FormParam("phone") String phone, @FormParam("address") String address,
			@FormParam("postnumber") String postnumber, @HeaderParam("X-Token") String token) {

		TokenHelper.verifyToken(tokenService, uid, token);

		if (StringUtils.isBlank(uid) || StringUtils.isBlank(name) || StringUtils.isBlank(phone)
				|| StringUtils.isBlank(address) || StringUtils.isBlank(postnumber)) {
			return Response.status(Response.Status.NOT_FOUND).entity("缺少参数").type(MediaType.TEXT_PLAIN).build();
		}

		try {
			Contact contact = new Contact();
			contact.setAddress(address);
			contact.setName(name);
			contact.setPhone(phone);
			contact.setPostnumber(postnumber);
			contact.setStatus(0);
			contact.setUid(uid);
			contactService.saveOrUpdate(contact);
			return Response.status(Status.OK).entity(contact).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN)
					.build();
		}

	}

	/**
	 * 更新联系人
	 * 
	 * @param id
	 * @param name
	 * @param phone
	 * @param address
	 * @param postnumber
	 * @return
	 */
	@POST
	@Path("/{id}")
	@Consumes("application/x-www-form-urlencoded")
	public Response update(@PathParam("id") int id, @FormParam("name") String name, @FormParam("phone") String phone,
			@FormParam("address") String address, @FormParam("postnumber") String postnumber,
			@HeaderParam("X-Token") String token) {

		if (StringUtils.isBlank(name) || StringUtils.isBlank(phone) || StringUtils.isBlank(address)
				|| StringUtils.isBlank(postnumber)) {
			return Response.status(Response.Status.NOT_FOUND).entity("缺少参数").type(MediaType.TEXT_PLAIN).build();
		}

		Contact contact = contactService.getByid(id);

		TokenHelper.verifyToken(tokenService, contact.getUid(), token);

		try {

			contact.setName(name);
			contact.setPhone(phone);
			contact.setAddress(address);
			contact.setPostnumber(postnumber);
			contactService.saveOrUpdate(contact);
			return Response.status(Status.OK).entity(contact).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN)
					.build();
		}
	}

	/**
	 * 删除联系人
	 * 
	 * @param id
	 * @return
	 */
	@DELETE
	@Path("/{id}")
	public Response delete(@PathParam("id") int id, @HeaderParam("X-Token") String token) {
		Contact contact = contactService.getByid(id);
		TokenHelper.verifyToken(tokenService, contact.getUid(), token);

		try {
			contactService.deleteContact(id);
			return Response.status(Status.OK).build();
		} catch (Exception e) {
			return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN)
					.build();
		}
	}

}
