package easydo.technology.core.controllers;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import easydo.technology.core.EasydoCore.EasydoMenu;
import easydo.technology.core.ZeroCoreSpringController;
import easydo.technology.core.processors.EasydoCoreProcessor;
import zmo.technology.lite.core.ZeroException;
import zmo.technology.lite.core.ZeroQuery;
import zmo.technology.lite.core.ZeroQueryOperation;
import zmo.technology.lite.core.ZeroRequest;
import zmo.technology.lite.core.ZeroRequestGuider;

// @Controller
// @RequestMapping(EasydoMenusController.MENUS_PREFIX)
public class EasydoMenusController extends ZeroCoreSpringController {

    public static final String MENUS_PREFIX = "menus";

    public EasydoMenusController() {
        super(MENUS_PREFIX);
    }

    @Autowired
    private DataSource dataSource;

    @Override
    protected String handlerNameConstructor(ZeroRequestGuider guider) throws Exception {
        if (guider.getPaths().size() != 1)
            throw new ZeroException(404, "no handlers");
        return guider.getPaths().get(0);
    }

    @RequestMapping(GENERAL_HANDLER_PATH)
    public void general(HttpServletRequest request, HttpServletResponse response) throws Exception {
        proxyHandler(dataSource, request, response);
    }

    public void add(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoMenu> request = ZeroRequest.build(EasydoMenu.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoMenu menu : request.getQuerys())
            processor.addMenus(menu);
        guider.write(success());
    }

    public void up(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoMenu> request = ZeroRequest.build(EasydoMenu.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoMenu menu : request.getQuerys())
            processor.upMenu(menu);
        guider.write(success());
    }

    public void rm(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoMenu> request = ZeroRequest.build(EasydoMenu.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoMenu menu : request.getQuerys())
            processor.rmMenu(menu);
        guider.write(success());
    }

    public void addsubmenu(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoMenu> request = ZeroRequest.build(EasydoMenu.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoMenu menu : request.getQuerys()) {
            for (EasydoMenu submenu : menu.getSubMenus()) {
                if (submenu.getId() == null)
                    processor.addMenus(submenu);
                processor.addMenusRelevancy(menu.uuIDString(), submenu.uuIDString());
            }
        }
        guider.write(success());
    }

    public void rmsubmenu(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoMenu> request = ZeroRequest.build(EasydoMenu.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoMenu menu : request.getQuerys()) {
            for (EasydoMenu submenu : menu.getSubMenus()) {
                processor.rmMenusRelevancy(menu.uuIDString(), submenu.uuIDString());
            }
        }
        guider.write(success());
    }

    protected static final String OPTION_ROOT = "root";
    protected static final String OPTION_RECURSION = "recursion";

    private void recursionMenus(EasydoCoreProcessor processor, EasydoMenu menu) throws SQLException {
        processor.fetchSubMenus(menu);
        for (EasydoMenu submenu : menu.getSubMenus())
            recursionMenus(processor, submenu);
    }

    public void fetch(ZeroRequestGuider guider) throws Exception {

        ZeroRequest<ZeroQuery<EasydoMenu>> request = ZeroRequest.build(guider.getRawData(),
                EasydoMenu.class);

        ZeroQueryOperation<EasydoMenu> operation = ZeroQueryOperation.build(EasydoMenu.class, guider);
        operation.openConnect(request.getQuerys().get(0).setTableName("easydo_menus_view"));
        if (request.hasOption(OPTION_ROOT))
            operation.appendCondition(() -> "MENU_ID IS NULL");

        List<EasydoMenu> datas = new ArrayList<>();
        JSONObject expands = new JSONObject();
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);

        operation.exec(() -> {
            EasydoMenu data = operation.getCurrent();
            if (request.hasOption(OPTION_RECURSION))
                recursionMenus(processor, data);
            datas.add(data);
        }).eachExpands((key, value) -> expands.put(key, value));
        guider.write(success(datas, expands));
    }
}
