package com.red.circle.other.adapter.app.game.party3rd;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.red.circle.mq.business.model.event.game.GameSudProperties;
import com.red.circle.other.app.dto.clientobject.game.sud.*;
import com.red.circle.other.app.dto.cmd.sud.SudRequest;
import com.red.circle.other.app.dto.cmd.sud.SudTokenRequest;
import com.red.circle.other.app.dto.cmd.sud.SudTokenUpdateRequest;
import com.red.circle.other.app.service.game.sud.GameSudService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/game/sud", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class GameSudRestController {


    private final GameSudService gameSudService;
    private final GameSudProperties gameSudProperties;
    @PostMapping("/getToken")
    public SudResponse<SudTokenResponse> getToken(@RequestBody @Validated SudTokenRequest request,
                                                  @RequestHeader("Sud-AppId") String appid,
                                                  @RequestHeader("Sud-Timestamp") Long timestamp,
                                                  @RequestHeader("Sud-Nonce") String nonce,
                                                  @RequestHeader("Sud-Signature") String sudSignature
    ) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(request);
        if (!getAppSecret(appid,timestamp,nonce,sudSignature,json)) {
            return SudResponse.fail(SudErrorCode.VERIFICATION_FAILED_PROCEDURE);
        }
        return gameSudService.getToken(request);
    }

    @PostMapping("/updateToken")
    public SudResponse<SudUpdateTokenResponse>  updateToken(@RequestBody @Validated SudTokenUpdateRequest request,
                                                            @RequestHeader("Sud-AppId") String appid,
                                                            @RequestHeader("Sud-Timestamp") Long timestamp,
                                                            @RequestHeader("Sud-Nonce") String nonce,
                                                            @RequestHeader("Sud-Signature") String sudSignature
    ) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(request);
        if (!getAppSecret(appid,timestamp,nonce,sudSignature,json)) {
            return SudResponse.fail(SudErrorCode.VERIFICATION_FAILED_PROCEDURE);
        }
        return gameSudService.updateToken(request);
    }

    @PostMapping("/getUserInfo")
    public  SudResponse<SudUserInfoResponse> getUserInfo(@RequestBody @Validated SudTokenUpdateRequest request,
                                                         @RequestHeader("Sud-AppId") String appid,
                                                         @RequestHeader("Sud-Timestamp") Long timestamp,
                                                         @RequestHeader("Sud-Nonce") String nonce,
                                                         @RequestHeader("Sud-Signature") String sudSignature) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(request);
        if (!getAppSecret(appid,timestamp,nonce,sudSignature,json)) {
            return SudResponse.fail(SudErrorCode.VERIFICATION_FAILED_PROCEDURE);
        }
        return gameSudService.getUserInfo(request);
    }



    private Boolean getAppSecret(String appid,Long timestamp,String nonce, String sudSignature,String request){
        SudRequest sudRequest=new SudRequest();
        sudRequest.setAppId(appid);
        sudRequest.setNonce(nonce);
        sudRequest.setSignature(sudSignature);
        sudRequest.setTimestamp(timestamp);
        return sudRequest.checkSignature(gameSudProperties.getAppSecret(),request);
    }
}
