package com.tuoheng.admin.service.third.oidc;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tuoheng.admin.dto.RolePermissionDto;
import com.tuoheng.admin.entity.Tenant;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.mapper.MenuMapper;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.request.third.oidc.CreateOidcTenantRequest;
import com.tuoheng.admin.request.third.oidc.DeletedOidcTenantRequest;
import com.tuoheng.admin.request.third.oidc.EditOidcTenantRequest;
import com.tuoheng.admin.service.permissions.OpPermissionsService;
import com.tuoheng.admin.service.role.IRoleService;
import com.tuoheng.admin.service.role.OpRolePermissionService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OidcServiceImpl implements OidcService {

    @Autowired
    private AddOidcUserService addOidcUserService;

    @Autowired
    private EditOidcUserService editOidcUserService;

    @Autowired
    private EditOidcUserPasswordService editOidcUserPasswordService;

    @Autowired
    private AddOidcTenantService addOidcTenantService;

    @Autowired
    private EditOidcTenantService editOidcTenantService;

    @Autowired
    private DeletedOidcTenantService deletedOidcTenantService;

    @Autowired
    private IRoleService roleService;
    @Autowired
    private MenuMapper menuMapper;


    public JsonResult addUser(User user, String password, Tenant tenant) {
        return addOidcUserService.add(user, password, tenant);
    }

    public JsonResult editUser(User user) {
        return editOidcUserService.edit(user);
    }

    public JsonResult editUserPassword(User user, String password) {
        return editOidcUserPasswordService.editUserPassword(user, password);
    }

    @Override
    public JsonResult createTenant(CreateOidcTenantRequest request) {
        return addOidcTenantService.createTenant(request);
    }

    @Override
    public JsonResult editTenant(EditOidcTenantRequest request) {
        return editOidcTenantService.editTenant(request);
    }

    @Override
    public JsonResult deletedTenant(DeletedOidcTenantRequest request) {
        return deletedOidcTenantService.deletedTenant(request);
    }

    /**
     * 租户获取可用的角色
     *
     * @return
     */
    @Override
    public JsonResult getRoleList() {

        JsonResult jsonResult = roleService.getRoles();
        return jsonResult;
    }

    /**
     * 查询角色对应的菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public JsonResult getMenuList(Integer roleId) {
        RolePermissionDto rolePermissionDto = new RolePermissionDto();
        rolePermissionDto.setOpMenusList(menuMapper.getMenuListByRoleId(roleId));
        rolePermissionDto.setRoleId(roleId);
        return JsonResult.success(rolePermissionDto);
    }
}
