classdef responseCurve < types.core.NWBDataInterface & types.untyped.GroupClass
% RESPONSECURVE ResponseCurve attributes


% PROPERTIES
properties
    name; % name of response curve
    x_bins; % bins of the firing rate map
    x_edges; % edges of the firing rate map
end

methods
    function obj = responseCurve(varargin)
        % RESPONSECURVE Constructor for responseCurve
        %     obj = RESPONSECURVE(parentname1,parentvalue1,..,parentvalueN,parentargN,name1,value1,...,nameN,valueN)
        % name = char
        % x_bins = float
        % x_edges = float
        obj = obj@types.core.NWBDataInterface(varargin{:});
        
        
        p = inputParser;
        p.KeepUnmatched = true;
        p.PartialMatching = false;
        p.StructExpand = false;
        addParameter(p, 'name',[]);
        addParameter(p, 'x_bins',[]);
        addParameter(p, 'x_edges',[]);
        misc.parseSkipInvalidName(p, varargin);
        obj.name = p.Results.name;
        obj.x_bins = p.Results.x_bins;
        obj.x_edges = p.Results.x_edges;
        if strcmp(class(obj), 'types.ndx_cell_explorer.responseCurve')
            types.util.checkUnset(obj, unique(varargin(1:2:end)));
        end
    end
    %% SETTERS
    function obj = set.name(obj, val)
        obj.name = obj.validate_name(val);
    end
    function obj = set.x_bins(obj, val)
        obj.x_bins = obj.validate_x_bins(val);
    end
    function obj = set.x_edges(obj, val)
        obj.x_edges = obj.validate_x_edges(val);
    end
    %% VALIDATORS
    
    function val = validate_name(obj, val)
        val = types.util.checkDtype('name', 'char', val);
    end
    function val = validate_x_bins(obj, val)
        val = types.util.checkDtype('x_bins', 'float', val);
        if isa(val, 'types.untyped.DataStub')
            valsz = val.dims;
        elseif istable(val)
            valsz = height(val);
        else
            valsz = size(val);
        end
        validshapes = {[Inf]};
        types.util.checkDims(valsz, validshapes);
    end
    function val = validate_x_edges(obj, val)
        val = types.util.checkDtype('x_edges', 'float', val);
        if isa(val, 'types.untyped.DataStub')
            valsz = val.dims;
        elseif istable(val)
            valsz = height(val);
        else
            valsz = size(val);
        end
        validshapes = {[Inf]};
        types.util.checkDims(valsz, validshapes);
    end
    %% EXPORT
    function refs = export(obj, fid, fullpath, refs)
        refs = export@types.core.NWBDataInterface(obj, fid, fullpath, refs);
        if any(strcmp(refs, fullpath))
            return;
        end
        if ~isempty(obj.name)
            if startsWith(class(obj.name), 'types.untyped.')
                refs = obj.name.export(fid, [fullpath '/name'], refs);
            elseif ~isempty(obj.name)
                io.writeDataset(fid, [fullpath '/name'], obj.name);
            end
        else
            error('Property `name` is required in `%s`.', fullpath);
        end
        if ~isempty(obj.x_bins)
            if startsWith(class(obj.x_bins), 'types.untyped.')
                refs = obj.x_bins.export(fid, [fullpath '/x_bins'], refs);
            elseif ~isempty(obj.x_bins)
                io.writeDataset(fid, [fullpath '/x_bins'], obj.x_bins, 'forceArray');
            end
        end
        if ~isempty(obj.x_edges)
            if startsWith(class(obj.x_edges), 'types.untyped.')
                refs = obj.x_edges.export(fid, [fullpath '/x_edges'], refs);
            elseif ~isempty(obj.x_edges)
                io.writeDataset(fid, [fullpath '/x_edges'], obj.x_edges, 'forceArray');
            end
        end
    end
end

end