package com.nokia.controller;

import com.alibaba.fastjson.JSONObject;
import com.nokia.common.R;
import com.nokia.pojo.Role;
import com.nokia.service.FlowService;
import com.nokia.vo.flow.FlowRoleCityVo;
import com.nokia.vo.flow.FlowRoleVo;
import com.nokia.vo.flow.FlowUserVo;
import com.nokia.vo.flow.UserTreeReq;

import lombok.extern.slf4j.Slf4j;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 提供给工作流的api
 */
@Slf4j
@RestController
@RequestMapping("rest")
public class FlowApiController {

    private final FlowService flowService;

    @Autowired
    public FlowApiController(FlowService flowService) {
        this.flowService = flowService;
    }

    @PostMapping("flow/api/userTree")
    public R getUserTree(@RequestBody UserTreeReq userTreeReq) {
        List<Map<String, Object>> userTree = flowService.getUserTree(userTreeReq);
        return R.ok().data(userTree);
    }

    @PostMapping("flow/api")
    @ResponseBody
    public R flowApi(@RequestBody JSONObject jsonObject) {
        String callType = jsonObject.getString("callType");
        if (callType == null) {
            return R.error().message("输入必须包含callType");
        }
        switch (callType) {
            case "findRoleList":
                return findRoleList(jsonObject);
            case "findToPage":
                return findToPage();
            case "getByLoginId":
                return getByLoginId(jsonObject);
            case "get":
                return get(jsonObject);
            case "findRoleIdByUserId":
                return findRoleIdByUserId(jsonObject);
            case "findUserIdByRoleId":
                return findUserIdByRoleId(jsonObject);
            case "findAuthorizedUser":
                return findAuthorizedUser(jsonObject);
            case "getRoleCityByUserId":
                return getRoleCityByUserId(jsonObject);
            default:
                return R.error().message("callType类型错误");
        }
    }

    /**
     * 
     * @param jsonObject
     * @return
     */
    private R getRoleCityByUserId(JSONObject jsonObject) {
        Integer userId = jsonObject.getInteger("userId");
        if (null == userId) {
            return R.error().message("输入必须包含userId");
        }
        List<FlowRoleCityVo> flowRoleCityVos = new ArrayList<>();
        for (Role role : flowService.getRoleCityByUserId(userId)) {
            log.debug("查询结果：{}", role);
            if ("flow".equals(role.getSystem())) {
                flowRoleCityVos.add(new FlowRoleCityVo(role));
            }
        }
        return R.ok().data(flowRoleCityVos);
    }

    public R findRoleList(JSONObject jsonObject) {
        String username = jsonObject.getString("userName");
        if (username == null || username.equals("")) {
            return R.ok().data(flowService.findRoleList());
        }
        List<FlowRoleVo> roleList = flowService.findRoleList(username);
        return R.ok().data(roleList);
    }

    public R findToPage() {
        return R.ok().data(flowService.findToPage());
    }

    public R getByLoginId(JSONObject jsonObject) {
        String loginId = jsonObject.getString("loginId");
        if (loginId == null || loginId.equals("")) {
            return R.error().message("输入必须包含loginId");
        }
        FlowUserVo flowUserVo = flowService.getByLoginId(loginId);
        return R.ok().data(flowUserVo);
    }

    public R get(JSONObject jsonObject) {
        Integer userId = null;
        try {
            userId = jsonObject.getInteger("userId");
        } catch (NumberFormatException e) {
            return R.error().message("输入的userId必须为整数格式");
        }
        if (userId == null) {
            return R.error().message("输入必须包含userId");
        }
        FlowUserVo flowUserVo = flowService.getByUserId(userId);
        return R.ok().data(flowUserVo);
    }

    public R findRoleIdByUserId(JSONObject jsonObject) {
        Integer userId = null;
        try {
            userId = jsonObject.getInteger("userId");
        } catch (NumberFormatException e) {
            return R.error().message("输入的userId必须为整数格式");
        }
        if (userId == null) {
            return R.error().message("输入必须包含userId");
        }
        List<Integer> roleIdList = flowService.findRoleIdByUserId(userId);
        return R.ok().data(roleIdList);
    }

    public R findUserIdByRoleId(JSONObject jsonObject) {
        Integer roleId = null;
        try {
            roleId = jsonObject.getInteger("roleId");
        } catch (NumberFormatException e) {
            return R.error().message("输入的roleId必须为整数格式");
        }
        if (roleId == null) {
            return R.error().message("输入必须包含roleId");
        }
        List<Integer> userIdList = flowService.findUserIdByRoleId(roleId);
        return R.ok().data(userIdList);
    }

    public R findAuthorizedUser(JSONObject jsonObject) {
        String loginId = jsonObject.getString("loginId");
        loginId = loginId == null || loginId.equals("") ? null : loginId.trim();
        Integer roleId = null;
        try {
            roleId = jsonObject.getInteger("roleId");
        } catch (NumberFormatException ignored) {
        }
        String userName = jsonObject.getString("userName");
        userName = userName == null || userName.equals("") ? null : userName.trim();
        List<FlowUserVo> flowUserVos = flowService.findAuthorizedUser(loginId, roleId, userName);
        return R.ok().data(flowUserVos);
    }
}
