package es.uv.saic.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import es.uv.saic.shared.domain.Usuari;
import es.uv.saic.shared.dto.OrganDTO;
import es.uv.saic.shared.feign.OrganClient;
import jakarta.servlet.http.HttpSession;


@Controller
public class OrganController {

	@Autowired
	private OrganClient oc;

	// POST que buscar titulaciones que se encuentren asociadas a uno o varios
	// centros
	@PostMapping("/search/titulations")
	public String getTitulationsByCenter(Model model, Authentication auth,
			@RequestParam(name = "centers[]", required = false) List<Integer> centres) throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenter(centres,
				((Usuari) auth.getPrincipal()).getUsuari());

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/search/managers/titulations")
	public String getCenterTitulations(Model model, Authentication auth,
			@RequestParam("center") Integer centre,
			HttpSession session) {

		HashMap<String, Object> response = oc.getCenterTitulations(centre);

		if (response == null) {
			return "401";
		}

		List<Object> rawList = (List<Object>) response.get("resp_titulacions");
		ObjectMapper mapper = new ObjectMapper();

		List<OrganDTO> titulacions = rawList.stream()
				.map(obj -> mapper.convertValue(obj, OrganDTO.class))
				.collect(Collectors.toList());

		response.remove("resp_titulacions");

		model.addAllAttributes(response);
		model.addAttribute("sup_titulacions", titulacions);

		return "components/selector_titulacions_managers";
	}

	@PostMapping("/find/titulations2")
	public String getTitulationsByCenters(Model model, Authentication auth,
			@RequestParam("centers[]") List<Integer> centres, @RequestParam("procedure") Integer idProces)
			throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenters(centres, idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions_managers";
	}

	@PostMapping("/find/titulations")
	public String getTitulationsByCenter(Model model, Authentication auth,
			@RequestParam("center") Integer centre, @RequestParam("procedure") Integer idProces) throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenter(centre, idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/get/titulations")
	public String getAllTitulationsByCenter(Model model, Authentication auth, @RequestParam("center") Integer centre)
			throws IOException {
		HashMap<String, Object> response = oc.getAllTitulationsByCenter(centre);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/find/centers")
	public String getAllCentresByAmbit(Model model, Authentication auth, @RequestParam("procedure") Integer idProces)
			throws IOException {
		HashMap<String, Object> response = oc.getAllCentresByAmbit(idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_centres";
	}

	@PutMapping("/organ/centre")
	@ResponseBody
	public void createNewCentre(Model model, Authentication auth,
			@RequestParam("codiCentro") Integer codigo,
            @RequestParam("nomCasCentro") String nomCas,
            @RequestParam("nomValCentro") String nomVal,
            @RequestParam("ructCentro") Integer ruct) {
		oc.createNewCentre(codigo, nomCas, nomVal, ruct);
	}

	@PutMapping("/organ/titulacion")	
	@ResponseBody
	public void createNewTitulacion(Model model, Authentication auth,
			@RequestParam("codiTit") Integer codigo,
            @RequestParam("centre") Integer idCentro,
            @RequestParam("nomCasTit") String nomCas,
            @RequestParam("nomValTit") String nomVal,
            @RequestParam("ructTit") Integer ruct,
            @RequestParam("tambit") String ambit) {
		oc.createNewTitulacion(codigo ,idCentro, nomCas, nomVal, ruct, ambit);
	}

	@PostMapping("/organ/centre")
	@ResponseBody
	public Integer updateCentre(Model model, Authentication auth, 
			@RequestParam("lugar") Integer lugar,
			@RequestParam("tlugar") String tlugar,
			@RequestParam("nomCasTit") String nomCas,
			@RequestParam("nomValTit") String nomVal,
			@RequestParam("ructTit") Integer ruct) {
		oc.updateCentre(lugar, tlugar, nomCas, nomVal, ruct);

		return ruct;
	}
	
	@PostMapping("/organ/titulacion")
	@ResponseBody
	public Integer updateTitulacion(Model model, Authentication auth, 
			@RequestParam("lugar") Integer lugar,
			@RequestParam("tlugar") String tlugar,
			@RequestParam("nomCasTit") String nomCas,
			@RequestParam("nomValTit") String nomVal,
			@RequestParam("ructTit") Integer ruct,
		    @RequestParam("centre") Integer idCentro,
			@RequestParam("tambit") String ambit) {
		oc.updateTitulacion(lugar, tlugar, nomCas, nomVal, ruct, idCentro, ambit);

		return ruct;
	}
}
