
%% generate_pvs_file
%   function will create a pvs file representing the table stored
%   in data.
% inputs:
%   object:PVS_checker - PVS_checker object
%   filename:string - string representing file name excluding
%   extension
% outputs;
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function error = generate_pvs_file(object,filename)
% type_mode = 0 -> use pvs types
% type_mode = 1 -> use simulink typing information

error = 0;

if object.type_mode
    if size(object.output_type,2) == 2     
        object.data.function_name = PVS_checker.replace_types_sim(object.data.function_name,object.output_type{2});
    end
end
function_names = EMLGenerator.parse_inputs(object.data.function_name);
function_name = char(function_names{1}(1));

% make sure that function name is valid
valid = regexp(function_name,'[a-zA-Z][_a-zA-Z0-9]*','match');
if ~strcmp(function_name,valid)
    msgbox(['function name is not valid PVS syntax' char(10) 'proper syntax for PVS Id is [a-zA-Z][_a-zA-Z0-9]*']);
    error = 1;
    return;
end


% setup 1 output vs multiple outputs
if object.data.multi_mode == 0
    
    if(object.type_mode)
        function_name_type = object.output_type{2};
    else
        
        if (size(function_names{1},2) == 2)
            function_name_type = char(function_names{1}(2));

        else
            function_name_type = 'real';
        end
    end
else
    output = [];
    for i=1:size(object.data.Grid1.cells,2)
        parsed_output = EMLGenerator.parse_inputs(strtrim(char(object.data.Grid1.cells(i).cond_text)));
        % if no specified output type
        output = [output char(parsed_output{1}(1))];
        %%%%%%%%%%
        if object.type_mode
            for j=1:size(object.output_type,2)
                if strcmp(char(parsed_output{1}(1)),object.output_type{j,1})
                    if size(parsed_output{1},2) == 1
                        output = [output ':' object.output_type{j,2}];
                    else
                        output = [output  PVS_checker.replace_types_sim([':' char(parsed_output{1}(2))],object.output_type{j,2})];
                    end
                end
            end
        else
            
        
        %%%%%%%%%    
            if size(parsed_output{1},2) == 1
                output = [output ':real'];
            else
                output = [output ':' char(parsed_output{1}(2))];
            end
        end
        
        if i ~= size(object.data.Grid1.cells,2)
            output = [output ','];
        end
    end
    function_name_type = ['[# ' output ' #]'];
end


fileid = fopen([filename '.pvs'],'w');
code = [];
code = sprintf('%s:THEORY\nBEGIN\n',function_name);
%code = [code sprintf('IMPORTING Funcs\n')];
imports = object.pvs_check_for_imports;
if ~isempty(imports)
    
    if ~isempty(regexp(imports,'double|single', 'once'))
        % flag to copy files
        error = 2;
    end
end
code = [code imports];



code = [code object.user_imports];
if object.type_mode
    code = [code 'IMPORTING matlab_types\n'];
end
% declare variables for each input


%if object.type_mode == 0
    %inputs = get(object.gui.function_inputs_control,'String');
    %inputs = regexp(inputs,',','split');
    parsed_inputs = EMLGenerator.parse_inputs(object.data.function_inputs);
    
    % add inputs to list of inputs to function
    inputs_new = [];
    for i=1:size(parsed_inputs,2)
        % set to zero
        if object.type_mode
            for j=1:2:size(object.input_type,2)
                if strcmp(char(parsed_inputs{i}(1)),object.input_type{j,1})
                    if(size(parsed_inputs{i},2) == 2)
                        inputs_new = [inputs_new char(parsed_inputs{i}(1)) PVS_checker.replace_types_sim([':' char(parsed_inputs{1}(2))],object.input_type{j,2})];
                    else
                        inputs_new = [inputs_new char(parsed_inputs{i}(1)) ':' object.input_type{j,2}];
                    end
                end
            end
            
        else
            if(size(parsed_inputs{i},2) == 2)
                inputs_new = [inputs_new char(parsed_inputs{i}(1)) ':' char(parsed_inputs{i}(2))];
            else
                inputs_new = [inputs_new char(parsed_inputs{i}(1)) ':' object.default_type];
            end
        end
        
        if(i~=(size(parsed_inputs,2)))
            inputs_new = [inputs_new ','];
        end
    end
    code = [code sprintf('%s(%s):%s = \n',function_name,inputs_new,function_name_type)];
    

code = [code object.generate_pvs(object.data.Grid1,object.data.Grid2,0)];
code = [code sprintf('\nEND %s',function_name)];
fprintf(fileid,code);

fclose(fileid);

end


