package cn.axzo.msg.center.nimpush.payload;

import cn.axzo.msg.center.nimpush.PushContent;
import cn.axzo.msg.center.nimpush.PushPeer;
import cn.axzo.msg.center.nimpush.payload.intent.Intent;
import cn.axzo.msg.center.nimpush.payload.intent.IntentValue;
import cn.axzo.msg.center.nimpush.payload.intent.JsonIntent;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 苹果
 *
 * @author yanglin
 */
@Component
class PGPushPayloadBuilder implements PushPayloadBuilder<JsonIntent> {

    @Override
    public JsonIntent createIntent(PushContent content, PushPeer peer) {
        JsonIntent intent = new JsonIntent();
        if (StringUtils.isNotBlank(content.getIosPushUrl()))
            Intent.setRouter(intent, IntentValue
                    .create(content.getIosPushUrl()));
        return intent;
    }

    @Override
    public void build(PushContent content, JsonIntent intent, JSONObject payload) {
        JSONObject userInfo = new JSONObject();
        userInfo.putAll(intent.build());

        JSONObject alert = new JSONObject();
        alert.put("title", content.getTitle());
        alert.put("body", content.getContent());
        alert.put("userInfo", userInfo);

        JSONObject apsField = new JSONObject();
        apsField.put("alert", alert);

        payload.put("apsField", apsField);
    }

}