package com.purui.prims.headless.api.pojo.response;


import com.google.common.collect.Lists;
import com.purui.prims.common.pojo.RecordInfo;
import com.purui.prims.headless.api.pojo.AppAuthConfig;
import com.purui.prims.headless.api.pojo.AppAuthItem;
import com.purui.prims.headless.api.pojo.enums.AppStatus;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
public class AppResp extends RecordInfo {

    private Integer id;

    private String name;

    private String description;

    private AppStatus appStatus;

    private AppAuthConfig config;

    private Date endDate;

    private Integer qps;

    private List<String> owners;

    private boolean hasAdminRes;

    public void setOwner(String owner) {
        if (StringUtils.isBlank(owner)) {
            owners = Lists.newArrayList();
            return;
        }
        owners = Arrays.asList(owner.split(","));
    }

    public Set<AppAuthItem> allItems() {
        Set<AppAuthItem> itemSet = new HashSet<>();
        for (AppAuthItem item : config.getItems()) {
            itemSet.add(item);
            itemSet.addAll(item.getRelateItems());
        }
        return itemSet;
    }

}
