function [success, errorMsg] = checkParamFile(fullFile, option)
% checkParamFile checks model paramters
% checkParamFile chekcs if the paramters of the IMPACT model file are
% correct and assign the paramters to the caller workspace

%   Project: IMPACT
%   Title of Code: checkParamFile
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.


errorMsg= '';
success = 1;

if ~exist(fullFile,'file')
    errorMsg = sprintf('The file %s. does not exist\n', fullFile);
    success = -1;
    return;
end


fid = fopen(fullFile);
if fid == -1
    errorMsg = sprintf('File %s cannot be open for reading', fullFile);
    success = -1;
    return;
end
m = 1;
while 1
    tline = fgetl(fid);
    if ~ischar(tline)
        break
    end
    try
        eval(tline);
    catch
        errorMsg = sprintf('Error occurred when reading line %d: %s. ', m, tline);
        success = -1;
        fclose(fid);
        return
    end
    m = m+1;
end
fclose(fid);


if ~exist('iWecType','var')
    errorMsg = sprintf('%sError reding the model file. WEC type cannot be found in the model\n',errorMsg);
    success = -1;
    return;
end
    

if iWecType == 1 || iWecType ==2
    chkArray{1} = {'rFloaterMass', 'rFloaterIx', 'rFloaterIy', 'rFloaterIz', 'rAddedDampingBody1', 'rFloaterVolume',... % Floater parameters
        'rGRAVITY', 'rDENSITY'}; % Environmenatal parameters
    chkArray{2} = {'rCogFloater'};
else
    chkArray{1} = {'rFloaterMass', 'rFloaterIx', 'rFloaterIy', 'rFloaterIz', 'rAddedDampingBody1', 'rFloaterVolume',... % Floater parameters
        'rSparMass',  'rSparIx', 'rSparIy', 'rSparIz', 'rAddedDampingBody2', 'rSparVolume'... % Spar parameters
        'rMooringX', 'rMooringY', 'rMooringZ', ... % Mooring parameters
        'rGRAVITY', 'rDENSITY',... % Environmenatal parameters
        'rTetherSpring','rTetherPretension','rTetherLength0'}; % Tether Paramters
    % Values that can be negative
    chkArray{2} = {'rCogFloater','rCogSpar'};
    
    chkArray{3} = {'iBodyToBodyInteraction'};
end

   


% check if all parameters exist
for ind = 1:numel(chkArray)
    chkArrayTemp = chkArray{ind};
    for m = 1:numel(chkArrayTemp)
        if ~exist(chkArrayTemp{m}, 'var')
            success = 0;
        end
    end
end

if ~success
    errorMsg = sprintf('%sVariables are missing in the paramteres file\n',errorMsg);
    return;
end


% positve values
for m = 1:numel(chkArray{1})
    temp_var = eval([chkArray{1}{m}, ';']);
    if ~isscalar(temp_var) || temp_var<0 || ~isfinite(temp_var)
        errorMsg = sprintf('%sSome of the parameters in the file are wrong. Please check\n',errorMsg);
        success = 0;
        return
    end
end
% Negative values
for m = 1:numel(chkArray{2})
    temp_var = eval([chkArray{2}{m}, ';']);
    if ~isscalar(temp_var) || ~isfinite(temp_var)
        errorMsg = sprintf('%sSome of the parameters in the file are wrong. Please check\n',errorMsg);
        success = 0;
        return
    end
end

% values 0 or 1
if iWecType == 3 || iWecType ==4
    for m = 1:numel(chkArray{3})
        temp_var = eval([chkArray{3}{m}, ';']);
        if ~isscalar(temp_var) || (temp_var~=0 && temp_var~=1) || ~isfinite(temp_var)
            errorMsg = sprintf('%sBody to body interaction can only be 0 or 1. Please check\n',errorMsg);
            success = 0;
            return
        end
    end
end

% H5 File
if ~exist(stH5File,'file')
    errorMsg = sprintf('%sBEM file %s cannot be found\n',errorMsg,stH5File);
    success = 0;
    return
else
    try
        if(iWecType==1 || iWecType==2)
            h5FileData = getParamH5(stH5File,1);
        else
            h5FileData = getParamH5(stH5File,2);
        end
    catch
        errorMsg = sprintf('%sBEM data cannot be read\n',errorMsg);
        success = 0;
        return
    end
end


% PTO transfer function
if ~isa(tfPto, 'tf') ||isequal(tfPto, tf(nan))
    errorMsg = sprintf('%sPTO transfer function is not right\n',errorMsg);
    success = 0;
    return
end

% copy variables into a different workspace
if nargin>1
    switch option
        case 'caller'
            varCell = who;
            for m=1:numel(varCell)
                str = varCell{m};
                assignin('caller', str, eval(str));
            end
    end
end

errorMsg = 0;


