package cn.ibizlab.codegen.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.ibizsys.model.service.IPSSubSysServiceAPI;
import net.ibizsys.model.system.IPSSystemModule;

import java.util.*;

@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class ClientModel extends BaseModel {

    private SystemModel system;

    private Map<String, ClientEntityModel> clientEntityMap = new LinkedHashMap<>();

    private Map<String, ClientEntityRSModel> clientEntityRSMap = new LinkedHashMap<>();

    public Collection<ClientEntityRSModel> getClientEntityRSes() {
        return clientEntityRSMap.values();
    }

    public ClientEntityRSModel getClientEntityRS(String name) {
        return clientEntityRSMap.get(name);
    }


    private Map<String, List<ClientEntityRSModel>> clientEntityParentRSMap = new LinkedHashMap<>();

    public List<ClientEntityRSModel> getApiEntityParentRSes(String name) {
        return clientEntityParentRSMap.get(name);
    }

    public String service;

    public ClientModel(IPSSubSysServiceAPI iPSSubSysServiceAPI) {
        this.opt = iPSSubSysServiceAPI;
        this.setCodeName(iPSSubSysServiceAPI.getCodeName());
        this.setName(iPSSubSysServiceAPI.getName());
        if (getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDEs() != null) {
            getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDEs().forEach(item -> {
                clientEntityMap.put(item.getCodeName(), new ClientEntityModel(this, item));
            });
        }
        service = getPSSubSysServiceAPI().getServiceCodeName().replace("_","-");
        if (getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDERSs() != null) {
            getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDERSs().forEach(item -> {
                ClientEntityRSModel clientEntityRSModel = new ClientEntityRSModel(this, item);
                clientEntityRSMap.put(item.getName(), clientEntityRSModel);
                if (!clientEntityParentRSMap.containsKey(clientEntityRSModel.getMinorEntityCodeName())) {
                    clientEntityParentRSMap.put(clientEntityRSModel.getMinorEntityCodeName(), new ArrayList<>());
                }
                clientEntityParentRSMap.get(clientEntityRSModel.getMinorEntityCodeName()).add(clientEntityRSModel);
            });
        }
    }

    public Collection<ClientEntityModel> getClientEntities() {
        return clientEntityMap.values();
    }

    public ClientEntityModel getClientEntity(String codeName) {
        return clientEntityMap.get(codeName);
    }

    public IPSSubSysServiceAPI getPSSubSysServiceAPI() {
        return (IPSSubSysServiceAPI)opt;
    }

    public String module;

    public String getModule()
    {
        if(module==null)
        {
            IPSSystemModule systemModule = getPSSubSysServiceAPI().getPSSystemModule();
            if(systemModule!=null&&systemModule.getCodeName()!=null&&systemModule.getPSSysModelGroup() == null) {
                module = getPSSubSysServiceAPI().getPSSystemModule().getCodeName().toLowerCase();
                if("rt".equals(module))
                    module="";
            }
            else
                module="";
        }
        return module;
    }

}
