package fr.eni.enchere.controllers;

import fr.eni.enchere.bll.ArticleService;
import fr.eni.enchere.bll.CategorieService;
import fr.eni.enchere.bll.EnchereService;
import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.Categorie;
import fr.eni.enchere.bo.Enchere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ArticleService articleService;
    private EnchereService enchereService;
    private UserService userService;
    private CategorieService categorieService;

    public AdminController(UserService userService, CategorieService categorieService, ArticleService articleService, EnchereService enchereService) {
        this.userService = userService;
        this.categorieService = categorieService;
        this.articleService = articleService;
        this.enchereService = enchereService;
    }

    @GetMapping
    public String viewAdminPanel(Model model) {
        model.addAttribute("categories", categorieService.findAllCategories());
        model.addAttribute("userProfil", userService.listeUtilisateurs());
        model.addAttribute("categorie", new Categorie());
        return "admin";
    }

    @PostMapping("/new")
    public String ajouterCategorie(@ModelAttribute("categorie") Categorie categorie) {
        categorieService.saveCategorie(categorie);
        return "redirect:/admin";
    }

    @PostMapping("/update")
    public String updateCategorie(@RequestParam("newCategorie") String libelle, @RequestParam("IdCategorie") int idCategorie) {
        Categorie categorie = new Categorie();
        categorie.setLibelle(libelle);
        categorie.setId(idCategorie);
        categorieService.updateCategorie(categorie);
        return "redirect:/admin";
    }

    @PostMapping("/update/credit")
    public String updateCreditUser(@RequestParam("newCredit") float credit, @RequestParam("idUser") int idUser) {
        userService.setCredit(credit, idUser);
        return "redirect:/admin";
    }

    @PostMapping("/deleteC")
    public String deleteCategorie(@RequestParam("deleteIdCategorie") int id) {
        categorieService.deleteCategorie(id);
        return "redirect:/admin";
    }

    @PostMapping("/delete")
    public String deleteUser(@RequestParam("userDelete") int id) {
        userService.deleteUtilisateur(id);
        //Annuler les ventes en cours
        List<Article> userArticles = articleService.findByUser(id);
        for (Article article : userArticles) {
            articleService.deleteArticle(article.getId());
        }
        //Annuler les enchères en cours
        List<Enchere> userEnchere = enchereService.enchereByUser(id);
        for (Enchere enchere : userEnchere) {
            enchereService.deleteEnchere(enchere.getId());
        }
        return "redirect:/admin";
    }

    @PostMapping("/disabled")
    public String disabledUser(@RequestParam("userDisabled") int id, @RequestParam("isDis") boolean isDis) {
        if (isDis){
            userService.enableUtilisateur(id);
        }else {
            userService.disableUtilisateur(id);
            //Annuler les ventes en cours
            List<Article> userArticles = articleService.findByUser(id);
            for (Article article : userArticles) {
                articleService.deleteArticle(article.getId());
            }
            //Annuler les enchères en cours
            List<Enchere> userEnchere = enchereService.enchereByUser(id);
            for (Enchere enchere : userEnchere) {
                enchereService.deleteEnchere(enchere.getId());
            }
        }
        return "redirect:/admin";
    }

    @PostMapping("/administrateur")
    public String administrateurUser(@RequestParam("userAdmin") int id, @RequestParam("isAdmin") boolean isAdmin) {
        if (isAdmin){
            userService.offAdminUtilisateur(id);
        }else {
            userService.onAdminUtilisateur(id);
        }
        return "redirect:/admin";
    }

}
