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

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.constant.OidcUrlConstant;
import com.tuoheng.admin.constant.SystemConstant;
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.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.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Slf4j
@Service
public class AddOidcUserService {

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

    public JsonResult add(User user, Tenant tenant) {
        OidcCreateUserRequest request = this.buildOidcCreateUserRequest(user, tenant);
        //设置请求头
        HttpHeaders resultRequestHeader = new HttpHeaders();
        resultRequestHeader.add("Authorization", "Bearer " + SecurityUserUtils.token());
        HttpEntity httpEntity = new HttpEntity(request, resultRequestHeader);
        String url = CommonConfig.oidcUrl + OidcUrlConstant.USER_CREATE;
        ResponseEntity<JsonResult> response;
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
        } catch (Exception e) {
            log.info("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 OidcCreateUserRequest buildOidcCreateUserRequest(User user, Tenant tenant) {
        OidcCreateUserRequest oidcCreateUserRequest = new OidcCreateUserRequest();
        oidcCreateUserRequest.setUsername(user.getUsername());
        if (StringUtils.isNotEmpty(user.getPassword())) {
            oidcCreateUserRequest.setPassword(user.getPassword());
        } else {
            oidcCreateUserRequest.setPassword(SystemConstant.INIT_PASSWORD);
        }
        if (ObjectUtil.isNull(tenant)) {
            //  普通用户新增
            oidcCreateUserRequest.setTenantFlag(0);
            oidcCreateUserRequest.setTenantName("");
        } else {
            oidcCreateUserRequest.setTenantFlag(1);
            oidcCreateUserRequest.setTenantName(tenant.getName());
        }
        List<ClientRoleDto> clientRoleDtoList = new ArrayList<>();
        ClientRoleDto clientRoleDto;
        if (StringUtils.isNotEmpty(user.getClientId())) {
            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);
                }
            }
        }
        oidcCreateUserRequest.setClientRoleDtoList(clientRoleDtoList);
        return oidcCreateUserRequest;
    }
}
