package com.roma.romaapi.controller;


import com.roma.romaapi.service.ApiService;
import com.roma.romaapi.service.CaptchaService;
import com.roma.romaapi.utils.CommonUtil;
import com.roma.romaapi.utils.CustomResponse;
import com.roma.romaapi.utils.UUIDUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
public class ApiController {
    private Integer timeout = 600;
    @Autowired
    CaptchaService captchaService;
    @Autowired
    ApiService apiService;
    @Autowired
    private UUIDUtil uuidUtil;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    CommonUtil commonUtil;
    // 获取验证码
    @RequestMapping("/login/verifyCode")
    public Map<String, Object> loginVerifyCode() throws IOException {
        return captchaService.captchaCreator();
    }

    // 登录系统
    @RequestMapping("/api/login")
    public Map login(HttpServletRequest request){
        Map<String, String[]> maps = request.getParameterMap();
        Map<String, Object> loginResult = apiService.login(maps);

        return CustomResponse.formatResponse(loginResult);
    }

    // 获取用户信息
    @RequestMapping("/api/userInfo")
    public Map userInfo(@RequestHeader("Authorization") String authorization){
        Map<String, Object> loginResult = apiService.userInfo(authorization);

        return CustomResponse.formatResponse(loginResult);
    }

    // 退出登录
    @RequestMapping("/api/logout")
    public Map logout(@RequestHeader("Authorization") String authorization){
        Map<String, Object> loginResult = apiService.logout(authorization);

        return CustomResponse.formatResponse(loginResult);
    }

    //=============================================权限相关======================================
    // 权限管理-获取所有权限
    @RequestMapping("/api/permissionList")
    public Map permissionList(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<>();
        List allPermiss = apiService.permissionList();
        ret.put("options", allPermiss);

        return CustomResponse.formatResponse(ret);
    }

    // 权限管理-获取全部权限项
    @RequestMapping("/api/selectPermissionList")
    public Map pagePermissionList(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<>();
        List allPermiss = apiService.selectPermissionList();
        ret.put("options", allPermiss);

        return CustomResponse.formatResponse(ret);
    }

    // 权限管理-添加权限
    @RequestMapping("/api/addPermissions")
    public Map addPermissions(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();

        ret = apiService.addPermissions(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 获取指定权限项数据
    @RequestMapping("/api/permissionDetailed")
    public Map permissionDetailed(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map detailInfo = apiService.permissionDetailById(maps);

        return CustomResponse.formatResponse(detailInfo);
    }

    // 权限管理-添加权限-获取所有页面,选择关联页面,无分页,且为options格式
    @RequestMapping("/api/pageList")
    public Map pageDetailsList(){
        Map<String, Object> ret = new HashMap<>();
        List pageList = apiService.pageList();
        ret.put("options", pageList);

        return CustomResponse.formatResponse(ret);
    }

    // 返回带分页格式的数据-page列表管理页
    @RequestMapping("/api/pageManageList")
    public Map pageManageList(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.pageListHasPaging(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 获取指定页面page_details表数据
    @RequestMapping("/api/pageDetail")
    public Map pageDetail(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map detailInfo = apiService.pageDetailById(maps);
        detailInfo.put("sysPageDetails", "1");

        return CustomResponse.formatResponse(detailInfo);
    }

    // 页面管理-新增页面
    @RequestMapping("/api/pageSave")
    public Map pageSave(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.addPage(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 返回带分页格式的数据-sql列表管理页
    @RequestMapping("/api/sqlManageList")
    public Map sqlManageList(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.sqlListHasPaging(maps);

        return CustomResponse.formatResponse(ret);
    }

    // sql管理-获取sql详情
    @RequestMapping("/api/sqlDetailed")
    public Map sqlDetailed(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.sqlDetailById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // basic基础管理-删除对应数据
    @RequestMapping("/api/deleteBasic")
    public Map deleteBasic(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.delDetailById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 删除权限数据
    @RequestMapping("/api/deletePermission")
    public Map deletePermission(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.deletePermissionById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // sql管理-新增和编辑Sql
    @RequestMapping("/api/sqlSave")
    public Map sqlSave(HttpServletRequest request, @RequestBody String data, @RequestHeader("Authorization") String authorization) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.addSql(maps, authorization);

        return CustomResponse.formatResponse(ret);
    }

    // 权限管理-添加权限-获取所有页面,选择关联sql,无分页,且为options格式
    @RequestMapping("/api/sqlList")
    public Map sqlDetailsList(){
        Map<String, Object> ret = new HashMap<>();
        List pageList = apiService.sqlList();
        ret.put("options", pageList);

        return CustomResponse.formatResponse(ret);
    }

    // 权限管理-添加权限-获取所有页面,选择关联api,无分页,且为options格式
    @RequestMapping("/api/apiList")
    public Map apiDetailsList(){
        Map<String, Object> ret = new HashMap<>();
        List apiList = apiService.apiList();
        ret.put("options", apiList);

        return CustomResponse.formatResponse(ret);
    }

    // 权限管理-添加权限-获取所有页面,选择关联item,无分页,且为options格式
    @RequestMapping("/api/itemList")
    public Map itemDetailsList(){
        Map<String, Object> ret = new HashMap<>();
        List pageList = apiService.itemList();
        ret.put("options", pageList);

        return CustomResponse.formatResponse(ret);
    }

    // 角色管理-获取全部角色
    @RequestMapping("/api/roleList")
    public Map roleList(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.roleList(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 角色管理-获取角色详情
    @RequestMapping("/api/roleDetailed")
    public Map roleDetailed(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.roleDetailById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 添加角色
    @RequestMapping("/api/roleSave")
    public Map roleSave(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.addRole(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 返回带分页格式的数据-api列表管理页
    @RequestMapping("/api/apiManageList")
    public Map apiManageList(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.apiListHasPaging(maps);

        return CustomResponse.formatResponse(ret);
    }

    // api管理-新增和编辑api
    @RequestMapping("/api/apiSave")
    public Map apiSave(HttpServletRequest request, @RequestBody String data, @RequestHeader("Authorization") String authorization) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.saveApi(maps, authorization);

        return CustomResponse.formatResponse(ret);
    }

    // Api管理-获取api详情
    @RequestMapping("/api/apiDetailed")
    public Map apiDetailed(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.apiDetailById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 返回带分页格式的数据-item列表管理页
    @RequestMapping("/api/itemManageList")
    public Map itemManageList(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.itemListHasPaging(maps);

        return CustomResponse.formatResponse(ret);
    }

    // item管理-新增和编辑item
    @RequestMapping("/api/itemSave")
    public Map itemSave(HttpServletRequest request, @RequestBody String data,
                        @RequestHeader("Authorization") String authorization) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.saveItem(maps, authorization);

        return CustomResponse.formatResponse(ret);
    }

    // Item管理-获取item详情
    @RequestMapping("/api/itemDetailed")
    public Map itemDetailed(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.itemDetailById(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 返回带分页格式的数据-用户管理列表页
    @RequestMapping("/api/userManageList")
    public Map userManageList(HttpServletRequest request, @RequestBody String data){
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map<String, Object> ret = new HashMap<>();
        ret = apiService.userListHasPaging(maps);

        return CustomResponse.formatResponse(ret);
    }

    // 用户分配角色,无分页,且为options格式
    @RequestMapping("/api/optionRoleList")
    public Map optionRoleList(){
        Map<String, Object> ret = new HashMap<>();
        List pageList = apiService.roleList();
        ret.put("options", pageList);

        return CustomResponse.formatResponse(ret);
    }

    // 上传头像
    @RequestMapping("api/uploadSave")
    public Map uploadSave(@RequestParam("file") MultipartFile file) {
        Map<String, Object> ret = new HashMap<>();
        Map uploadInfo = commonUtil.uploadFile(file);
        if(uploadInfo.containsKey("uploadPath")) {
            ret.put("value", uploadInfo.get("uploadPath").toString());
        }

        return CustomResponse.formatResponse(ret);
    }

    // 用户管理-新增用户
    @RequestMapping("/api/userSave")
    public Map userSave(HttpServletRequest request, @RequestBody String data,
                        @RequestHeader("Authorization") String authorization) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数

        Map ret = apiService.userSave(maps, authorization);

        return CustomResponse.formatResponse(ret);
    }

    // 用户管理-通过用户id获取用户详情
    @RequestMapping("/api/userDetailById")
    public Map userDetailById(HttpServletRequest request, @RequestBody String data) {
        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
        Map ret = apiService.getUserInfoById(maps);

        return CustomResponse.formatResponse(ret);
    }
}
