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.EasydoRole;
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.ZeroQuery;
import zmo.technology.lite.core.ZeroQueryOperation;
import zmo.technology.lite.core.ZeroRequest;
import zmo.technology.lite.core.ZeroRequestGuider;

// @Controller
// @RequestMapping(EasydoUserController.USERS_PREFIX)
public class EasydoUserController extends ZeroCoreSpringController {

    public static final String USERS_PREFIX = "users";

    public EasydoUserController() {
        super(USERS_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<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoUser user : request.getQuerys()) {
            processor.addUser(user);
            if (user.getUserInfo() != null) {
                user.getUserInfo().setUser(user);
                processor.addUserInfo(user.getUserInfo());
            }
        }
        guider.write(success());
    }

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

    public void reset(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoUser user : request.getQuerys())
            processor.resetUserPassword(user);
        guider.write(success());
    }

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

    public void addrole(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoUser user : request.getQuerys()) {
            if (user.getRoles() != null)
                for (EasydoRole role : user.getRoles())
                    processor.addUserRole(user.uuIDString(), role.uuIDString());
        }
        guider.write(success());
    }

    public void rmrole(ZeroRequestGuider guider) throws Exception {
        ZeroRequest<EasydoUser> request = ZeroRequest.build(EasydoUser.class, guider.getRawData());
        EasydoCoreProcessor processor = EasydoCoreProcessor.build(guider);
        for (EasydoUser user : request.getQuerys()) {
            if (user.getRoles() != null)
                for (EasydoRole role : user.getRoles())
                    processor.rmUserRole(user.uuIDString(), role.uuIDString());
        }
        guider.write(success());
    }

    protected static final String OPTION_ROLES = "roles";

    public void fetch(ZeroRequestGuider guider) throws Exception {

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

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

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

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