package com.galaxis.manatee.manager.impl;

import com.galaxis.manatee.entity.ding.DingTalkRobot;
import com.galaxis.manatee.entity.ding.DingTalkRobotRequest;
import com.galaxis.manatee.entity.ding.DingTalkRobotResponse;
import com.galaxis.manatee.manager.DingTalkRobotWebHook;
import com.galaxis.manatee.util.DingTalkRobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author zcj
 * @version 0.1
 * @date 2020/1/9 9:40 上午
 */
@Slf4j
@Service
public class DingTalkRobotWebHookImpl implements DingTalkRobotWebHook {
    @Override
    public DingTalkRobotResponse invoke(DingTalkRobotRequest dingTalkRobotRequest, DingTalkRobot dingTalkRobot) throws Exception {
        RestTemplate restTemplate=new RestTemplate();
        Long timeStamp=System.currentTimeMillis();
        if(dingTalkRobot.getEncryptionMethod()==DingTalkRobot.EncryptionMethod.SECRET){
            return restTemplate.postForObject(dingTalkRobot.getUrl(),dingTalkRobotRequest,DingTalkRobotResponse.class,timeStamp.toString(),DingTalkRobotUtils.sign(dingTalkRobot.getEncryption(),timeStamp));
        }else {
            return restTemplate.postForObject(dingTalkRobot.getUrl(),dingTalkRobotRequest,DingTalkRobotResponse.class);
        }
//        return switch(dingTalkRobot.getEncryptionMethod()){
//            case SECRET-> restTemplate.postForObject(dingTalkRobot.getUrl(),dingTalkRobotRequest,DingTalkRobotResponse.class,timeStamp.toString(),DingTalkRobotUtils.sign(dingTalkRobot.getEncryption(),timeStamp));
//            default -> restTemplate.postForObject(dingTalkRobot.getUrl(),dingTalkRobotRequest,DingTalkRobotResponse.class);
//        };
    }
}
