classdef Notifier < handle
%Notifier class for populating the 

%   Copyright 2014 The MathWorks, Inc.
    
    % Fundamental properties
    properties (GetAccess = public, SetAccess = protected)
        rootModelH = [];
        refModels = [];
        session;
        WebDDG = [];
        studio = [];
    end
    
    methods 
        function this = Notifier(session)
            this.session = session;
            this.rootModelH = session.rootModelH;
            this.refModels = session.refModels;
        end
        
        function out = getEditors(this)
            out = [];
            out = [out modelEditors(this.rootModelH)];
            for idx = 1:numel(this.refModels)
                out = [out modelEditors(this.refModels(idx))];   %#ok<AGROW>
            end
        end
        
        function refreshLabel(this, htmlStr) 
            edtrs = this.getEditors();
            msgId = 'SlDV:Slicer:Info';
            
                for idx=1:numel(edtrs)
                    ed = edtrs(idx);

                    % Remove all existing notifications
                    ed.closeNotificationByMsgID(msgId);

                    % Add this notification to all editors
                    ed.deliverInfoNotification(msgId, htmlStr);
                end
        end
        
        function url = fileUrl(this, filePath) %#ok<INUSL>
            if ispc
            else
                url = sprintf('<img src=file://%s>',filePath);
            end
        end
        
        function str = criteriaSummary(this, overlay)
            slCriterion = overlay.slCriterion;
            iconUrl = this.fileUrl(overlay.iconFile);
            blkCnt = slCriterion.blkCnt;
            totalBlks = slCriterion.modelSlicer.ir.dfgIdxToHandle.Count;
            blkPercentage = 100 * (blkCnt/double(totalBlks));
            
            str = sprintf('%s %s <i>%.0f%% (%d/%d) blocks</i>', ...
                           slCriterion.name, iconUrl, blkPercentage, blkCnt, totalBlks);
        end
        
        
        function update(this)
            visOverlays = this.session.visibleOverlays;
            overlapUrl = this.fileUrl(this.session.multiOverlapIconFile);
            multiOverlapUrl = this.fileUrl(this.session.multiOverlapIconFile);
            
            if this.useDock
                htmlStr = '';
            else
                htmlStr = '<b>Model Slicer: </b>';
            end
            
            if emptyHighlight(visOverlays)
                htmlStr = [htmlStr '<i>Right-click model items to select.</i>'];
            else
                for idx=1:numel(visOverlays)
                    critStr = this.criteriaSummary(visOverlays(idx));
                    if idx==1
                        htmlStr = [htmlStr critStr];          %#ok<AGROW>
                    else
                        htmlStr = [htmlStr ', ' critStr];     %#ok<AGROW>
                    end
                end
                
                if numel(visOverlays) == 2
                    htmlStr = [htmlStr ', Intersection ' overlapUrl];
                elseif numel(visOverlays) == 3
                    htmlStr = [htmlStr ', 2-Way Intersection ' overlapUrl];
                    htmlStr = [htmlStr ', 3-Way Intersection ' multiOverlapUrl];
                end
            end
            
            this.refreshLabel(htmlStr);
        end
    end
end

function out = emptyHighlight(visOverlays)
    out = false;
    
    if numel(visOverlays)>1
        return;
    end
    
    out = isempty(visOverlays) || isempty(visOverlays.slCriterion.startingPoints);
end

function out = modelEditors(modelH)
    try
        % XXX - This MUST change because we can be viewing a subsystem
        out = GLUE2.Util.findAllEditors(get_param(modelH,'Name'));
    catch MX
        out = [];
    end
end
        
 
