package com.poseidon.controller;

import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.poseidon.annotation.NotNullArgs;
import com.poseidon.annotation.ViewName;
import com.poseidon.dao.*;
import com.poseidon.model.*;

@Controller
public class ContaController {

	@Autowired
	private AuthoritiesRepository authoritiesRepository;
	
	@Autowired
	private UserRepository  userRepository;
	
	@Autowired
	private DadoSessao dadoSessao;
	
    @RequestMapping("/conta")
    @ViewName(name = "Conta")
    public ModelAndView buildContaPage(ModelAndView modelAndView,@RequestParam("isCadastroConta") String isCadastroConta,@RequestParam("isEditarConta") String isEditarConta,@RequestParam("isDeleteConta") String isDeleteConta,@RequestParam("isExibirConta") String isExibirConta){
        modelAndView.getModelMap().addAttribute("isCadastroConta", isCadastroConta);
        modelAndView.getModelMap().addAttribute("isEditarConta", isEditarConta);
        modelAndView.getModelMap().addAttribute("isDeleteConta", isDeleteConta);
        modelAndView.getModelMap().addAttribute("isExibirConta", isExibirConta);
        exibirConta(modelAndView);
        return  modelAndView;
    }
    
	@RequestMapping(value="/cadastrarConta")
	@ViewName(name = "redirect:/conta?isCadastroConta=true&isEditarConta=false&isDeleteConta=false&isExibirConta=false")
	@NotNullArgs
	public ModelAndView cadastrarConta(@ModelAttribute ContaView contaView,@ModelAttribute String role, ModelAndView modelAndView){
		Users user = Users.createUser(contaView);
		//if(dadoSessao.getIdUsuario()!= null)user.setId(dadoSessao.getIdUsuario());
		System.out.println("CREATE CONTA");
		System.out.println("user>"+user.toString());
		userRepository.save(user);
/*		modelAndView.setViewName("redirect:/home");
		if(role!= null && !role.isEmpty()){
			
			contaView.setRole(role);
			modelAndView.setViewName("redirect:/home");
		}
		if(dadoSessao.getIdUsuario()== null)authoritiesRepository.save(new Authorities(user.getUsername(),new StringBuilder().append("ROLE_").append(contaView.getRole()).toString()));
		dadoSessao.setIdUsuario(null);*/
		System.out.println("cadastrou conta.");
		return modelAndView;
	}
	
	@RequestMapping(value="/exibirConta")
	@ViewName(name = "redirect:/conta?isCadastroConta=false&isEditarConta=false&isDeleteConta=false&isExibirConta=true")
	@NotNullArgs
    private void exibirConta(ModelAndView modelAndView) {
        Iterable<Users> contaList = userRepository.findAll();
        List<ContaView> contasView = Lists.newArrayList();
		for(Users user : contaList){
			Authorities authority = authoritiesRepository.findByUsername(user.getUsername());
			contasView.add(new ContaViewBuilder().convertUsersThroughContaView(user).withAuthority(authority.getAuthority()).build());
        }
        modelAndView.getModelMap().addAttribute("contas", contasView);
    }
    
	@RequestMapping(value="/deletarConta")
	@ViewName(name = "redirect:/conta?isCadastroConta=false&isEditarConta=false&isDeleteConta=true&isExibirConta=false")
	@NotNullArgs
	public ModelAndView deletarConta(@ModelAttribute ContaView contaView, ModelAndView modelAndView){
		Users user = userRepository.findByUsername(contaView.getUsername());
		Authorities authorities = authoritiesRepository.findByUsername(contaView.getUsername());
		System.out.println("Delete USER");
		System.out.println("user>"+user.toString());
		userRepository.delete(user);
		authoritiesRepository.delete(authorities);
		return modelAndView;
	}
	
	@RequestMapping("/editarConta")
	@ViewName(name = "redirect:/conta?isCadastroConta=true&isEditarConta=true&isDeleteConta=false&isExibirConta=false")
	@NotNullArgs
	public ModelAndView editarConta(ModelAndView modelAndView, @ModelAttribute ContaView contaView, RedirectAttributes redirectAttributes){
		Users user = userRepository.findByUsername(contaView.getUsername());
		if(user == null){
			redirectAttributes.addFlashAttribute("contaView",contaView);
			redirectAttributes.addFlashAttribute("isEditarError", "true");
			modelAndView.setViewName("redirect:/home");
			return modelAndView;
		}
		dadoSessao.setIdUsuario(user.getId());
		Authorities authorities = authoritiesRepository.findByUsername(contaView.getUsername());
		contaView.setPassword(user.getPassword());
		contaView.setUsername(user.getUsername());
		contaView.setRole(authorities.getAuthority().replace("ROLE_", ""));
		redirectAttributes.addFlashAttribute("isEditar", "isEditar");
		redirectAttributes.addFlashAttribute("contaView",contaView);
		modelAndView.setViewName("redirect:/home");
		return modelAndView;
	}
}

@@ -49,16 +49,49 @@
 il.List;
+%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -810,17 +810,79 @@
 Sessao;%0A
-%09
+%0A%09private Logger logger = Logger.getLogger(%22ContaController%22);%0A
 %0A    @Re
@@ -2161,179 +2161,12 @@
 r);%0A
-/*%09%09modelAndView.setViewName(%22redirect:/home%22);%0A%09%09if(role!= null && !role.isEmpty())%7B%0A%09%09%09%0A%09%09%09contaView.setRole(role);%0A%09%09%09modelAndView.setViewName(%22redirect:/home%22);%0A%09%09%7D%0A
 %09%09
+//
 if(d
@@ -2197,16 +2197,20 @@
 == null)
+%0A%09%09%09
 authorit
@@ -2343,16 +2343,18 @@
 ()));%0A%09%09
+//
 dadoSess
@@ -2379,10 +2379,8 @@
 ll);
-*/
 %0A%09%09S
@@ -2827,16 +2827,135 @@
 aList)%7B%0A
+%09%09%09if(user!= null && user.getUsername() != null && !user.getUsername().isEmpty()) %7B%0A%09%09%09%09logger.info(user.toString());%0A%09
 %09%09%09Autho
@@ -3031,16 +3031,17 @@
 ame());%0A
+%09
 %09%09%09conta
@@ -3158,24 +3158,23 @@
 ild());%0A
-        
+%09%09%09%7D%0A%09%09
 %7D%0A      

ajuste no add conta