package org.springblade.mpush.controller;

import com.alibaba.fastjson.JSONObject;
import com.mpush.api.push.*;
import com.mpush.tools.log.Logs;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.mpush.pojo.vo.NotificationVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 首页
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/message")
@Tag(name = "消息推送", description = "消息推送接口")
public class MpushController {

	@GetMapping("/push")
	public void push() throws Exception {
		Logs.init();
		PushSender sender = PushSender.create();
		sender.start().join();
		Thread.sleep(1000);

		NotificationVO NotificationVO = new NotificationVO();
		NotificationVO.setContent("当前时间" + new Date());

		for (int i = 0; i < 1; i++) {
			PushMsg msg = PushMsg.build(MsgType.MESSAGE, JSONObject.toJSONString(NotificationVO));
			msg.setMsgId("msgId_" + i);

			PushContext context = PushContext.build(msg)
				.setAckModel(AckModel.AUTO_ACK)
				.setUserId("user-" + i)
				.setBroadcast(false)
				//.setTags(Sets.newHashSet("test"))
				//.setCondition("tags&&tags.indexOf('test')!=-1")
				//.setUserIds(Arrays.asList("user-0", "user-1"))
				.setTimeout(2000)
				.setCallback(new PushCallback() {
					@Override
					public void onResult(PushResult result) {
						System.err.println("\n\n" + result);
					}
				});
			FutureTask<PushResult> future = sender.send(context);

			System.err.println("\n\n" + future.get());
		}

		LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30));
	}
}
