addpath(genpath('../../cellLib/Scripts/'))
load("comp.mat")
% Build models
cmnames=string(extractfield(comp,'name'));
modelnames=cmnames([14]);
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

modelnames=cmnames([1:6]);
for mm = 1:length(modelnames)
    modelname=modelnames(mm);
    parentnames=modelname;
    listComp={};
    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=strcat("../cellLib/Components/MarkovS.cellml");
        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)
end
for N=[10, 18]
modelname=sprintf("MWC_%d",N);
parentnames=modelname;
listComp={};
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;
    
    if m==1
        s1="O_0";
        s2="C_1";
        si="C_0";
        dsi="dC_0";
        si_init="C0_init";
        ichd_def="C0_S2";
    end
    if m==N/2
        s1=sprintf("C_%d",m-2);
        s2=sprintf("O_%d",m-1);
        si=sprintf("C_%d",m-1);
        dsi=sprintf("dC_%d",m-1);
        si_init=sprintf("C%d_init",m-1);
        ichd_def="CN_S2";
    end
    if m==N/2+1
        s1="C_0";
        s2="O_1";
        si="O_0";
        dsi="dO_0";
        si_init="O0_init";
        ichd_def="O0_S2";
    end
    
    if m==N
        s1=sprintf("O_%d",m/2-2);
        s2=sprintf("C_%d",m/2-1);
        si=sprintf("O_%d",m/2-1);
        dsi=sprintf("dO_%d",m/2-1);
        si_init=sprintf("O%d_init",m/2-1);
        ichd_def="ON_S2";
    end
    if m>=2&&m<=N/2-1
        celli=m-1;
        i=sprintf("C%d_i",celli);
        s1=sprintf("C_%d",celli-1);
        s2=sprintf("O_%d",celli);
        s3=sprintf("C_%d",celli+1);
        si=sprintf("C_%d",celli);
        dsi=sprintf("dC_%d",celli);
        si_init=sprintf("C%d_init",celli);
        ichd_def="C_S3";
    end
   
    if m>=N/2+2&&m<=N-1
        celli=m-(N/2+1);
        i=sprintf("O%d_i",celli);
        s1=sprintf("O_%d",celli-1);
        s2=sprintf("C_%d",celli);
        s3=sprintf("O_%d",celli+1);
        si=sprintf("O_%d",celli);
        dsi=sprintf("dO_%d",celli);
        si_init=sprintf("O%d_init",celli);
        ichd_def="O_S3";
    end    
    if m==1 || m==N/2 || m==N/2+1 || m==N
        listComp(m).label=["S_1",s1;"S_2",s2;"S_i",si;"dS_i",dsi;"Si_init",si_init;"i",i];
    else
        listComp(m).label=["S_1",s1;"S_2",s2;"S_3",s3;"S_i",si;"dS_i",dsi;"Si_init",si_init;"i",i];
    end
    
    listComp(m).def=ichd_def;
    listComp(m).from=strcat(ichd_def,".cellml");
    listComp(m).impComp=ichd_def;
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)
end

for N=[10, 18]
modelname=sprintf("MWC_%d_test",N);
listComp={};
m=0;
m=m+1;
listComp(m).name=sprintf("MWC_%d",N);
listComp(m).def=sprintf("MWC_%d",N);
listComp(m).from=sprintf("../Components/MWC_%d.cellml",N);
listComp(m).impComp=sprintf("MWC_%d",N);
listComp(m).label=[];
m=m+1;
listComp(m).name=sprintf("output_%d",N);
listComp(m).def=sprintf("output_%d",N);
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=[];
m=m+1;
listComp(m).name=sprintf("free_para_%d",N);
listComp(m).def=sprintf("free_para_%d",N);
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=[];
m=m+1;
listComp(m).name="dPulse_protocol_ms";
listComp(m).def="dPulse_protocol_ms";
listComp(m).from="../cellLib/Protocols/Patch_clamp_protocol.cellml";
listComp(m).impComp="dPulse_protocol_ms";
listComp(m).label=["time","t";];
m=m+1;
listComp(m).name="time";
listComp(m).def="time_ms";
listComp(m).from="../cellLib/Components/time.cellml";
listComp(m).impComp="time_ms";
listComp(m).label=["time","t";];

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

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