function [fval,G_gamma] = fvalue(x,b,n,Ahandle,AThandle,gamma,mu,mu2)
% Compute the function value of F_gamma at x = (z,y)
% where F_gamma is the envelope of the objective function in 
% min 0.5*||Az - b||^2 - mu_2*<y,z> + mu_1*||z||_1 +...
%     delta_{||.|| <= 1}(y) 
% %   ( with the form min f(x) + P(x) )

% Input

% n                - integer
% x                - 2n-dimensional vectors
% AShandle/AThandle - defined matrix handles
% gamma, mu, mu2   - constant


z = x(1:n);
y = x(n+1:2*n);

C = Ahandle(z);
f = 0.5*norm(C - b)^2 - mu2*(z'*y);
% gradient computation
grad_f = [AThandle(C - b) - mu2*y; -mu2*z];
% two parts of x - gamma*grad_f(x)
w1 = z - gamma*grad_f(1:n);
w2 = y - gamma*grad_f(n+1:2*n);
% prox_gammag(x - gamma*grad_f)
ustar = soft_thresh(w1,mu*gamma);
vstar = proj(w2);
P_gamma = [ustar; vstar];
G_gamma = (x - P_gamma)/gamma;

fval = f + mu*norm(ustar,1) - gamma*(grad_f'*G_gamma) + 0.5*gamma*norm(G_gamma)^2;

end

