/*
 * ********************************************************************************************************************
 *
 *                      :*$@@%$*:                         ;:                ;;    ;;
 *                    :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                   :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                   =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                   =@*                                  %!              @ $= % %@=   =%@!      %=
 *              *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *            %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *          ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *          $@*   ;@@@%=!:                *@*
 *          =@$    ;;;!=%@@@@=!           =@!
 *           %@$:      =@%: :*@@@*       %@=                    Copyright (c) 2016-2019.  北京上格云技术有限公司
 *            ;%@@$=$@@%*       *@@@$=%@@%;
 *               ::;::             ::;::                                              All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.persagy.server.sso

import com.persagy.server.models.requests.UserChangePasswordRequest
import com.persagy.server.models.requests.UserDeleteRequest
import com.persagy.server.models.requests.UserUpdateRequest
import com.persagy.server.models.responses.BaseResponse
import com.persagy.server.models.responses.TokenCheckResponse
import com.persagy.server.models.responses.UserCreateRequest
import com.persagy.server.models.responses.UserCreateResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.client.RestTemplate

/**
 * 用户服务
 *
 * @author  庞利祥(sybotan@126.com)
 */
object SSoClient {

    // 日志
    private val logger = LoggerFactory.getLogger(SSoClient::class.java)
    private const val SSO = "http://SAGACLOUD-SSO"

    /**
     * 创建用户
     */
    fun create(@RequestBody request: UserCreateRequest): UserCreateResponse {
        val user = HashMap<String, Any?>()
        user["username"] = request.userName
        user["password"] = request.password
        user["realname"] = request.realName
        user["phone"]    = request.phone
        user["mail"]     = request.mail
        user["certno"]   = request.certno

        val restTemplate = SSpringContextUtil.getBean(RestTemplate::class.java) as RestTemplate
        return restTemplate.postForObject("$SSO/server/user/create", user, UserCreateResponse::class.java) as UserCreateResponse
    } // Function create()

    /**
     * 更新用户
     */
    fun update(@RequestBody request: UserUpdateRequest): BaseResponse {
        val user = HashMap<String, Any?>()
        user["id"]       = request.id
        user["username"] = request.userName
        user["realname"] = request.realName
        user["phone"]    = request.phone
        user["mail"]     = request.mail
        user["certno"]   = request.certno

        val restTemplate = SSpringContextUtil.getBean(RestTemplate::class.java) as RestTemplate
        return restTemplate.postForObject("$SSO/server/user/update", user, BaseResponse::class.java) as BaseResponse
    } // Function update()

    /**
     * 删除用户
     */
    fun delete(@RequestBody request: UserDeleteRequest): BaseResponse {
        val user = HashMap<String, Any?>()
        user["id"]       = request.id

        val restTemplate = SSpringContextUtil.getBean(RestTemplate::class.java) as RestTemplate
        return restTemplate.postForObject("$SSO/server/user/delete", user, BaseResponse::class.java) as BaseResponse
    } // Function delete()

    /**
     * 修改用户密码
     */
    fun changePwd(@RequestBody request: UserChangePasswordRequest): BaseResponse {
        val user = HashMap<String, Any?>()
        user["id"]       = request.id
        user["newPwd"]   = request.newPwd
        user["oldPwd"]   = request.oldPwd

        val restTemplate = SSpringContextUtil.getBean(RestTemplate::class.java) as RestTemplate
        return restTemplate.postForObject("$SSO/server/user/delete", user, BaseResponse::class.java) as BaseResponse
    } // Function changePwd()

    /**
     * 审核token
     *
     * @param token     被审核的token
     */
    fun tokenCheck(@RequestBody token: String): TokenCheckResponse {
        val restTemplate = SSpringContextUtil.getBean(RestTemplate::class.java) as RestTemplate
        return restTemplate.getForObject("$SSO/auth/token/check?token=$token", TokenCheckResponse::class.java) as TokenCheckResponse
    } // Function tokenCheck()
} // Object SSoClient()
