function [ data ] = ConnectMissingNodesToNeighborsNeighbors( data, actual_graph_size, num_missing_nodes, num_attr_nodes, addMissingAtt )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

        if nargin < 4
            num_attr_nodes = 0;
            addMissingAtt = 0;
            fprintf('run ConnectMissingNodesToNeighborsNeighbors without attributes params\n');
        end

        first_missing_node = actual_graph_size - num_missing_nodes + 1;
        
        % Sigal -  TOCHECK remove connection to attributes nodes
        if addMissingAtt > 0 % sigal 10.2.14 % == 1 % sigal 24.10.13 TODO val > 0
            first_node = 1; %% sigal test 6.2.13 num_attr_nodes+1;
            first_social_node = num_attr_nodes+1;
        else
            first_node = num_attr_nodes+1;
        end
        %last_node = first_missing_node-1;
        
        for i = first_missing_node : size(data,1)
            
            
            neighbor = find(data(i,:), 1 ); %the missing node could have more than one neighbor if in a previous iteration we added a neighbor to it. The min should be the original neighbor
            
            %fprintf('i = %d, neighbor = %d\n', i, neighbor);
            %sum(data(i,:))
            
            if size(neighbor,2) > 0  
                if addMissingAtt == 0 || num_attr_nodes == 0
                  data(i,first_node:end) = data(i,first_node:end) | data(neighbor, first_node:end);
                  data(first_node:end,i) = data(first_node:end,i) | data(first_node:end,neighbor);
                else 
                  % connect all friends
                  data(i,first_social_node:end) = data(i,first_social_node:end) | data(neighbor, first_social_node:end);
                  data(first_social_node:end,i) = data(first_social_node:end,i) | data(first_social_node:end,neighbor);

                  % connect % of attributes
                  % sigal - 19.2.14 - SAMI-A is better with only neighbor's attributes
                  % neighborNeighbors = find(data(neighbor, first_social_node:end));
                  neighborNeighbors = neighbor;
                  for j = neighborNeighbors
                      attData = data(j,1:num_attr_nodes);
                      choosenAtt = chooseAttData(attData, addMissingAtt);
                      attIndices = find(choosenAtt==1);
                      data(i,attIndices) = data(i,attIndices) | data(j,attIndices);
                      data(attIndices,i) = data(attIndices,i) | data(attIndices,j);
                  end
                end
            else
                  fprintf('Warning: node %d doesnt have any neighbors\n', i);
            end
%             data(i,:) = data(neighbor, :);
%             data(:,i) = data(:,neighbor);
%             data(i,neighbor) = 1;
%             data(neighbor,i) = 1;
            
        end

end

function [choosenAtt] = chooseAttData(inAttData, chooseAttPerc)
    choosenAtt = inAttData;
    if chooseAttPerc < 1
        attIndices = find(inAttData==1);
        for i= attIndices
            if rand(1)>chooseAttPerc
                choosenAtt(i) = 0;
            end
        end
    end
end % function chooseAtt



