package com.zanxiang.game.back.serve.oceanengine;

import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.encryption.ShaUtil;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
@Component
public class MiniGameCallback {
    private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
    @Autowired
    private RestTemplate restTemplate;

    public void doCallback(String url, String token, MiniGameCallbackRequest request) throws OceanengineCallbackException {
        int time = Long.valueOf(System.currentTimeMillis() / 1000).intValue();
        int nonce = RANDOM.nextInt();
        String[] arr = {token, String.valueOf(nonce), String.valueOf(time)};
        Arrays.sort(arr);
        String signature = ShaUtil.encryptSHA1(arr[0] + arr[1] + arr[2]);

        String params = "timestamp=" + time + "&nonce=" + nonce + "&signature=" + signature;
        url = url.contains("?") ? (url + "&" + params) : (url + "?" + params);
        log.error("头条小游戏回传参数,url: {}, body{}", url, JsonUtil.toString(request));
        ResponseEntity<Map> response = restTemplate.postForEntity(url, request, Map.class);
        log.error("头条小游戏回传完成：{}", JsonUtil.toString(response.getBody()));
        if (response.getStatusCode().is2xxSuccessful()) {
            return;
        }
        throw new OceanengineCallbackException(response.getStatusCodeValue(), JsonUtil.toString(response.getBody()));
    }

    @Getter
    public enum EventType {
        /**
         * 投放微信小游戏时，可将首次「调起微信小游戏」定义为激活事件
         * 即，新用户/新设备首次联网打开（不同客户定义口径存在差异，可自行调整）微信小游戏
         */
        ACTIVE(0),
        /**
         * 在调起微信小游戏后，且在联网环境下，在微信小游戏「完成创角」定义为注册事件
         */
        ACTIVE_REGISTER(1),
        /**
         * 在调起微信小游戏后，且在联网环境下，「发生交易并完成的每一笔付款」定义为付费事件，付费事件应包括首充及复充数据，并回传付费金额
         * 付费金额回传要求
         * props:{"pay_amount":xxx}
         * xxx单位为分，格式int
         */
        ACTIVE_PAY(2),
        /**
         * 常见的关键行为有：IPU次数、观看激励视频次数、游戏时长等。具体可参考文档游戏IAA专项_关键行为(api)_使用宝典：从入门到精通 [对外]
         * https://bytedance.feishu.cn/docs/doccn7kCEIE7oh4RtjoUK4cIiwd
         */
        GAME_ADDICTION(25),
        /**
         * 用户激活后次日在互联网环境下打开微信小游戏
         */
        NEXT_DAY_OPEN(6);

        private final Integer eventType;

        EventType(Integer eventType) {
            this.eventType = eventType;
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class MiniGameCallbackRequest {
        private String clue_token;

        private String open_id;

        private String union_id;

        private String event_type;

        private MiniGameCallbackRequestProps props;

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        @Builder
        public static class MiniGameCallbackRequestProps {

            private Long pay_amount;
        }
    }
}
