function sucess = setParams(iWecType,filename, filepath, hObject,handles)
% setParams 
%   setParams sets model parameters in GUI

%   Project: IMPACT
%   Title of Code: setParams
%   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.

fullFile = fullfile(filepath, filename);
% return the filename and filepath for the parameter file
hParam = paramInitial(iWecType, filename, filepath, fullFile);
param = guidata( hParam ); % the handle to the current figure
sucess = 1;

if ~exist(fullfile(filepath, filename),'file')
    success = -1;
    return;
end

try
    readFile(fullFile);
catch
    sucess = 0;
end

if ~exist('iWecType','var')
    success = -1;
    return;
end


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

for m = 1:numel(chkArray)
    if exist(chkArray{m}, 'var')
        eval(sprintf('set(param.edit_%s, ''String'', %s);', chkArray{m}, chkArray{m}));
    end
end


%%

% Body-Body interaction
if exist('iBodyToBodyInteraction','var')
    if iBodyToBodyInteraction
        set(param.radiobutton_b2b,'Value',1);
    else
        set(param.radiobutton_b2b,'Value',0);
    end
    success = 0;
end

% H5 file
if exist('stH5File','var')
    [folder, file , ext]=fileparts(stH5File);
    fileName = sprintf('%s%s',file,ext);
    set(param.text_h5File, {'String','toolTipString'}, {fileName,folder});
    param.h5FilePath = folder;
    param.h5FileName = fileName;
else
    success = 0;
end


% PTO transfer function
if exist('tfPto','var') && isa(tfPto, 'tf') && ~isequal(tfPto, tf(nan))
    [arPtoNum, arPtoDen] = tfdata(tfPto,'v');
    
    stPtoNum = '[';
    for iCont = 1:numel(arPtoNum)
        stPtoNum =sprintf('%s %1.3f',stPtoNum,arPtoNum(iCont));
    end
    stPtoNum =sprintf('%s]',stPtoNum);
    if strcmp(stPtoNum, '[]')
        stPtoNum = '[1]';
    end
    
    stPtoDen = '[';
    for iCont = 1:numel(arPtoDen)
        stPtoDen =sprintf('%s %1.3f',stPtoDen,arPtoDen(iCont));
    end
    stPtoDen =sprintf('%s]',stPtoDen);
    if strcmp(stPtoDen, '[]')
        stPtoDen = '[1]';
    end
    stPto = sprintf('tf(%s,%s)',stPtoNum,stPtoDen);
    
    set(param.edit_tfPto,'String',stPto);
else
    success = 0;
end


guidata(param.MLS_paramSetting,param);


handles.allFilepath = filepath;
guidata(hObject, handles);
