classdef CoveragePlotter < fusion.theaterPlot.TheaterPlotter
    %COVERAGEPLOTTER Create a new coverage plotter
    %
    %   See:  <a href="matlab:help theaterPlot/CoveragePlotter">theaterPlot/coveragePlotter</a>
    
    % Copyright 2018-2020 The MathWorks, Inc.
    
    properties (Dependent)
        Color
        Alpha
    end
    
    properties (SetAccess = protected, Hidden)
        % internal plotter
        Plotter
    end
    
    properties (Access = private)
        LegendHolder
    end
    
    methods
        
        function this = CoveragePlotter(tp,varargin)
            this@fusion.theaterPlot.TheaterPlotter(tp,varargin{:}); % Calls createGraphics
        end
        
        function plotCoverage(this,configs,varargin)
            this.Plotter.plotCoverage(configs,varargin{:});
            updateLegendHolder(this);
        end
        
        function clearData(this)
            clearData(this.Plotter);
            drawnow limitrate
        end
        
        function set.Alpha(this,value)
            % validation happens in the internal function
            this.Plotter.Alpha = value;
        end
        
        function val = get.Alpha(this)
            val = this.Plotter.Alpha;
        end
        
        function set.Color(this,value)
            this.Plotter.Color = value;
        end
        
        function val = get.Color(this)
            val = this.Plotter.Color;
        end
        
    end
    
    methods (Access = protected)
        function createGraphics(this,hAxes,p)
            pr = p.Results;
            args = struct2pv(pr);
            this.Plotter = radarfusion.internal.coveragePlotter(hAxes,args{:});

            if ~isempty(pr.DisplayName)
                this.LegendHolder = patch(hAxes,nan,nan,nan,...
                    'DisplayName',pr.DisplayName);
            else
                this.LegendHolder = patch(hAxes,nan,nan,nan,...
                    'HandleVisibility','off');
            end
        end
        
        function updateLegendHolder(this)
            if ishghandle(this.LegendHolder) && ~isempty(this.Plotter.Beams)
                % Sync color and alpha
                ph = this.LegendHolder;
                beam = this.Plotter.Beams(1);
                set(ph,'FaceColor',beam.FaceColor,...
                    'FaceAlpha',beam.FaceAlpha);
            end
                
        end
        
        function deleteGraphics(this)
            delete(this.Plotter);
            delete(this.LegendHolder);
        end
        
        function value =  getPrimaryObject(this)
            value = this.LegendHolder;
        end
        
        function initParser(this, p)
            initParser@fusion.theaterPlot.TheaterPlotter(this, p)
            addParameter(p,'Color','Auto');
            addParameter(p,'Alpha',[0.5 0.05]);
        end
        
    end
end

function out = struct2pv(in)
%converts struct to a cell array of NV pairs
fnames = fieldnames(in);
nfields = numel(fnames);
out = cell(1,2*(nfields-2));% remove 2 for display name and tag
ind = 1;
for i=1:nfields
    name = fnames{i};
    if ~any(strcmp(name,{'Tag','DisplayName'}))
        out{ind} = name;
        out{ind+1} = in.(name);
        ind = ind+2;
    end
end
end
