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.GameClientService;
import premium_store.service.TankService;

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

@Controller
@RequestMapping("/client")
public class ClientMvcController {
    private final GameClientService gameClientService;
    private final TankService tankService;

    public ClientMvcController(GameClientService gameClientService, TankService tankService){
        this.gameClientService = gameClientService;
        this.tankService = tankService;
    }

    @GetMapping
    public String getClients(Model model){
        model.addAttribute("clients",
                gameClientService.findAllClients().stream()
                        .map(ClientDTO::new)
                        .toList());

        return "client";
    }

    @GetMapping(value = {"/edit", "/edit/{id}"})
    public String editClient(@PathVariable(required = false) Long id, Model model){
        if(id == null || id <= 0){
            model.addAttribute("supportClientDTO", new SupportClientDTO());
        }
        else {
            model.addAttribute("clientId", id);
            model.addAttribute("supportClientDTO", new SupportClientDTO(gameClientService.findClient(id)));
        }

        List<TankDTO> tanks = tankService.findAllTanks().stream()
                .map(TankDTO::new)
                .toList();

        model.addAttribute("tanks", tanks);

        return "client-edit";
    }

    @GetMapping(value = {"/tanksOfClient", "/tanksOfClient/{id}"})
    public String editTanksOfClient(@PathVariable(required = false) Long id, Model model){
        if(id == null || id <= 0){
            model.addAttribute("clientDTO", new ClientDTO());
        }
        else {
            model.addAttribute("clientId", id);
            model.addAttribute("clientDTO", new ClientDTO(gameClientService.findClient(id)));
        }

        return "tanks-of-client-edit";
    }

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

        if(id == null || id <= 0){
            gameClientService.addClient(clientDTO.getNickName(), clientDTO.getNickName(), clientDTO.getBalance());
        } else {
            gameClientService.updateClient(clientDTO);
        }

        return "redirect:/client";
    }

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