package easydo.technology.core.controllers;

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.EasydoCore.EasydoRole;
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(EasydoRoleController.ROLES_PREFIX)
public class EasydoRoleController extends ZeroCoreSpringController {
    
    public static final String ROLES_PREFIX = "roles";

    public EasydoRoleController() {
        super(ROLES_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 create(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoRole> request = ZeroRequest.build(EasydoRole.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoRole role : request.getQuerys())
            processor.addRole(role);
        guider.write(success());
    }

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

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

    public void addmenu(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoRole> request = ZeroRequest.build(EasydoRole.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoRole role : request.getQuerys()) {
            if (role.getMenus() != null)
                for (EasydoMenu menu : role.getMenus())
                    processor.addRoleMenus(role.uuIDString(), menu.uuIDString());
        }
        guider.write(success());
    }

    public void rmmenu(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoRole> request = ZeroRequest.build(EasydoRole.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoRole role : request.getQuerys()) {
            if (role.getMenus() != null)
                for (EasydoMenu menu : role.getMenus())
                    processor.rmRoleMenus(role.uuIDString(), menu.uuIDString());
        }
        guider.write(success());
    }

    protected static final String OPTION_MENUS = "menus";

    public void fetch(ZeroRequestGuider guider) throws Exception {

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

        ZeroQueryOperation<EasydoRole> operation = ZeroQueryOperation.build(EasydoRole.class, guider);
        operation.openConnect(request.getQuerys().get(0).setTableName("easydo_roles"));

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

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