package es.uv.saic.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Optional;

import jakarta.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import es.uv.saic.service.IndicadorEnquestaService;
import es.uv.saic.service.ParserService;

// Controller for the parsing options page for admin user
@Controller
public class ParseController {

	@Autowired
	private ParserService ps;
	@Autowired
	private IndicadorEnquestaService ies;
	@Value("${saic.parser.surveys.path}")
	private String surveysPath;
	
	/*
	 * Load the parsing options page data into the model
	 * @param model
	 * @param auth Authentication
	 * @param err Optional error message
	 * @param ok Optional success message
	 * @return The name of the view to render
	 */ 
	//TO DO
	@GetMapping("/admin/parse")
	@Secured({"ROLE_ADMIN"})
	public String getParse(Model model, Authentication auth, @RequestParam(required=false, name="err") Optional<String> err, 
			@RequestParam(required=false, name="ok") Optional<Integer> ok) throws IOException, SQLException, ClassNotFoundException {	
		
		if(ok.isPresent()) {
			model.addAttribute("ok", ok.get());
		}
		else if(err.isPresent()) {
			model.addAttribute("err", true);
		}
		
		return "parseOptions";
	}
	
	/*
	 * Parse a given table from a given database for a given year
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param database The database to parse from
	 * @param table The table to parse from
	 * @param year The year to parse from
	 */
	@PostMapping("/admin/parse/do")
	public void doParse(HttpServletResponse response, @RequestParam String database, 
			@RequestParam String table, @RequestParam Integer year) throws IOException, SQLException, ClassNotFoundException {		
		
		System.out.println(database + " -> " + table + " -> " + year.toString());
		String r = this.ps.doParse(database, table, year.toString());
		response.sendRedirect("/admin/parse?ok="+r);

	}
	
	/*
	 * Parse table depending on the action requested: mediasCentroNP, bienalesProfesGrado, etc
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param action The action to perform
	 */
	@PostMapping("/admin/parse/action")
	public void doAction(HttpServletResponse response, @RequestParam String action) 
			throws IOException, SQLException, ClassNotFoundException {		
		
		System.out.println(action);
		String r = this.ps.doAction(action);
		if(r.equals("-1")) {
			response.sendRedirect("/admin/parse?err");
		}
		else {
			response.sendRedirect("/admin/parse?ok="+r);
		}
	}
	
	/*
	 * Method to delete survey indicators from the database
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param action The survey to delete indicators from
	 * @param year The year of the survey to delete indicators from (0 for all years)
	 */
	@PostMapping("/admin/parse/delete")
	public void doDelete(HttpServletResponse response, @RequestParam String action, @RequestParam Integer year) 
			throws IOException, SQLException, ClassNotFoundException {		
		
		String r = "-1";
		if(year == 0) {
			r = Integer.toString(this.ies.deleteByEnquesta(action));
		}
		else {
			r = Integer.toString(this.ies.deleteByEnquestaCurs(action, year));
		}
		
		if(r.equals("-1")) {
			response.sendRedirect("/admin/parse?err");
		}
		else {
			response.sendRedirect("/admin/parse?ok="+r);
		}
	}
		
}
