package es.uv.garcosda.controllers;

import es.uv.garcosda.domain.Meeting;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@Validated
public class MeetupController {
	
	DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

	List<Meeting> meetings = new ArrayList<Meeting>(
        Arrays.asList(new Meeting("First meeting", 
        						  LocalDateTime.parse("10/02/2023 10:00", format)),
        			  new Meeting("Second meeting", 
        					      LocalDateTime.parse("12/02/2023 16:30", format)),
        			  new Meeting("Fourth meeting", 
        					  	  LocalDateTime.parse("15/02/2023 13:00", format)),
        			  new Meeting("Fourth meeting", 
        					  	  LocalDateTime.parse("01/03/2023 11:45", format))));

	@GetMapping("/")
	public String getMeetups(Model model) {
		model.addAttribute("meetings", this.meetings);
		return "index";
	}
	
	@PostMapping("/")
	public RedirectView addMeeting(Model model, @RequestParam("subject") @NotNull @NotEmpty String subject, 
								   @RequestParam("date") @NotNull @NotEmpty @DateTimeFormat(pattern="dd/MM/yyyy hh:MM") String date) {
		
		meetings.add(new Meeting(subject, LocalDateTime.parse(date, this.format)));
		model.addAttribute("meetings", this.meetings);
		return new RedirectView("/");
	}
	
	@GetMapping("/remove/{id}")
	public RedirectView removeMeeting(Model model, @PathVariable("id") String id) {
		this.meetings = this.meetings.stream().filter(x -> !x.getId().equals(id)).collect(Collectors.toList());
		return new RedirectView("/");
	}
	
	@ExceptionHandler(ConstraintViolationException.class)
	@ResponseBody
	public ResponseEntity<String> validationError(ConstraintViolationException ex) {
		Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
		String msg = "";
		for(ConstraintViolation<?> v : violations) {
			msg += v.getPropertyPath().toString() + " : " + v.getMessage() + ",  "; 
		}
	    return new ResponseEntity<String>(msg, HttpStatus.BAD_REQUEST);
	}
		
}
