classdef polariMBDataset < internal.polariMouseBehavior
    % Mouse Behavior registration for dataset display widgets (lines,
    % filled polygons, etc).
    
    methods
        function obj = polariMBDataset(isButtonDown)
            
            if nargin > 0 && isButtonDown
                obj.InstallFcn  = @showToolTipAndPtr_datasetDown;
                obj.MotionEvent = @wbdrag;
                obj.DownEvent   = [];
                obj.UpEvent     = @internal.polariMBDataset.wbup;
                obj.ScrollEvent = [];
            else
                obj.InstallFcn  = @(p)showToolTipAndPtr(p,'motion');
                obj.MotionEvent = @wbmotion;
                obj.DownEvent   = @wbdown;
                obj.UpEvent     = [];
                obj.ScrollEvent = @internal.polariMBMagTicks.wbscroll;
            end
        end
    end
    
    % Functions are put in Static when other mouse handlers need to call
    % these directly.  Local functions (below) are private and are not
    % available for calls outside of this calss.
    methods (Static)
        function wbup(p,ev)
            % Button release on dataset - Never called?
            
            % Set point into axes:
            p.hFigure.CurrentPoint = ev.Point;
            
            % Check where mouse is to determine next step:
            s = computeHoverLocation(p,ev);
            if s.overDataset
                p.pHoverDataSetIndex = s.overDatasetIndex;
                changeMouseBehavior(p,'dataset');
                
                % Override standard 'dataset' behavior on mouse-up:
                showToolTipAndPtr(p,'up');
            else
                plot_glow(p,false);
                autoChangeMouseBehavior(p,s);
            end
        end
    end
end

function wbmotion(p,ev)

s = computeHoverLocation(p,ev);
firstEntry = p.ChangedState;
if firstEntry
    p.ChangedState = false;
    %datasetHilite(p,s.overDatasetIndex);
end

if s.overDataset
    % May have gone from one dataset to another without hovering off a
    % dataset -- i.e., two datasets touching each other and mouse shifts
    % from one to the other.
    if p.pHoverDataSetIndex ~= s.overDatasetIndex
        p.pHoverDataSetIndex = s.overDatasetIndex;
    end
    plot_glow(p,s.overDataset);
    if firstEntry
        showToolTipAndPtr(p,'motion');
    end
else
    % No longer over dataset (possibly not over axes)
    plot_glow(p,s.overDataset);
    autoChangeMouseBehavior(p,s);
end
end

function wbdown(p,ev)
% Click on dataset to add a temporary angle marker

if isIntensityData(p)
    internal.polariMBGrid.wbdown(p,ev);
    return % EARLY EXIT
end
                
p.hFigure.CurrentPoint = ev.Point;

% For context-menu click, don't bring trace to front. Just return quickly
% so only the context menu action occurs.
selType = p.hFigure.SelectionType;
if strcmpi(selType,'alt')
    % right-click: this action launches the context menu.
    %
    % Prevent further reaction to right-click here, so we don't do mag-tick
    % hilite and confuse the user when the context menu launches.
    p.hCurrentObject = p.hFigure.CurrentObject;
    resetToolTip(p);
    return
end

isOpen = strcmpi(selType,'open');

% Bring dataset to front.
% Automatically changes active dataset as well.
reorderDataPlot(p,+1);

if isOpen || p.TemporaryCursor
    if ~isa(p,'rf.internal.smithplot')
    % Single-click or double-click on dataset:
    %  - add new cursor
    
    % To check if "HG data cursor" is on right now.
    % If it is, do NOT create a polari angle marker.
    fig = p.hFigure; 
    dcm_obj = datacursormode(fig);
    if strcmp(dcm_obj.Enable, 'on')% isHGDataCursorOn()
        return
    end
    
    % Signal to wbup_anglemarker that marker is to be deleted on button
    % release.
    %
    % Recompute on each entry (not latched), since we get here with
    % 'normal' followed by 'open' on a double-click event. We want to keep
    % the 'open' outcome (false) in that situation.
    p.pDeleteCurrentMarkerOnButtonRelease = ~isOpen;
    
    % Get mag/theta mouse coords, setup cache entries.
    % Do before enabling new mouse behaviors:
    pt = p.hAxes.CurrentPoint(1,1:2);
    datasetIndex = p.pHoverDataSetIndex;
    m = i_addCursor(p,pt,datasetIndex);
    
    % Force update of marker highlight, drag state, etc, with a special
    % 3rd-arg call to wbmotion_*:
    changeMouseBehavior(p,'anglemarker');
    internal.polariMBAngleMarker.wbmotion(p,ev,m); % hover over ...
    
    % Override SelectionType so polariMBAngleMarker.wbdown doesn't try to
    % also do a "double-click" action
    if isOpen
        p.hFigure.SelectionType = 'normal';
    end
    internal.polariMBAngleMarker.wbdown(p,ev);     % ... then button down
    
    % Engage readout of marker magnitude/angle
    %angleMarkerOriginLine(p,true);
    
    %wbmotion_dataset(p,ev); % restore glow, hover
    end
else
    plot_glow(p,true);
    changeMouseBehavior(p,'dataset_buttondown');
end
end

function wbdrag(p,ev)
% Drag on data - Never called?

% Set point into axes:
p.hFigure.CurrentPoint = ev.Point;
end

function showToolTipAndPtr_datasetDown(p)
resetToolTip(p);
setptr(p.hFigure,'arrow');
end

function showToolTipAndPtr(p,option,datasetIndex)

if p.ToolTips && ~isempty(p.hToolTip) && isvalid(p.hToolTip)
    %[Nthis,Nall] = numMarkersOnDataset(p,datasetIndex);
    
    if nargin < 3
        datasetIndex = p.pHoverDataSetIndex;
    end
    if (getNumDatasets(p) > 1) && ...
            (datasetIndex == p.pCurrentDataSetIndex)
        active = ' (ACTIVE)';
    else
        active = '';
    end
    str = sprintf('DATASET %d%s\n',datasetIndex,active);
    if strcmpi(option,'up')
        str = {str,'Drag cursor to reposition'};
    else
        if isIntensityData(p)
            str = {str,'Drag to change magnitude limits'};
        else
%             Nall = numel(p.hPeakAngleMarkers) + numel(p.hCursorAngleMarkers);
            if p.TemporaryCursor
%                 if Nall==1 % Shashank
%                     str = {str,'Click to show angle span'};
%                 else
%                     str = {str,'Click to show cursor'};
%                 end
            else
                str = {str};
            end
        end
        str = [str 'Double-click to add cursor'];
    end
    start(p.hToolTip,str);
end

setptr(p.hFigure,'arrow');

end
