classdef chanCoords < types.core.LabMetaData & types.untyped.GroupClass
% CHANCOORDS channel coordinates


% PROPERTIES
properties
    layout; % doc for layout
    shankSpacing; % doc for shankSpacing
    source; % doc for source
    x; % doc for x
    y; % doc for y
end

methods
    function obj = chanCoords(varargin)
        % CHANCOORDS Constructor for chanCoords
        %     obj = CHANCOORDS(parentname1,parentvalue1,..,parentvalueN,parentargN,name1,value1,...,nameN,valueN)
        % layout = char
        % shankSpacing = float
        % source = char
        % x = float
        % y = float
        obj = obj@types.core.LabMetaData(varargin{:});
        
        
        p = inputParser;
        p.KeepUnmatched = true;
        p.PartialMatching = false;
        p.StructExpand = false;
        addParameter(p, 'layout',[]);
        addParameter(p, 'shankSpacing',[]);
        addParameter(p, 'source',[]);
        addParameter(p, 'x',[]);
        addParameter(p, 'y',[]);
        misc.parseSkipInvalidName(p, varargin);
        obj.layout = p.Results.layout;
        obj.shankSpacing = p.Results.shankSpacing;
        obj.source = p.Results.source;
        obj.x = p.Results.x;
        obj.y = p.Results.y;
        if strcmp(class(obj), 'types.ndx_cell_explorer.chanCoords')
            types.util.checkUnset(obj, unique(varargin(1:2:end)));
        end
    end
    %% SETTERS
    function obj = set.layout(obj, val)
        obj.layout = obj.validate_layout(val);
    end
    function obj = set.shankSpacing(obj, val)
        obj.shankSpacing = obj.validate_shankSpacing(val);
    end
    function obj = set.source(obj, val)
        obj.source = obj.validate_source(val);
    end
    function obj = set.x(obj, val)
        obj.x = obj.validate_x(val);
    end
    function obj = set.y(obj, val)
        obj.y = obj.validate_y(val);
    end
    %% VALIDATORS
    
    function val = validate_layout(obj, val)
        val = types.util.checkDtype('layout', 'char', val);
    end
    function val = validate_shankSpacing(obj, val)
        val = types.util.checkDtype('shankSpacing', 'float', val);
        if isa(val, 'types.untyped.DataStub')
            valsz = val.dims;
        elseif istable(val)
            valsz = height(val);
        else
            valsz = size(val);
        end
        validshapes = {[1]};
        types.util.checkDims(valsz, validshapes);
    end
    function val = validate_source(obj, val)
        val = types.util.checkDtype('source', 'char', val);
    end
    function val = validate_x(obj, val)
        val = types.util.checkDtype('x', '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_y(obj, val)
        val = types.util.checkDtype('y', '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.LabMetaData(obj, fid, fullpath, refs);
        if any(strcmp(refs, fullpath))
            return;
        end
        if ~isempty(obj.layout)
            if startsWith(class(obj.layout), 'types.untyped.')
                refs = obj.layout.export(fid, [fullpath '/layout'], refs);
            elseif ~isempty(obj.layout)
                io.writeDataset(fid, [fullpath '/layout'], obj.layout);
            end
        end
        if ~isempty(obj.shankSpacing)
            if startsWith(class(obj.shankSpacing), 'types.untyped.')
                refs = obj.shankSpacing.export(fid, [fullpath '/shankSpacing'], refs);
            elseif ~isempty(obj.shankSpacing)
                io.writeDataset(fid, [fullpath '/shankSpacing'], obj.shankSpacing);
            end
        end
        if ~isempty(obj.source)
            if startsWith(class(obj.source), 'types.untyped.')
                refs = obj.source.export(fid, [fullpath '/source'], refs);
            elseif ~isempty(obj.source)
                io.writeDataset(fid, [fullpath '/source'], obj.source);
            end
        end
        if ~isempty(obj.x)
            if startsWith(class(obj.x), 'types.untyped.')
                refs = obj.x.export(fid, [fullpath '/x'], refs);
            elseif ~isempty(obj.x)
                io.writeDataset(fid, [fullpath '/x'], obj.x, 'forceArray');
            end
        end
        if ~isempty(obj.y)
            if startsWith(class(obj.y), 'types.untyped.')
                refs = obj.y.export(fid, [fullpath '/y'], refs);
            elseif ~isempty(obj.y)
                io.writeDataset(fid, [fullpath '/y'], obj.y, 'forceArray');
            end
        end
    end
end

end