package com.ilya.sergeev.potlach;

import java.io.IOException;
import java.security.Principal;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import retrofit.http.Multipart;
import retrofit.http.Streaming;

import com.google.common.collect.Lists;
import com.ilya.sergeev.potlach.client.Gift;
import com.ilya.sergeev.potlach.client.GiftSvcApi;
import com.ilya.sergeev.potlach.client.GiftsFileManager;
import com.ilya.sergeev.potlach.client.ImageStatus;
import com.ilya.sergeev.potlach.client.ImageStatus.ImageState;
import com.ilya.sergeev.potlach.repository.GiftRepository;
import com.ilya.sergeev.potlach.repository.UserInfoRepository;

//TODO make paging in future

@Controller
public class GiftController
{
	@Autowired
	UserInfoRepository mUserRepository;
	
	@Autowired
	GiftRepository mGiftRepository;
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.ALL_GIFT_PATH, method = RequestMethod.GET)
	public @ResponseBody
	Collection<Gift> getAllGifts()
	{
		return Lists.newArrayList(mGiftRepository.findAll());
	}
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.MY_GIFT_PATH, method = RequestMethod.GET)
	public @ResponseBody
	Collection<Gift> getMyGifts(Principal principal)
	{
		return Lists.newArrayList(mGiftRepository.findByUserName(principal.getName()));
	}
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.GIFT_PATH, method = RequestMethod.GET)
	public @ResponseBody
	Collection<Gift> getGiftsByUserName(@RequestParam(GiftSvcApi.USER_PARAM) String userName)
	{
		return Lists.newArrayList(mGiftRepository.findByUserName(userName));
	}
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.SEARCH_GIFT_PATH, method = RequestMethod.GET)
	public @ResponseBody
	Collection<Gift> searchGifts(@RequestParam(GiftSvcApi.TAG_PARAM) String tag)
	{
		return Lists.newArrayList(mGiftRepository.findByTitleContainingIgnoreCase(tag));
	}
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.SINGLE_GIFT_PATH, method = RequestMethod.GET)
	public @ResponseBody
	Gift getGift(@PathVariable(GiftSvcApi.ID_PARAM) long giftId)
	{
		return mGiftRepository.findOne(giftId);
	}
	
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.CREATE_GIFT_PATH, method = RequestMethod.POST)
	public @ResponseBody
	Gift createGift(@RequestBody Gift gift, Principal principal)
	{
		if (gift.getId() > 0)
		{
			throw new IllegalArgumentException();
		}
		gift.setUserName(principal.getName());
		gift = mGiftRepository.save(gift);
		if (gift.getId() > 0)
		{
			return mGiftRepository.save(gift);
		}
		return null;
	}
	
	@PreAuthorize("hasRole('ADMIN')")
	@RequestMapping(value = GiftSvcApi.SAVE_GIFT_PATH, method = RequestMethod.POST)
	public @ResponseBody
	Gift saveGift(@RequestBody Gift gift, Principal principal)
	{
		if (gift.getId() == 0)
		{
			throw new IllegalArgumentException();
		}
		
		return mGiftRepository.save(gift);
	}
	
	@Multipart
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.GIFT_DATA_PATH, method = RequestMethod.POST)
	public @ResponseBody
	ImageStatus setImageData(@PathVariable(GiftSvcApi.ID_PARAM) long giftId, @RequestParam(GiftSvcApi.DATA_PARAMETER) MultipartFile giftData)
	{
		Gift gift = mGiftRepository.findOne(giftId);
		if (gift != null)
		{
			try
			{
				GiftsFileManager.get().saveGiftData(gift, giftData.getInputStream());
				return new ImageStatus(ImageState.READY);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}
	
	@Streaming
	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = GiftSvcApi.GIFT_DATA_PATH, method = RequestMethod.GET)
	public void getData(@PathVariable(GiftSvcApi.ID_PARAM) long giftId, HttpServletResponse response)
	{
		Gift gift = mGiftRepository.findOne(giftId);
		if (gift == null)
		{
			throw new ResourceNotFoundException();
		}
		try
		{
			GiftsFileManager giftFileManager = GiftsFileManager.get();
			if (giftFileManager.hasGiftData(gift))
			{
				giftFileManager.copyGiftData(gift, response.getOutputStream());
				response.setHeader("MIME", "image/jpeg");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new ResourceNotFoundException();
		}
	}
}

@@ -116,16 +116,69 @@
 lection;
+%0Aimport java.util.Collections;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1738,34 +1738,23 @@
 %09return 
-Lists.newArrayList
+reverse
 (mGiftRe
@@ -1975,34 +1975,23 @@
 %09return 
-Lists.newArrayList
+reverse
 (mGiftRe
@@ -2276,34 +2276,23 @@
 %09return 
-Lists.newArrayList
+reverse
 (mGiftRe
@@ -2564,26 +2564,15 @@
 urn 
-Lists.newArrayList
+reverse
 (mGi
@@ -4911,13 +4911,177 @@
 ();%0A
-
 %09%09%7D%0A%09%7D%0A
+%09%0A%09private List%3CGift%3E reverse(Iterable%3CGift%3E gifts)%0A%09%7B%0A%09%09List%3CGift%3E giftsList = Lists.newArrayList(gifts);%0A%09%09Collections.reverse(giftsList);%0A%09%09return giftsList;%0A%09%7D%0A
 %7D%0A

change sort in gifts controller