function arrowhead(Xin1,Yin1,ArrowChek,McmCollect,ArWidth,flxDirect)
% arrowhead
% is a subfunction of NetDraw for drawing arrowheads on
% coordinates generated by ArrowheadProcess function.
% 
% Inputs:
% Xin1, Yin1: Coordinates generated by  ArrowheadProcess.
% ArrowChek: Overlapping reactions' info.
% McmCollect: Colormapping info for flux carrying reactions.
% ArWidth: Arrow width
% flxDirect: Information on reaction directionality from ArrowheadProcess.

% O. Jamialahmadi
% TMU, Chem. Eng. Dept., Biotech. Group 
% July 2016
%--------------------------------------------------------------------------

H = ones(numel(Xin1),1).*(2.5*ArWidth); % Arrowhead width
if ~isempty(ArrowChek)
    for i1 = 1:size(ArrowChek,1)
        if isempty(Xin1{ArrowChek(i1,1)})
            continue
        end
        H(ArrowChek(i1,1)) = H(ArrowChek(i1,1))/(ArrowChek(i1,2));
    end
end
if numel(McmCollect) == 1
    McmCollect = ones(numel(Xin1),3).*McmCollect;
elseif size(McmCollect,1) == 1
    TempMcm = ones(numel(Xin1),3);
    TempMcm(:,1) = TempMcm(:,1).*McmCollect(1);
    TempMcm(:,2) = TempMcm(:,2).*McmCollect(2);
    TempMcm(:,3) = TempMcm(:,3).*McmCollect(3);
    McmCollect = TempMcm;
end

for cnt1 = 1:numel(Xin1)
    if isempty(Xin1{cnt1})
        continue
    end
    if ~isempty(flxDirect)
        Dirct = flxDirect.D{cnt1};
        flx = flxDirect.F(cnt1);
        if flx < -1e-6 && numel(Dirct) > 1
            Dirct = Dirct.*-1;
        end
    end
    Xin = Xin1{cnt1}; Yin = Yin1{cnt1}; 
    Mcm = McmCollect(cnt1,:);
    for cnt = 1:size(Xin,1)
        X = Xin(cnt,:); Y = Yin(cnt,:); coorX = []; coorY =[];
        if ~diff(Y)
            coorX(1) = X(1); coorY(1) = Y(1) + H(cnt1);
            coorX(2) = X(2); coorY(2) = Y(2);
            coorX(3) = X(1); coorY(3) = Y(1) - H(cnt1);  
        else
            LineSlope = diff(Y)/diff(X);
            LineSlope = -1/LineSlope;
            coorX(1) = X(1) + H(cnt1)*(1/sqrt(1+LineSlope^2));
            coorY(1) = Y(1) + H(cnt1)*(LineSlope/sqrt(1+LineSlope^2));
            coorX(2) = X(2); coorY(2) = Y(2);
            coorX(3) = X(1) - H(cnt1)*(1/sqrt(1+LineSlope^2));
            coorY(3) = Y(1) - H(cnt1)*(LineSlope/sqrt(1+LineSlope^2));
        end
        if ~isempty(flxDirect)
            if numel(Dirct) == 1 % Irreversible
                patch(coorX,coorY,zeros(1,numel(coorX)),'EdgeColor','none',...
                'FaceColor',[Mcm(1),Mcm(2),Mcm(3)],'Parent',gca)                
            else
              if Dirct(cnt) == 1
                  patch(coorX,coorY,zeros(1,numel(coorX)),'EdgeColor','none',...
                'FaceColor',[Mcm(1),Mcm(2),Mcm(3)],'Parent',gca) 
              else
                  patch(coorX,coorY,zeros(1,numel(coorX)),'EdgeColor',...
                      [Mcm(1),Mcm(2),Mcm(3)],...
                'FaceColor',[1,1,1],'Parent',gca) 
              end
            end
        else
            patch(coorX,coorY,zeros(1,numel(coorX)),'EdgeColor','none',...
                'FaceColor',[Mcm(1),Mcm(2),Mcm(3)],'Parent',gca)
        end
    end
end