function [msgType, varargout] = helperBLEMeshLightnessPDUDecode(msgType, lightnessPDU)
%helperBLEMeshLightnessPDUDecode Decode Bluetooth mesh lightness PDU
%   [MSGTYPE, VARARGOUT] = helperBLEMeshLightnessPDUDecode(MSGTYPE,
%   LIGHTNESSPDU) decodes lightness PDU and returns the decoded lightness
%   PDU parameters based on the message type, MSGTYPE.
%
%   MSGTYPE is a character vector or a string scalar indicating the type of
%   message.
%
%   LIGHTNESSPDU is a column vector of decimal octets.
%
%   Output parameters can be different for each message
%   1. Light lightness set, Light lightness set unacknowledged
%   If non-instantaneous state change, output parameters are MSGTYPE, 
%   LIGHTNESS, TID, TRANSITIONTIMESTEPS, TRANSITIONTIMERESOLUTION, DELAY. 
%   If instantaneous state change, output parameters are MSGTYPE,
%   LIGHTNESS, TID.
% 
%   2. Light lightness status
%   If state transition is in progress, output parameters are MSGTYPE, 
%   PRESENTLIGHTNESS, TARGETLIGHTNESS, REMAININGTIMESTEPS, 
%   REMAININGTIMERESOLUTION otherwise, output parameters are MSGTYPE, 
%   LIGHTNESS
%   
%   LIGHTNESS, PRESENTLIGHTNESS, TARGETLIGHTNESS, TID, TRANSITIONTIMESTEPS,
%   TRANSITIONTIMERESOLUTION, DELAY, REMAININGTIMESTEPS, 
%   REMAININGTIMERESOLUTION are integer values.

%   Copyright 2019 The MathWorks, Inc.

%#codegen

% Validate lightness PDU
validateattributes(lightnessPDU, {'numeric'}, ...
    {'vector', 'nonnegative', '<=', 255}, mfilename, 'lightnessPDU');

% Validate message type
validateattributes(msgType, {'string', 'char'}, {}, mfilename, 'msgType');

% Initialize
varargout{1} = 0;
varargout{2} = 0;
varargout{3} = 0;
varargout{4} = 0;
varargout{5} = 0;
cnt = 1;

switch msgType
    case {'Light lightness set', 'Light lightness set unacknowledged'}        
        % Lightness (2 octets)
        lightnessOctets = lightnessPDU(cnt:cnt+1);
        varargout{1} = bi2de(reshape(de2bi(lightnessOctets, 8)', 1, []));
        cnt = cnt + 2;
        
        % Transaction identifier (1 octet)
        varargout{2} = lightnessPDU(cnt);
        cnt = cnt + 1;
        
        % State change is non-instantaneous
        if (length(lightnessPDU) > 3)
            % Transition time (1 octet)
            transitionTime = de2bi(lightnessPDU(cnt), 8);
            varargout{3} = bi2de(transitionTime(1:6));
            varargout{4} = bi2de(transitionTime(7:8));
            cnt = cnt + 1;
            
            % Message execution delay (1 octet)
            varargout{5} = lightnessPDU(cnt);
        end
        
    case 'Light lightness status'        
        % Lightness (2 octets)
        lightnessOctets = lightnessPDU(cnt:cnt+1);
        varargout{1} = bi2de(reshape(de2bi(lightnessOctets, 8)', 1, []));
        cnt = cnt + 2;
        
        % State change is in progress
        if(length(lightnessPDU) > 2)
            % Target Lightness (2 octets)
            targetLightnessOctets = lightnessPDU(cnt:cnt+1);
            varargout{2} = bi2de(reshape(de2bi(targetLightnessOctets, 8)', 1, []));
            cnt = cnt+2;            
            
            % Remaining time to reach target state (1 octet)
            remainingTime = de2bi(lightnessPDU(cnt), 8);
            varargout{3} = bi2de(remainingTime(1:6));
            varargout{4} = bi2de(remainingTime(7:8));
        end
    otherwise
        fprintf("LightnessPDUDecode: Given message type is not supported.\n");
end
end
