clear
addpath(genpath('../../cellLib/Scripts/'))
load("comp.mat")
% Build models
cmnames=string(extractfield(comp,'name'));
modelnames=cmnames([7,10:14,24]);
listComp={};
labels=cell(length(modelnames),1);
txtfiles=[];
for i=1:length(modelnames)
    modelname=modelnames(i);
    listComp.name=modelname;
    listComp.def=modelname;
    listComp.from="";
    listComp.impComp="";
    listComp.label=labels{i,1};
    model=buildModel(modelname,comp,listComp,idx);
    modelfile=strcat(modelname,'.txt');
    txtfiles=[txtfiles,modelname];
    writeModel(model,comp,modelfile,idx)
end
libPath2="../SinglePASMC/Components/";
modelname="SMC";
parentnames=modelname;
nm=0;
listComp={};
labels={};
chd=[];
for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=[chd;icomp.children];
end
uni_chd=unique(chd);

for m=1:length(uni_chd)
    ichd=uni_chd(m);
    listComp(m).name=ichd;
    listComp(m).def=ichd;
    if ismember(ichd,["JNKAi","JKi","JCli","IP3","Jdegradi","JPLCdeltai","JIP3i","Cai"])
        listComp(m).from=strcat(ichd,".cellml");
    else
        listComp(m).from=strcat(libPath2,ichd,".cellml");
    end
    listComp(m).impComp=ichd;
    listComp(m).label=[];
end

for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=icomp.children;
    m=m+1;
    listComp(m).name=parentnames(n);
    listComp(m).def=parentnames(n);
    listComp(m).from="";
    listComp(m).impComp="";
    listComp(m).label=[];
end
model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelname];
writeModel(model,comp,modelfile,idx)

modelname="JKi";
parentnames=modelname;
nm=0;
listComp={};
labels={};
chd=[];
for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=[chd;icomp.children];
end
uni_chd=unique(chd);

for m=1:length(uni_chd)
    ichd=uni_chd(m);
    listComp(m).name=ichd;
    listComp(m).def=ichd;
    listComp(m).from="../cellLib/Components/E_Nernst.cellml";
    listComp(m).impComp=ichd;
    listComp(m).label=["E_K","v_K";];
end

for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=icomp.children;
    m=m+1;
    listComp(m).name=parentnames(n);
    listComp(m).def=parentnames(n);
    listComp(m).from="";
    listComp(m).impComp="";
    listComp(m).label=[];
end
model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelname];
writeModel(model,comp,modelfile,idx)

modelname="JCli";
parentnames=modelname;
nm=0;
listComp={};
labels={};
chd=[];
for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=[chd;icomp.children];
end
uni_chd=unique(chd);

for m=1:length(uni_chd)
    ichd=uni_chd(m);
    listComp(m).name=ichd;
    listComp(m).def=ichd;
    listComp(m).from="../cellLib/Components/E_Nernst.cellml";
    listComp(m).impComp=ichd;
    listComp(m).label=["E_Cl","v_Cl";];
end

for n=1:length(parentnames)
    icomp=comp(cmnames==parentnames(n));
    chd=icomp.children;
    m=m+1;
    listComp(m).name=parentnames(n);
    listComp(m).def=parentnames(n);
    listComp(m).from="";
    listComp(m).impComp="";
    listComp(m).label=[];
end
model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelname];
writeModel(model,comp,modelfile,idx)

modelname="SMC_KCl_noise";
listComp={};
m=0;
m=m+1;
listComp(m).name="SMC";
listComp(m).def="SMC";
listComp(m).from="../Components/SMC.cellml";
listComp(m).impComp="SMC";
listComp(m).label=[];
m=m+1;
listComp(m).name="output";
listComp(m).def="output";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
m=m+1;
listComp(m).name="clamp_para";
listComp(m).def="clamp_para";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=["KCl","Ko"];
m=m+1;
listComp(m).name="free_para";
listComp(m).def="free_para";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
m=m+1;
listComp(m).name="noise_G";
listComp(m).def="noise_G";
listComp(m).from="";
listComp(m).impComp="";
listComp(m).label=[];
m=m+1;
listComp(m).name="Para";
listComp(m).def="Para";
listComp(m).from="../Components/Para.cellml";
listComp(m).impComp="Para";
listComp(m).label=["G_Ca","G_Ca_0";"G_NCX","G_NCX_0";"G_Cl","G_Cl_0";"G_Ki","G_K_0";];
m=m+1;
listComp(m).name="X_clamp_protocol";
listComp(m).def="X_clamp_protocol";
listComp(m).from="../cellLib/Protocols/X_clamp_protocol.cellml";
listComp(m).impComp="X_clamp_protocol";
listComp(m).label=["time","t";];
m=m+1;
listComp(m).name="time";
listComp(m).def="time_s";
listComp(m).from="../cellLib/Components/time.cellml";
listComp(m).impComp="time_s";
listComp(m).label=["time","t";];

model=buildModel(modelname,comp,listComp,idx);
modelfile=strcat(modelname,'.txt');
txtfiles=[txtfiles,modelname];
writeModel(model,comp,modelfile,idx)

% write the filenames for conversion
txtfiles=[txtfiles,modelname];
fileID = fopen('filenames.txt','w');
fprintf(fileID,'"%s",',txtfiles');
fclose(fileID);