classdef pixeldata < handle
    %PIXELDATA Shows Pixel Data, Gets High RI Reference
    % Yep that's what it does.
    
    properties
        rawui
        interface
        axes
        sum
        dark
        bg
        type
    end
    
    methods
        function obj = pixeldata(interface) % Add interface to send data
            obj.rawui = rawviewer();
            obj.interface = interface;
            x = get(obj.rawui, 'Children');
            % x = guidata(obj.rawui)
            % x.rawaxes
            for y = 1:length(x)
                if strcmp('rawaxes', get(x(y), 'Tag'))
                    obj.axes = x(y);
                end
            end
            set(obj.rawui, 'UserData', obj);
            obj.dark =[];
            obj.bg = [];
            obj.sum = [];
        end
        
        % All data will have an Error property.
        function receive(obj, data, command)
            if ~data.Error && ~isempty(data.Type) && command.type == commandtype.readpixels
                switch data.Type
                    case 'sum'
                        obj.sum = data.PixelValues;
                    case 'bg'
                        obj.bg = data.PixelValues;
                    case 'dark'
                        obj.dark = data.PixelValues;
                end
                switch obj.type
                    case 'Norm'
                        if ~isempty(obj.sum) && ~isempty(obj.bg) && ~isempty(obj.dark)
                            plot(obj.axes, obj.sum - obj.bg - obj.dark);
                        end
                    case 'Raw'
                        plot(obj.axes, obj.sum);
                    case 'Ref'
                        plot(obj.axes, obj.bg);
                end
            end
        end
        
        function getdata(obj, channel, type)
            % Perform functions here
            % New channel is a number between 0 and 23 based on which of
            % the 24 sensors channels we've selected
            obj.sum = [];
            obj.bg = [];
            obj.dark = [];
            switch type
                case 'Norm'
                    obj.type = type;
                    cd = commanddata();
                    cd.Channel = channel;
                    cd.Type = 'sum';
                    obj.interface.sendcommand(command(cd, commandtype.readpixels, obj));
                    cd = commanddata();
                    cd.Channel = channel;
                    cd.Type = 'bg';
                    obj.interface.sendcommand(command(cd, commandtype.readpixels, obj));
                    cd = commanddata();
                    cd.Channel = channel;
                    cd.Type = 'dark';
                    obj.interface.sendcommand(command(cd, commandtype.readpixels, obj));
                case 'Raw'
                    cd = commanddata();
                    cd.Channel = channel;
                    obj.type = type;
                    cd.Type = 'sum';
                    obj.interface.sendcommand(command(cd, commandtype.readpixels, obj));
                case 'Ref'
                    cd = commanddata();
                    cd.Channel = channel;
                    obj.type = type;
                    cd.Type = 'bg';
                    obj.interface.sendcommand(command(cd, commandtype.readpixels, obj));
                otherwise
            end
        end
        
        function show(obj)
            set(obj.rawui, 'Visible', 'on');
        end
        
        function delete(obj)
            delete(obj.rawui);
        end
    end
end