function [offsets, dates] = calculateOffsetsAndDates(pFloatRafosDates, pSoundsourceRafosDates, pSoundsourceOffset, pDrift, pSchedule)
%CALCULATEOFFSETSANDDATES Summary of this function goes here
%   Detailed explanation goes here


%% Prepare variables

if isnan(pDrift)
    pDrift = 0;
end

schedule = pSchedule / 24;

soundsourceBegin = pSoundsourceRafosDates(1);
soundsourceEnd = pSoundsourceRafosDates(2);
soundsourceOffsetTotal = [];
soundsourceDaysTotal = [];
[m, ~] = size(pSoundsourceRafosDates);
if m == 1
    soundsourceEnd = pFloatRafosDates(2);
end

floatBegin = pFloatRafosDates(1);
floatEnd = pFloatRafosDates(2);

offsetDate = soundsourceBegin;
offset = 0;
if ~isempty(pSoundsourceOffset)
    offsetDate = artoa.convert.dmy2rd( ...
        pSoundsourceOffset(:, 3), ...
        pSoundsourceOffset(:, 2), ...
        pSoundsourceOffset(:, 1) ...
    );
    offset = pSoundsourceOffset(:, 4);
end

%% Calculations

for i = 1:length(offset) - 1
    tmpDates = [offsetDate(i):schedule:offsetDate(i + 1)]';
    tmpOffset = ( ...
        (offset(i + 1) - offset(i)) ...
         / (offsetDate(i + 1) - offsetDate(i)) ...
    ) * [tmpDates - offsetDate(i) - 1] + offset(i);
    soundsourceOffsetTotal = [soundsourceOffsetTotal; tmpOffset];
    soundsourceDaysTotal = [soundsourceDaysTotal; tmpDates];
    clear tmpDates tmpOffset;
end

floatDays = [offsetDate(end):schedule:soundsourceEnd]';
tmpOffset = (floatDays - offsetDate(length(offsetDate))) * pDrift + offset(length(offset));
soundsourceOffsetTotal = [soundsourceOffsetTotal;tmpOffset];
soundsourceDaysTotal = [soundsourceDaysTotal; floatDays];
clear tmpOffset floatDays;

%% Determine start and end date
startDate = soundsourceBegin;
if floatBegin >= soundsourceBegin
    startDate = floatBegin;
end
endDate = soundsourceEnd;
if floatEnd <= soundsourceEnd
    endDate = floatEnd;
end

totalDays = [startDate:schedule:endDate]';
index = find((soundsourceDaysTotal <= endDate) & (soundsourceDaysTotal >= startDate));

offsets = soundsourceOffsetTotal(index);
dates = soundsourceDaysTotal(index);

end

