package org.tio.sitexxx.service.service.jpush.honor.android;

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang.StringUtils;
import org.tio.sitexxx.service.service.jpush.honor.message.Notification;
import org.tio.sitexxx.service.service.jpush.huawei.util.ValidatorUtils;


/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-02-26 16:00
 *    desc   : 从
 * </pre>
 */
public class AndroidNotification {
    private static final String URL_PATTERN = "^https.*";

    @JSONField(name = "title")
    private String title;

    @JSONField(name = "body")
    private String body;

    @JSONField(name = "clickAction")
    private ClickAction clickAction;

    @JSONField(name = "image")
    private String image;

    @JSONField(name = "badge")
    private BadgeNotification badge;

    @JSONField(name = "notify_id")
    private Integer notifyId;

    private AndroidNotification(AndroidNotification.Builder builder) {
        this.title = builder.title;
        this.body = builder.body;

        this.clickAction = builder.clickAction;

        this.image = builder.image;
        this.notifyId = builder.notifyId;


        if (null != builder.badge) {
            this.badge = builder.badge;
        } else {
            this.badge = null;
        }
    }

    /**
     * check androidNotification's parameters
     *
     * @param notification which is in message
     */
    public void check(Notification notification) {
        if (null != notification) {
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getTitle()) || StringUtils.isNotEmpty(this.title), "title should be set");
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getBody()) || StringUtils.isNotEmpty(this.body), "body should be set");
        }

        if (this.clickAction != null) {
            this.clickAction.check();
        }

        if (StringUtils.isNotEmpty(this.image)) {
            ValidatorUtils.checkArgument(this.image.matches(URL_PATTERN), "notifyIcon must start with https");
        }

        if (badge != null) {
            this.badge.check();
        }
    }

    /**
     * getter
     */
    public String getTitle() {
        return title;
    }

    public String getBody() {
        return body;
    }

    public ClickAction getClickAction() {
        return clickAction;
    }

    public String getImage() {
        return image;
    }

    public Integer getNotifyId() {
        return notifyId;
    }

    public BadgeNotification getBadge() {
        return badge;
    }

    /**
     * builder
     *
     * @return
     */
    public static AndroidNotification.Builder builder() {
        return new AndroidNotification.Builder();
    }

    public static class Builder {

        private String title;

        private String body;

        private ClickAction clickAction;

        private String image;

        private Integer notifyId;

        private BadgeNotification badge;

        private Builder() {
        }

        public AndroidNotification.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public AndroidNotification.Builder setBody(String body) {
            this.body = body;
            return this;
        }

        public AndroidNotification.Builder setClickAction(ClickAction clickAction) {
            this.clickAction = clickAction;
            return this;
        }

        public AndroidNotification.Builder setImage(String image) {
            this.image = image;
            return this;
        }

        public AndroidNotification.Builder setNotifyId(Integer notifyId) {
            this.notifyId = notifyId;
            return this;
        }

        public AndroidNotification.Builder setBadge(BadgeNotification badge) {
            this.badge = badge;
            return this;
        }

        public AndroidNotification build() {
            return new AndroidNotification(this);
        }
    }
}
