package pl.dmcs.mati.controller;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import pl.dmcs.mati.domain.AppCar;
import pl.dmcs.mati.service.AppCarServiceImpl;
import pl.dmcs.mati.service.AppCarService;
import pl.dmcs.mati.validator.AppCarValidator;

@Controller
public class AppCarController {
    private AppCarValidator appCarValidator = new AppCarValidator();

    private AppCarService appCarService;
    @Autowired
    public AppCarController(AppCarService appCarService) {this.appCarService = appCarService; }
    @RequestMapping(value = "/appCars")
    public String showAppCars(Model model, HttpServletRequest request) {
        int appCarId = ServletRequestUtils.getIntParameter(request, "appCarId", -1);
        if (appCarId > 0) {
            model.addAttribute("appCar", appCarService.getAppCar(appCarId));
        } else
            model.addAttribute("appCar", new AppCar());
        model.addAttribute("appCarList", appCarService.listAppCar());
        return "appCar";
    }

    @RequestMapping("/appCarList")
    public String showAppCarLi5st(Model model) {
        model.addAttribute("appCarList", appCarService.listAppCar());
        return "appCarList";
    }

    @RequestMapping(value = "/addAppCar", method = RequestMethod.POST)
    public String addAppCar(@Valid @ModelAttribute("appCar") AppCar appCar, BindingResult result, Model model) {
        System.out.println("Brand : " + appCar.getBrandName() +
                " Model: " + appCar.getModelName() +
                " Body Type: " + appCar.getBodyType() +
                " Year of production: " + appCar.getProductionYear() +
                " Color: " + appCar.getCarColor() +
                " Fuel Type: " + appCar.getFuelType() +
                " Engine size: " + appCar.getEngineSize() +
                " Mileage: " + appCar.getMileage() +
                " Power: " + appCar.getPower() +
                " Vin: " + appCar.getVin() +
                " Price: " + appCar.getPrice() +
                " Picture: " + appCar.getPicture());

        appCarValidator.validate(appCar, result);

        if (result.getErrorCount() == 0) {


            if (appCar.getId() == 0)
                appCarService.addAppCar(appCar);
            else
                appCarService.editAppCar(appCar);

            return "redirect:appCars";
        }
        model.addAttribute("appCarList", appCarService.listAppCar());
        return "appCar";
    }

    @RequestMapping("/delete2/{appCarId}")
    public String deleteCar(@PathVariable("appCarId") Long appCarId) {
        appCarService.removeAppCar(appCarId);
        return "redirect:/appCars";
    }
}
