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

import com.tuoheng.admin.constant.OidcUrlConstant;
import com.tuoheng.admin.dto.ClientRoleDto;
import com.tuoheng.admin.entity.Tenant;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.ClientEnum;
import com.tuoheng.admin.request.oidc.OidcCreateUserRequest;
import com.tuoheng.admin.request.oidc.OidcUpdateUserRequest;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.SecurityUserUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Slf4j
@Service
public class EditOidcUserService {

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    public JsonResult edit(User user) {
        //修改用户角色
        OidcUpdateUserRequest oidcUpdateUserRequest = buildOidcUpdateUserRequest(user);

        //设置请求头
        HttpHeaders resultRequestHeader = new HttpHeaders();
        resultRequestHeader.add("Authorization", "Bearer " + SecurityUserUtils.token());
        HttpEntity httpEntity = new HttpEntity(oidcUpdateUserRequest, resultRequestHeader);
        //设置地址(飞手平台)
        String url = CommonConfig.oidcUrl + OidcUrlConstant.USER_UPDATEROLE;
        ResponseEntity<JsonResult> response = null;
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
        } catch (Exception e) {
            log.error("oidc修改用户信息响应失败");
            return JsonResult.error("oidc修改用户信息响应失败");
        }
        if (null == response || !response.hasBody()) {
            log.error("oidc修改用户信息响应失败");
            return JsonResult.error("oidc修改用户信息响应失败");
        }
        if (response.getBody().getCode() != JsonResult.SUCCESS) {
            log.error("oidc修改用户响应失败" + response.getBody());
            return JsonResult.error(response.getBody().getMsg());
        }
        return JsonResult.success();
    }

    /**
     * 封装请求->oidc认证信息
     *
     * @param user
     * @return
     */
    private OidcUpdateUserRequest buildOidcUpdateUserRequest(User user) {
        OidcUpdateUserRequest oidcUpdateUserRequest = new OidcUpdateUserRequest();
        oidcUpdateUserRequest.setUsername(user.getUsername());
        List<ClientRoleDto> clientRoleDtoList = new ArrayList<>();
        ClientRoleDto clientRoleDto;
        String[] clientIdArr = user.getClientId().split(",");
        for (String clientIdStr : clientIdArr) {
            int clientId = Integer.valueOf(clientIdStr);
            ClientEnum clientEnum = ClientEnum.parse(clientId);
            if (null != clientEnum) {
                clientRoleDto = new ClientRoleDto();
                clientRoleDto.setClientId(clientEnum.getDescription());
                clientRoleDto.setRoleId(user.getRoleId());
                clientRoleDtoList.add(clientRoleDto);
            }
        }
        oidcUpdateUserRequest.setClientRoleDtoList(clientRoleDtoList);
        return oidcUpdateUserRequest;
    }
}
