package com.eactive.testmaster.queue.controller;

import com.eactive.testmaster.queue.entity.QueueHandler;
import com.eactive.testmaster.queue.mapper.QueueHandlerMapper;
import com.eactive.testmaster.queue.service.DynamicQueueManager;
import com.eactive.testmaster.queue.service.QueueHandlerService;
import com.eactive.testmaster.queue.dto.QueueHandlerDTO;
import com.eactive.testmaster.queue.dto.QueueHandlerSearch;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.PostConstruct;
import java.util.List;

@Controller
@RequestMapping("/mgmt/queues")
public class QueueHandlerMgmtController {

    private static final String QUEUE_HANDLER = "queueHandler";
    private static final String QUEUE_HANDLER_EDIT = "page/queues/queueHandlerEdit";
    private static final String QUEUE_HANDLER_LIST_VIEW = "redirect:/mgmt/queues/list_view.do";

    private final QueueHandlerService queueHandlerService;
    private final Validator validator;
    private final QueueHandlerMapper queueHandlerMapper;
    private final DynamicQueueManager dynamicQueueManager;

    public QueueHandlerMgmtController(QueueHandlerService queueHandlerService, Validator validator, QueueHandlerMapper queueHandlerMapper, DynamicQueueManager dynamicQueueManager) {
        this.queueHandlerService = queueHandlerService;
        this.validator = validator;
        this.queueHandlerMapper = queueHandlerMapper;
        this.dynamicQueueManager = dynamicQueueManager;

    }


    @PostConstruct
    public void init() {
        queueHandlerService.initAllQueues();
    }

    @GetMapping("/list_view.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public ModelAndView listView(@ModelAttribute("queueHandlerSearch") QueueHandlerSearch queueHandlerSearch,
        Pageable pageable) {
        ModelAndView mav = new ModelAndView("page/queues/queueHandlerList");
        Page<QueueHandler> page = queueHandlerService.findAll(queueHandlerSearch.buildSpecification(), pageable);
        mav.addObject("page", page);
        mav.addObject("pageable", pageable);
        mav.addObject("modelSearch", queueHandlerSearch);
        return mav;
    }

    @GetMapping("/create_view.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public ModelAndView createView() {
        ModelAndView mav = new ModelAndView(QUEUE_HANDLER_EDIT);
        mav.addObject(QUEUE_HANDLER, new QueueHandlerDTO());
        return mav;
    }

    @GetMapping("/update_view.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public ModelAndView updateView(@RequestParam("id") String id) {
        ModelAndView mav = new ModelAndView(QUEUE_HANDLER_EDIT);
        QueueHandler found = queueHandlerService.findById(id);
        mav.addObject(QUEUE_HANDLER, found); // Assuming direct usage of entity, you might want to use a DTO mapper
        return mav;
    }

    @PostMapping("/register.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public String register(@ModelAttribute("queueHandler") QueueHandlerDTO dto,
        BindingResult bindingResult,
        ModelMap model) {

        if (dto == null) {
            return "redirect:/mgmt/queues/create_view.do";
        }

        validator.validate(dto, bindingResult);

        if (bindingResult.hasErrors()) {
            model.addAttribute(QUEUE_HANDLER, dto);
            return QUEUE_HANDLER_EDIT;
        }

        QueueHandler queueHandler = queueHandlerMapper.map(dto);
        queueHandlerService.create(queueHandler);
        queueHandlerService.initAllQueues();
        return QUEUE_HANDLER_LIST_VIEW;
    }

    @PostMapping("/update.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public String update(@RequestParam("queueName") String queueName,
                        @ModelAttribute("queueHandler") QueueHandlerDTO dto,
        BindingResult bindingResult,
        ModelMap model) {

        if (dto.getQueueName() == null) {
            return QUEUE_HANDLER_LIST_VIEW;
        }

        validator.validate(dto, bindingResult);

        if (bindingResult.hasErrors()) {
            model.addAttribute(QUEUE_HANDLER, dto);
            return QUEUE_HANDLER_EDIT;
        }

        QueueHandler queueHandler =  queueHandlerMapper.map(dto);
        queueHandlerService.update(dto.getQueueName(), queueHandler);
        queueHandlerService.initAllQueues();
        return QUEUE_HANDLER_LIST_VIEW;
    }

    @PostMapping("/delete.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public String delete(@RequestParam("checkedIdForDel") List<String> ids) {
        for (String id : ids) {
            queueHandlerService.deleteById(id);
        }
        queueHandlerService.initAllQueues();
        return QUEUE_HANDLER_LIST_VIEW;
    }

    @PostMapping("/start.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public String startQueue(@RequestParam("id") String id) {
        QueueHandler queueHandler = queueHandlerService.findById(id);
        dynamicQueueManager.startQueueListener(queueHandler);
        return QUEUE_HANDLER_LIST_VIEW;
    }

    @PostMapping("/stop.do")
    @Secured("ROLE_MANAGE_QUEUE")
    public String stopQueue(@RequestParam("id") String id) {
        QueueHandler queueHandler = queueHandlerService.findById(id);
        dynamicQueueManager.stopQueueListener(queueHandler.getQueueName());
        return QUEUE_HANDLER_LIST_VIEW;
    }
}
