package org.artgasmator.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.artgasmator.model.Artist;
import org.artgasmator.model.ArtistLabel;
import org.artgasmator.model.Member;
import org.artgasmator.service.ArtistLabelService;
import org.artgasmator.service.ArtistService;
import org.artgasmator.service.MemberService;
import org.artgasmator.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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;

@Controller
public class ArtistController {
	
	@Autowired
	private ArtistService artistService;
	
	@Autowired
	private ArtistLabelService artistLabelService;
	
	@Autowired
	private MemberService memberService;
	
	
	@RequestMapping(value="/artists/labels", method=RequestMethod.GET)
	public @ResponseBody List<ArtistLabel> loadLabels() {
		
		return artistLabelService.listLabels();
	}
	
	@RequestMapping(value="/artists/create", method=RequestMethod.GET)
	public String createArtist(Model model) {
		model.addAttribute("artist", new Artist());
		model.addAttribute("labels", artistLabelService.listLabels());
		return "createArtist";
	}
	
	@RequestMapping(value="/artists/members/add/{artistId}", method=RequestMethod.GET)
	public String addMember(@PathVariable("artistId") Integer artistId, Model model) {
		
		List<Member> members = memberService.loadMembers(artistId);
		model.addAttribute("members", members);
		model.addAttribute("member", new Member());
		model.addAttribute("artistId", artistId);
		
		return "addMember";
	}
	
	@RequestMapping(value="/artists/members/photo/{memberId}", method=RequestMethod.GET)
	public void renderPhoto(
			   HttpServletResponse response,
			   @PathVariable("memberId") Integer memberId) 
			   throws IOException {
		
		Member member = memberService.loadMember(memberId);
		
		if (member != null) {
			
			byte[] photo = member.getPhoto();
			
			if (photo != null && 
					photo.length > 0 ) {
				IOUtils.writeImageByteArrayToStream(response, photo);
			}
		}
	}
	
	@RequestMapping(value="/artists/members/create/{artistId}", method=RequestMethod.POST)
	public String createMember(@ModelAttribute("member") Member member, 
			@PathVariable("artistId") Integer artistId, @RequestParam("file")
			MultipartFile file) {
		
		if (!file.isEmpty()) {
			try {
				byte[] photo = file.getBytes();
				member.setPhoto(photo);
			} catch (IOException e) {
				
			}
		}
		
		member.setArtistId(artistId);
		memberService.createMember(member);
		
		return "redirect:/artists/members/add/" + artistId;
	}
	
	@RequestMapping(value="/artists/create", method=RequestMethod.POST)
	public String createArtist(@ModelAttribute("artist") Artist artist, BindingResult
			bindingResult, Model model) {
		 
		 Integer createdArtistId = artistService.createArtist(artist);
		 
		 return "redirect:/artists/members/add/" + createdArtistId;
	}
	
}
