package ua.com.javer.flowerexpert.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ua.com.javer.flowerexpert.entity.Flower;
import ua.com.javer.flowerexpert.entity.User;
import ua.com.javer.flowerexpert.service.FlowersService;
import ua.com.javer.flowerexpert.service.UserService;

@Controller
@RequestMapping("/manager-page")
public class ManagerController {
    private static final Logger DEBUG_LOGGER = Logger.getLogger(ManagerController.class);

    @Autowired
    UserService userService;

    @Autowired
    FlowersService flowersService;

    @RequestMapping("")
    public String showIndexManagerPage(Model model){
//        if( DEBUG_LOGGER.isDebugEnabled()){
//            DEBUG_LOGGER.debug( "ManagerController.showIndexManagerPage() is executed!" );
//        }
        return "manager";
    }

    @RequestMapping("/flowers")
    public String showManagerFlowers(Model model){
//        if( DEBUG_LOGGER.isDebugEnabled()){
//            DEBUG_LOGGER.debug( "ManagerController.showManagerFlowers() is executed!" );
//        }
        String userEmail = ((org.springframework.security.core.userdetails.User)(SecurityContextHolder
                .getContext()
                .getAuthentication()
                .getPrincipal()))
                .getUsername();

        User user = userService.getUserByEmail(userEmail);
        model.addAttribute("flowers", user.getFlowers());
        return "pages/entities/flower/show-flowers";
    }

    @RequestMapping(value = "/flowers/edit/{flowerId}", method = RequestMethod.GET)
    public String editFlower(Model model, @PathVariable int flowerId){
        Flower flower= flowersService.getFlowerById(flowerId);
        model.addAttribute("flower", flower);
        return "pages/entities/flower/edit-flower";
    }

}
