package premium_store.controller.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import premium_store.controller.DTO.*;
import premium_store.service.NationService;
import premium_store.service.TankLevelService;
import premium_store.service.TankService;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/tank")
public class TankMvcController {
    private final TankService tankService;
    private final NationService nationService;
    private final TankLevelService tankLevelService;

    public TankMvcController(TankService tankService, NationService nationService, TankLevelService tankLevelService){
        this.tankService = tankService;
        this.nationService = nationService;
        this.tankLevelService = tankLevelService;
    }

    @GetMapping
    public String getTanks(Model model){
        model.addAttribute("tanks",
                tankService.findAllTanks().stream()
                        .map(TankDTO::new)
                        .toList());

        List<SimpleNationDTO> nations = nationService.findAllNations().stream()
                .map(SimpleNationDTO::new)
                .toList();

        model.addAttribute("nations", nations);

        List<LevelDTO> levels = tankLevelService.findAllLevels().stream()
                .map(LevelDTO::new)
                .toList();

        model.addAttribute("levels", levels);

        model.addAttribute("sortDTO", new SortDTO());

        return "tank";
    }

    @GetMapping(value = {"/edit", "/edit/{id}"})
    public String editTank(@PathVariable(required = false) Long id, Model model){
        if(id == null || id <= 0){
            model.addAttribute("supportTankDTO", new SupportTankDTO());
        }
        else {
            model.addAttribute("tankId", id);
            model.addAttribute("supportTankDTO", new SupportTankDTO(tankService.findTank(id)));
        }

        List<SimpleNationDTO> nations = nationService.findAllNations().stream()
                .map(SimpleNationDTO::new)
                .toList();

        model.addAttribute("nations", nations);

        List<LevelDTO> levels = tankLevelService.findAllLevels().stream()
                .map(LevelDTO::new)
                .toList();

        model.addAttribute("levels", levels);

        return "tank-edit";
    }

    @GetMapping("/filteredList")
    public String getFilteredTanks(@ModelAttribute SortDTO sortDTO,
                                   Model model) {
        List<TankDTO> tanks = tankService.findListTank(sortDTO.getNation(), sortDTO.getFirstLevel(), sortDTO.getSecondLevel()).stream()
                .map(TankDTO::new)
                .toList();

        model.addAttribute("tanks", tanks);

        return "filter";
    }

    @PostMapping(value = {"", "/{id}"})
    public String saveTank(@PathVariable(required = false) Long id,
                             @ModelAttribute @Valid SupportTankDTO tankDTO,
                             BindingResult bindingResult,
                             Model model){
        if(bindingResult.hasErrors()){
            model.addAttribute("errors", bindingResult.getAllErrors());
            return "tank-edit";
        }

        if(id == null || id <= 0){
            tankService.addTank(tankDTO);
        } else {
            tankService.updateTank(tankDTO);
        }

        return "redirect:/tank";
    }

    @PostMapping("/delete/{id}")
    public String deleteTank(@PathVariable Long id){
        tankService.deleteTank(id);
        return "redirect:/tank";
    }
}