package ru.ulstu.grant.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.service.GrantService;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.util.StringUtils.isEmpty;


@Controller()
@RequestMapping(value = "/grants")
public class GrantController {
    private final GrantService grantService;

    public GrantController(GrantService grantService) {
        this.grantService = grantService;
    }

    @GetMapping("/grants")
    public void getGrants(ModelMap modelMap) {
        modelMap.put("grants", grantService.findAllDto());
    }

    @GetMapping("/dashboard")
    public void getDashboard(ModelMap modelMap) {
        modelMap.put("grants", grantService.findAllDto());
    }

    @GetMapping("/grant")
    public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
        if (id != null && id > 0) {
            modelMap.put("grantDto", grantService.findOneDto(id));
        } else {
            modelMap.put("grantDto", new GrantDto());
        }
    }

    @PostMapping(value = "/grant", params = "save")
    public String save(@Valid GrantDto grantDto, Errors errors) throws IOException {
        filterEmptyDeadlines(grantDto);
        if (grantDto.getDeadlines().isEmpty()) {
            errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
        }
        if (errors.hasErrors()) {
            return "/grants/grant";
        }
        grantService.save(grantDto);
        return "redirect:/grants/grants";
    }

    @PostMapping(value = "/grant", params = "addDeadline")
    public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
        filterEmptyDeadlines(grantDto);
        if (errors.hasErrors()) {
            return "/grants/grant";
        }
        grantDto.getDeadlines().add(new DeadlineDto());
        return "/grants/grant";
    }

    @PostMapping(value = "/grant", params = "createProject")
    public String createProject(@Valid GrantDto grantDto, Errors errors) {
        if (errors.hasErrors()) {
            return "/grants/grant";
        }
        grantService.createProject(grantDto);
        return "/grants/grant";
    }

    @GetMapping("/delete/{grant-id}")
    public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
        grantService.delete(grantId);
        return "redirect:/grants/grants";
    }

    @ModelAttribute("allStatuses")
    public List<Grant.GrantStatus> getGrantStatuses() {
        return grantService.getGrantStatuses();
    }

    private void filterEmptyDeadlines(GrantDto grantDto) {
        grantDto.setDeadlines(grantDto.getDeadlines().stream()
                .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
                .collect(Collectors.toList()));
    }
}
