classdef eventdata < types.core.NWBDataInterface & types.untyped.GroupClass
% EVENTDATA event data attributes


% PROPERTIES
properties
    alignment; % alignment of events (peaks, start, center)
    event_file; % source file of events
    name; % name of events
    x_bins; % bins of the event
    x_label; % x_label (with unit)
end

methods
    function obj = eventdata(varargin)
        % EVENTDATA Constructor for eventdata
        %     obj = EVENTDATA(parentname1,parentvalue1,..,parentvalueN,parentargN,name1,value1,...,nameN,valueN)
        % name = char
        % alignment = char
        % event_file = char
        % x_bins = float
        % x_label = char
        obj = obj@types.core.NWBDataInterface(varargin{:});
        
        
        p = inputParser;
        p.KeepUnmatched = true;
        p.PartialMatching = false;
        p.StructExpand = false;
        addParameter(p, 'name',[]);
        addParameter(p, 'alignment',[]);
        addParameter(p, 'event_file',[]);
        addParameter(p, 'x_bins',[]);
        addParameter(p, 'x_label',[]);
        misc.parseSkipInvalidName(p, varargin);
        obj.name = p.Results.name;
        obj.alignment = p.Results.alignment;
        obj.event_file = p.Results.event_file;
        obj.x_bins = p.Results.x_bins;
        obj.x_label = p.Results.x_label;
        if strcmp(class(obj), 'types.ndx_cell_explorer.eventdata')
            types.util.checkUnset(obj, unique(varargin(1:2:end)));
        end
    end
    %% SETTERS
    function obj = set.alignment(obj, val)
        obj.alignment = obj.validate_alignment(val);
    end
    function obj = set.event_file(obj, val)
        obj.event_file = obj.validate_event_file(val);
    end
    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_label(obj, val)
        obj.x_label = obj.validate_x_label(val);
    end
    %% VALIDATORS
    
    function val = validate_alignment(obj, val)
        val = types.util.checkDtype('alignment', 'char', val);
    end
    function val = validate_event_file(obj, val)
        val = types.util.checkDtype('event_file', 'char', val);
    end
    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_label(obj, val)
        val = types.util.checkDtype('x_label', 'char', val);
    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.alignment)
            if startsWith(class(obj.alignment), 'types.untyped.')
                refs = obj.alignment.export(fid, [fullpath '/alignment'], refs);
            elseif ~isempty(obj.alignment)
                io.writeDataset(fid, [fullpath '/alignment'], obj.alignment);
            end
        end
        if ~isempty(obj.event_file)
            if startsWith(class(obj.event_file), 'types.untyped.')
                refs = obj.event_file.export(fid, [fullpath '/event_file'], refs);
            elseif ~isempty(obj.event_file)
                io.writeDataset(fid, [fullpath '/event_file'], obj.event_file);
            end
        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_label)
            if startsWith(class(obj.x_label), 'types.untyped.')
                refs = obj.x_label.export(fid, [fullpath '/x_label'], refs);
            elseif ~isempty(obj.x_label)
                io.writeDataset(fid, [fullpath '/x_label'], obj.x_label);
            end
        end
    end
end

end