function kngr_plotGraph(fig)
%PLOTGRAPH general routine that plots the info stored in <session>
%
% this routine plots the vertices, the vertex labels, the edges and the edge labels based on the info in <session>
%
% syntax:
% plotGraph(fig,session)
%
% input:
% fig = this is the figure handle of the DelftConStruct main window
% session = input argument session contains information on the vertices and edges (sparse matrix form)
%
% output:
%
% see also
%

% --------------------------------------------------------------------------
% Copyright (c) WL|Delft Hydraulics 2004-2007 FOR INTERNAL USE ONLY
% Version: Version 1.1, June 2007 (Version 1.0, February 2004)
% By: <name author (email: email adress author)>
% --------------------------------------------------------------------------

%% get the ontology information (this could later be a global)
global ontology session;

if isempty(ontology)
    ontology = kngr_getOntology;
end

set(gca,'visible','off')
%% find edges info from session data
[idfrom,idto,idontology] = find(session.edges);

%% FRAMES: plot frames FIRST
[id] = find(ismember(idontology,[9, 10, 11, 12]));
if ~isempty(id)
    [id2] = unique(idfrom(id)); %id2 contains fromids
    for i=1:length(id2) % for each fromid that contains a frame relation
        if size(session.vertices,2)~=7; % this means no patch handle is available yet
            [vh, lh, fh, frid, vinfr] = deal([]);
            if size(session.vertices,2)==4;
                vh     = session.vertices{id2(i),3};
                lh     = session.vertices{id2(i),4};
            end
        else
            vh     = session.vertices{id2(i),3};
            lh     = session.vertices{id2(i),4};
            fh     = session.vertices{id2(i),5};
            frid   = session.vertices{id2(i),6};
            vinfr  = session.vertices{id2(i),7};
        end
        % find all frame relations for this from-vertex
        edges = full(session.edges(id2(i),1:end));
        [id3]  = find(ismember(edges, [9, 10, 11, 12])); %id3 contains relevant toids
        vinfr = id3;

        frid   = 1;
        
        % determine the necessary size of the frame (here we must include closing and opening)
        vxyinframe = vertcat(session.vertices{vinfr',2});
        w = (max(vxyinframe(:,1))-min(vxyinframe(:,1)))+20;
        h = (max(vxyinframe(:,2))-min(vxyinframe(:,2)))+20;
        x = min(vxyinframe(:,1))-10;
        y = max(vxyinframe(:,2))+10 - h;

        % plot the frame
        fh = kngr_plotFrame(fh, x, y, w, h);
        session.vertices{id2(i),5} = fh;

        % plot the vertex
        vh = kngr_plotVertex(vh, x, y + h);
        session.vertices{id2(i),3} = vh; session.vertices{id2(i),2} = [x, y + h];

        % plot the label
        lh = kngr_plotLabel(lh, session.vertices{id2(i),2}(1), session.vertices{id2(i),2}(2)+10, session.vertices{id2(i),1},8);
        session.vertices{id2(i),4} = lh;

        session.vertices{id2(i),6} = frid;
        session.vertices{id2(i),7} = vinfr;
    end
end

%% VERTICES: plot vertices SECOND
for i = 1:size(session.vertices,1)
    % plot vertices
    if size(session.vertices,2)==2; % this means no patch handle is available yet
        vh = []; lh = [];
    else
        vh = session.vertices{i,3};
        lh = session.vertices{i,4};
    end
    vh = kngr_plotVertex(vh, session.vertices{i,2}(1), session.vertices{i,2}(2), 5);
    session.vertices{i,3}=vh;

    % plot vertex labels
    lh = kngr_plotLabel(lh, ...
        session.vertices{i,2}(1), ...
        session.vertices{i,2}(2)+10, ...
        session.vertices{i,1},8);
    session.vertices{i,4}=lh;
end

%% EDGES: plot edges LAST
id = find(~ismember(idontology,[9, 10, 11, 12]));
if ~isempty(id)
    for i=1:length(id)
        % plot edges
        if isfield(session, 'edgeHandles') == 1
            eh = session.edgeHandles(idfrom(id(i)),idto(id(i)));
            if eh == 0
                eh = [];
            end
        else
            edges = size(session.edges);
            session.edgeHandles = sparse(edges(1),edges(2));
            session.edgeLabels  = sparse(edges(1),edges(2));
            eh = [];
        end
        eh = kngr_plotEdge(eh, ...
            [session.vertices{idfrom(id(i)),2}(1) session.vertices{idto(id(i)),2}(1)], ...
            [session.vertices{idfrom(id(i)),2}(2) session.vertices{idto(id(i)),2}(2)], ...
            ontology(session.edges( idfrom(id(i)),idto(id(i)) )),2);
        session.edgeHandles(idfrom(id(i)),idto(id(i))) = eh;

        % plot edge labels
        lh = session.edgeLabels(idfrom(id(i)),idto(id(i)));
        if lh == 0
            lh = [];
        end
        lh = kngr_plotLabel(lh, ...
            (session.vertices{idfrom(id(i)),2}(1)+session.vertices{idto(id(i)),2}(1))/2, ...
            (session.vertices{idfrom(id(i)),2}(2)+session.vertices{idto(id(i)),2}(2))/2, ...
            ontology(session.edges(idfrom(id(i)),idto(id(i)))).type,8);
        session.edgeLabels(idfrom(id(i)),idto(id(i)))=lh;
    end
end

%% finally set axis to zoom in on presented graph
data=vertcat(session.vertices{:,2});
axis([min(data(:,1))-50 max(data(:,1))+50 min(data(:,2))-50 max(data(:,2))+50]);
axis equal; box on
drawnow