function NLLData = NLLSimData_gradualValShiftSim(Data, simParams)
% function NLLData = NLLSimData(Data, simParams)
% calculate the simulated negative log-likelihood of the data given a set
% of input parameters
%
% Data: structure with fields 'resp', 'rt', and a column vector of
% attribute values representing the tastiness and healthiness of each food
% 
% simParams: a vector containing the parameters for use in estimating
% likelihoods. Entered in following order:
%   1 & 2 = initial weights on taste and health
%   3 & 4 = final weights on taste and health
%   5 = barrier
%   6 & 7 = time of value shift and rate
%   8 = non-decision time

% assign each data observation its corresponding probability, and transform
% it using the negative log of the probability
NLLDataProb = zeros(length(Data.resp),1);
dataVals1 = Data.attribVals * simParams(1:2)'; % pre-shift values
dataVals2 = Data.attribVals * simParams(3:4)'; % post-shift values

uniqueValCombos = unique([dataVals1 dataVals2],'rows');

maxRT = 4;
for i = 1:size(uniqueValCombos,1)
    params.initDrift = uniqueValCombos(i,1);
    params.finalDrift = uniqueValCombos(i,2);
    params.timeShift = simParams(6);
    params.rateShift = simParams(7);
    params.barrier = simParams(5);
    params.nonDec = simParams(8);
    simData = simulDDM_valShiftNormApprox(params);
    
    matchingTrials = find(dataVals1 == params.initDrift & dataVals2 == params.finalDrift);
    
    for t = matchingTrials'
        RTbin = round(Data.rt(t) * 1000);
        Resp = Data.resp(t);
        if isnan(RTbin)
            Resp = NaN;
        end
        
        if RTbin > maxRT*1000 % catches weird glitch where some RTs are slightly over the time limit
            if RTbin < maxRT*1000 + 20
                RTbin = maxRT*1000;
            else
                RTbin = NaN;
            end
        end

        switch Resp
            case 0
                NLLDataProb(t) = -log(simData.pCross1(RTbin) + eps);
            case 1
                NLLDataProb(t) = -log(simData.pCross2(RTbin) + eps);
            otherwise
                NLLDataProb(t) = -log(simData.pNonResp + eps);
        end     
    end  
end

% sum over all log(prob)
NLLData = sum(NLLDataProb);
