package cn.ibizlab.codegen.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.ibizsys.model.dataentity.service.IPSDEServiceAPIMethod;
import net.ibizsys.model.dataentity.service.IPSDEServiceAPIRS;
import net.ibizsys.model.service.IPSSubSysServiceAPIDE;
import net.ibizsys.model.service.IPSSubSysServiceAPIDEMethod;
import net.ibizsys.model.service.IPSSubSysServiceAPIDERS;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

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

    private ClientModel client;

    public ClientEntityModel(ClientModel clientModel, IPSSubSysServiceAPIDE iPSSubSysServiceAPIDE) {
        this.opt = iPSSubSysServiceAPIDE;
        this.client = clientModel;
        this.setCodeName(iPSSubSysServiceAPIDE.getCodeName());
        this.setName(iPSSubSysServiceAPIDE.getName());
        this.setId(String.format("%1$s-%2$s", client.getCodeName(), iPSSubSysServiceAPIDE.getCodeName()));
    }

    public IPSSubSysServiceAPIDE getPSSubSysServiceAPIDE() {
        return (IPSSubSysServiceAPIDE) opt;
    }

    public EntityModel getEntity() {
        return this.getClient().getSystem().getEntity(getCodeName());
    }

    private String apiName;

    public String getApiName() {
        if (apiName == null)
            this.apiName = ObjectUtils.isEmpty(getPSSubSysServiceAPIDE().getLogicName()) ? getEntity().getLogicName() : getPSSubSysServiceAPIDE().getLogicName();
        return apiName;
    }


    public boolean isMajor() {
        return getPSSubSysServiceAPIDE().isMajor();
    }


    public List<ClientMethodModel> getMethods() {
        List<ClientMethodModel> methods = new ArrayList<>();
        //主接口方法
        if (getPSSubSysServiceAPIDE().isMajor() && getPSSubSysServiceAPIDE().getPSSubSysServiceAPIDEMethods() != null) {
            for (IPSSubSysServiceAPIDEMethod iPSSubSysServiceAPIDEMethod : getPSSubSysServiceAPIDE().getPSSubSysServiceAPIDEMethods()) {
                methods.add(new ClientMethodModel(this, null, iPSSubSysServiceAPIDEMethod));
            }
        }
        //接口关系方法
//        if (this.getClient().getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDERSs() != null) {
//            for (IPSSubSysServiceAPIDERS iPSSubSysServiceAPIDERS : this.getClient().getPSSubSysServiceAPI().getAllPSSubSysServiceAPIDERSs()) {
//                if(!iPSSubSysServiceAPIDERS.getMinorPSSubSysServiceAPIDE().getId().equalsIgnoreCase(this.getPSSubSysServiceAPIDE().getId()))
//                    continue;
//                if (iPSSubSysServiceAPIDERS.getPSDEServiceAPIMethods() != null) {
//                    ApiEntityRSModel apiEntityRSModel = api.getApiEntityRS(iPSDEServiceAPIRS.getName());
//                    //计算父路径
//                    List<List<ApiEntityRSModel>> parentApiEntityList = new ArrayList<>();
//                    fillDEAPIRSPath(parentApiEntityList, apiEntityRSModel, null);
//                    for (List<ApiEntityRSModel> parentApiEntities : parentApiEntityList) {
//                        for (IPSDEServiceAPIMethod iPSDEServiceAPIMethod : iPSDEServiceAPIRS.getPSDEServiceAPIMethods()) {
//                            if(iPSDEServiceAPIMethod.getPSDEDataSet()!=null && iPSDEServiceAPIMethod.getPSDEDataSet().getPSDEDataQueries()==null)
//                                continue ;
//                            methods.add(new ApiMethodModel(this, parentApiEntities, iPSDEServiceAPIMethod));
//                        }
//                    }
//                }
//            }
//        }
        return methods;
    }

    /**
     * 递归计算接口关系path
     * 注意自身关系
     *
     * @param parentClientEntityList
     * @param clientEntityRSModel
     * @param parentClientEntities
     */
    private void fillDEAPIRSPath(List<List<ClientEntityRSModel>> parentClientEntityList, ClientEntityRSModel clientEntityRSModel, List<ClientEntityRSModel> parentClientEntities) {
        if (parentClientEntities == null) {
            parentClientEntities = new ArrayList<>();
        }
        //防止递归
        if (parentClientEntities.stream().anyMatch(rs -> rs.getName().equals(clientEntityRSModel.getName()))) {
            return;
        }
        parentClientEntities.add(clientEntityRSModel);
        if (clientEntityRSModel.isMajorEntityMajor()) {
            List<ClientEntityRSModel> temp = new ArrayList<>();
            temp.addAll(parentClientEntities);
            parentClientEntityList.add(temp);
        }
        //递归
//        List<ApiEntityRSModel> parents = api.getApiEntityParentRSes(apiEntityRSModel.getMajorEntityCodeName());
        List<ClientEntityRSModel> parents = new ArrayList<>();
        if (!CollectionUtils.isEmpty(parents)) {
            for (ClientEntityRSModel parentRs : parents) {
                List<ClientEntityRSModel> tempParentApiEntities = new ArrayList<>();
                tempParentApiEntities.addAll(parentClientEntities);
                fillDEAPIRSPath(parentClientEntityList, parentRs, tempParentApiEntities);
            }
        }
    }

}
