package cn.axzo.msg.center.message.service.impl.v3;

import cn.axzo.msg.center.service.domain.UrlConfigVisitor;
import cn.axzo.msg.center.service.domain.url.AppUrl;
import cn.axzo.msg.center.service.enums.TerminalTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yanglin
 */
@Getter
public class NativeAppLinkUrlConfigVisitor implements UrlConfigVisitor {

    private final List<AppLink> links = new ArrayList<>();
    private final Set<TerminalTypeEnum> terminals = new HashSet<>();

    @Override
    public void visitAppManagerAndroid(AppUrl android) {
        if (android.hasUrl())
            addLink(new AppLink(TerminalTypeEnum.ANDROID, android.getUrl()));
    }

    @Override
    public void visitAppManagerIos(AppUrl ios) {
        if (ios.hasUrl())
            addLink(new AppLink(TerminalTypeEnum.IOS, ios.getUrl()));
    }

    @Override
    public void visitAppWorkerAndroid(AppUrl android) {
        if (android.hasUrl())
            addLink(new AppLink(TerminalTypeEnum.ANDROID, android.getUrl()));
    }

    @Override
    public void visitAppWorkerIos(AppUrl ios) {
        if (ios.hasUrl())
            addLink(new AppLink(TerminalTypeEnum.IOS, ios.getUrl()));
    }

    private void addLink(AppLink link) {
        if (!terminals.contains(link.getPlatform())) {
            links.add(link);
            terminals.add(link.getPlatform());
        }
    }
}