clear
addpath(genpath('../cellLib/Scripts/'))
file='Vars.csv';
txtfiles=[];
% build new components from the .csv
comp_dir=pwd;
[comp,idx]=newComp([comp_dir filesep file]);
% copy the existing components if need
% read the existing model and get the existing components
%comp=copyComp(comp,listComp,idx);

% Encapsuate new components
encap=["PASMC"];
chdname=["JVOCCi","JNCXi","JSERCAi","JCICRi","JPMCAi","Jleaki","JNKAi","JCli","JKi","Casr","Cai","Vm"];
labels=cell(length(chdname),1);
chd={};
for i=1:length(chdname)
    chd(i).name= chdname(i);
    chd(i).def= chdname(i);
    chd(i).label=labels{i,1};
end
comp=encapNew(comp,encap,chd,idx);

% Add the protocol and time
filename='../cellLib/Protocols/X_clamp_protocol.cellml';
compRead=readComp(filename);
comp=[comp,compRead];
filename='../cellLib/Components/time.cellml';
compRead=readComp(filename);
comp=[comp,compRead];
comp=getPara(comp,idx);
comp=updateVIO(comp,idx);
% Add equations to components
Eqs=["J_VOCCi=G_Ca*((v_Ca1-v_i)/(1{dimensionless}+exp((v_Ca2-v_i)/R_Ca)));",...
    "J_NCXi=G_NCX*(c_i/(c_i+c_NCX))*(v_NCX-v_i);",...
    "J_SERCAi=B*(pow(c_i,2{dimensionless})/(pow(c_i,2{dimensionless})+pow(c_b,2{dimensionless})));",...
    "J_CICRi=C*(pow(s_i,2{dimensionless})/(pow(s_i,2{dimensionless})+pow(s_c,2{dimensionless})))*(pow(c_i,4{dimensionless})/(pow(c_i,4{dimensionless})+pow(c_c,4{dimensionless})));",...
    "J_PMCAi=D*c_i*(1{dimensionless}+(v_i-v_d)/(R_d));",...
    "J_leaki=L*s_i;",...
    "J_NKAi=C_NKA*(K_out/(K_mK+K_out));",...
    "J_Cli=G_Cl*(v_i-v_Cl);",...
    "J_Ki=G_Ki*w_i*(v_i-v_K);",...
    "ode(w_i,t)=lambda*(K_activationi-w_i);",...
    "K_activationi = sqr(c_i+c_w)/(sqr(c_i+c_w)+beta*exp(-(v_i-v_Ca3)/R_K));",...
    "ode(c_i,t)=J_VOCCi-J_NCXi-J_SERCAi+J_CICRi-J_PMCAi+J_leaki;",...
    "ode(s_i,t)=J_SERCAi-J_CICRi-J_leaki;",...
    "ode(v_i,t)=gamma*(-J_NKAi-J_Cli+2{dimensionless}*J_VOCCi+J_NCXi-J_Ki);"];

for i=1:8
    comp(i).Eqs=Eqs(i);
end
comp(9).Eqs=Eqs(9:11);
for i=10:12
    comp(i).Eqs=Eqs(i+2);
end

save("comp.mat","comp")