package es.uv.saic.web;

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

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.RequestParam;

import es.uv.saic.domain.DummyDataTransfer;
import es.uv.saic.domain.Organ;
import es.uv.saic.domain.Proces;
import es.uv.saic.domain.Usuari;
import es.uv.saic.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("/ajax/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("/ajax/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";	
        }

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

	@PostMapping("/ajax/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("/ajax/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("/ajax/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("/ajax/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";
	}
}
