package com.lanyuanxiaoyao.service.command.provider;

import com.lanyuanxiaoyao.service.forest.service.InfoService;
import java.util.List;
import java.util.Optional;
import org.springframework.core.MethodParameter;
import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;
import org.springframework.stereotype.Component;

/**
 * Table name 待选项
 *
 * @author ZhangJiacheng
 * @date 2022-05-30
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Component
public class AliasProvider extends BaseValueProvider {
    private final InfoService infoService;

    public AliasProvider(InfoService infoService) {
        this.infoService = infoService;
    }

    @Override
    public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
        Optional<String> field = getField(completionContext.getWords(), "--flink-job-id");
        return field.map(id -> infoService.simpleTableMetas(Long.parseLong(id))
                        .collect(info -> new CompletionProposal(info.getAlias()).description(info.getSchema()))
                        .toList())
                .orElseGet(() -> infoService.allAlias()
                        .collect(CompletionProposal::new)
                        .toList());
    }
}
