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.EasydoProject;
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(EasydoProjectController.PROJECTS_PREFIX)
public class EasydoProjectController extends ZeroCoreSpringController {

    public static final String PROJECTS_PREFIX = "projects";

    public EasydoProjectController() {
        super(PROJECTS_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<EasydoProject> request = ZeroRequest.build(EasydoProject.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoProject project : request.getQuerys())
            processor.addProject(project);
        guider.write(success());
    }

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

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

    protected static final String OPTION_ENTERPRISE = "enterprise";

    public void fetch(ZeroRequestGuider guider) throws Exception {

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

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

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

        operation.exec(() -> {
            EasydoProject data = operation.getCurrent();
            if (request.hasOption(OPTION_ENTERPRISE))
                data.setEnterprise(processor.fetchEnterpriseByID(data.getEnterprise().uuIDString()));
            datas.add(data);
        }).eachExpands((key, value) -> expands.put(key, value));
        guider.write(success(datas, expands));
    }
}
