package easydo.technology.core.controllers;

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 easydo.technology.core.EasydoCore.EasydoUser;
import easydo.technology.core.ZeroCoreSpringController;
import easydo.technology.core.processors.EasydoCoreProcessor;
import zmo.technology.lite.core.ZeroException;
import zmo.technology.lite.core.ZeroRequest;
import zmo.technology.lite.core.ZeroRequestGuider;

// @Controller
// @RequestMapping(EasydoAuthController.AUTH_PREFIX)
public class EasydoAuthController extends ZeroCoreSpringController {

    public static final String AUTH_PREFIX = "auth";

    public EasydoAuthController() {
        super(AUTH_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 login(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);

        guider.write(success());
    }

    public void reset(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);

        guider.write(success());
    }

    public void info(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);

        guider.write(success());
    }
}
