package es.uv.saic.web;

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

import jakarta.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import es.uv.saic.domain.Noticia;
import es.uv.saic.service.NoticiaService;


@Controller
public class LoginController {
	
	@Autowired
	private NoticiaService serv;
	@Autowired
	private ProceduresController procedures;
	
	@GetMapping("/login")
	public String get(Model model, Authentication auth, HttpSession session, @RequestParam(required = false) String error) throws IOException {
		if(auth != null) {
			return procedures.getActiveInstances(model, auth, session, null);
		}
		else {
			if(error != null) {
		        if(error.equals("expired")) {
		        	model.addAttribute("expired", true);
		        }		        
		        else {
		        	model.addAttribute("error", true);
		        }
			}
					
			model.addAttribute("notices", null);
			List<Noticia> l = this.serv.findVisibles();	
			if(l != null) {
				if(!l.isEmpty()) {
					model.addAttribute("notices", l);
				}
			}
			
			return "login";
		}
		
	}
}
