%function [] = PlotFoil(name,Hs)

close all;

results_filenm = create_filenm([dir 'FlappingFoil_' name],Hs,Ls,Lc,dt,tf,b)
load([results_filenm '.mat']);

BuoyScale = 1.0;
FoilScale = 6.0;



Tf = 60;

load([results_filenm '.mat']);


%xoa(3) = -20;



t = get(Buoy,'t');

%Get Buoy motion results
x = get(Buoy,'x');
xdot = get(Buoy,'xdot');
xddot = get(Buoy,'xddot');
 
%Get Foil motion results
y = get(Foil,'x');
ydot = get(Foil,'xdot');
yddot = get(Foil,'xddot');

xoa = get(Foil,'x0');
xob = get(Buoy,'x0');
xoa(3) = -20;

Bforce = get(Buoy,'Forces');
Fforce = get(Foil,'Forces');

%MOOS Buoy
%BuoyA2
buoy_pts = [-.8 -.8 -.65  -.35 -0.2 -0.2    0.2  0.2 .35   .65 .8 .8 -.8; 
             .4 -.3 -0.5  -0.5 -.65 -1.25 -1.25 -.65 -.5  -0.5 -.3 .4  .4];  %meters

if (name == 'BuoyA10')
  buoy_pts(1,:) = buoy_pts(1,:)*3.6/1.6;  %Scale up for larger buoy.
end
    
if (name == 'BuoyA20')
  buoy_pts(1,:) = buoy_pts(1,:)*5.0/1.6;  %Scale up for larger buoy.
end

buoy_pts = BuoyScale*buoy_pts;
   
%Set up points for foil
xx = 0:.01:1;
yy = NACA_4Digit_Ordinates(.15,xx);
foil_pts = [-xx(end:-1:1)+.25 -xx+.25; yy(end:-1:1) -yy];
foil_pts = foil_pts*Lc;
foil_pts = FoilScale*foil_pts;


max_rng = max(max(abs(buoy_pts)));
current_buoy_center = [xob(1) xob(3)];           
           
xinc = current_buoy_center(1)-10*max_rng:20*max_rng/100:current_buoy_center(1)+10*max_rng;
eta0 = eta(wave,xinc,0);


fh = figure('Position',[25 58 1561 1063]);

ah1 = axes('OuterPosition',[0 0 .5 1]);



set(fh,'DoubleBuffer','on');
set(ah1,'nextplot','replace');
set(ah1,'FontSize',18);

hold(ah1,'on');
axis(ah1,'equal');


%h_buoy = plot(ah1,buoy_pts(1,:)+xob(1),buoy_pts(2,:)+xob(3),'k','LineWidth',1.5);
h_buoy = fill(buoy_pts(1,:)+xob(1)+x(1,1),buoy_pts(2,:)+xob(3)+x(1,3),'y','LineWidth',1.0);

hold on;
axis equal;
xlabel('m');
ylabel('m');

%Plot Incident Wave
h_incwave = plot(ah1,xinc,eta0,'b','LineWidth',1.5);

%Plot Timestamp
h_time=text(-8,4,sprintf('t = %4.1f s',0*dt),'FontSize',20);

%Plot Plate
h_foil = plot(ah1,foil_pts(1,:)+xoa(1)+y(1,1),foil_pts(2,:)+xoa(3)+y(1,3),'k','LineWidth',1.5);
%set(h_foil,'EraseMode','xor');
%set(h_foil,'EraseMode','normal');


%Plot Tether
tether_pts = zeros(2);
tether_pts(1,:) = [p_aB(:,1) p_aF(:,1)];
tether_pts(2,:) = [p_aB(:,3) p_aF(:,3)];
tether_pts(:,1) = BuoyScale*tether_pts(:,1);
tether_pts(:,2) = FoilScale*tether_pts(:,2);

clear ppts;
ppts(1,1) = tether_pts(1,1)+xob(1)+x(1,1);
ppts(1,2) = tether_pts(1,2)+xoa(1)+y(1,1);
ppts(2,1) = tether_pts(2,1)+xob(3)+x(1,3);
ppts(2,2) = tether_pts(2,2)+xoa(3)+y(1,3);
h_tether = plot(ah1,ppts(1,:),ppts(2,:),'r-','LineWidth',1.5);


foo(1) = -8; 
foo(2) = 8;
foo(3) = -25;
foo(4) = 5;
axis(ah1,foo);
axis(ah1,'equal');


ah2 = axes('OuterPosition',[.5 .5 .5 .4]);
set(ah2,'FontSize',16);
set(ah2,'nextplot','replace');
h_Tension = plot(ah2,t,sqrt(Bforce{end}(:,1).^2+Bforce{end}(:,3).^2)/4.45,'LineWidth',1.5);
foo = axis;
foo(2) = Tf;
h_line_tension = line([0 0],foo(3:4),'LineWidth',1.5);
set(h_line_tension,'Color','g');
axis(foo);
xlabel('time (seconds)');
ylabel('Tether Tension (lbs)');

NN = min(Tf./dt,tf./dt);

ah3 = axes('OuterPosition',[.5 .1 .5 .4]);
set(ah3,'FontSize',18);
set(ah3,'nextplot','replace');
Elongation = zeros(NN,1);
ElongationRate = zeros(NN,1);
SpringTension = zeros(NN,1);
DampingTension = zeros(NN,1);
s = zeros(NN,3);
for i = 1:NN
    [Elongation(i),ElongationRate(i),s(i,:)] = TetherElongations(xob,x(i,:)',xdot(i,:)',p_aB,xoa,y(i,:)',ydot(i,:)',p_aF);
    %if(Elongation(i) > 0)  %If ram is fully retracted, tether is slack and no force is exerted.
    %    SpringTension(i) = ppval(Spring_Force_Coeffs,Elongation(i));
    %    DampingTension(i) = ppval(Damping_Force_Coeffs,ElongationRate(i));
    %end
end
h_elongation = plot(ah3,t(1:NN),Elongation,'LineWidth',1.5);
foo = axis;
foo(2) = Tf;
h_line_elongation = line([0 0],foo(3:4));
set(h_line_elongation,'Color','g','LineWidth',1.5);
axis(foo);
xlabel('time (seconds)');
ylabel('Tether Elongation (m)');

%pause
NN = min(Tf./dt,tf./dt);
%Frames = moviein(NN); % initialize the matrix 'Frames'

pause

for i = 1:NN;  %length(t)
  pause(.95*dt);

%Replot Buoy 
  R = [cos(x(i,5)) sin(x(i,5)); -sin(x(i,5)) cos(x(i,5))];
  ppts = R*buoy_pts;  
  for j = 1:length(ppts)
     ppts(1,j) = ppts(1,j)+x(i,1)+xob(1);
     ppts(2,j) = ppts(2,j)+x(i,3)+xob(3);
  end
  set(h_buoy,'XData',ppts(1,:),'YData',ppts(2,:));
  
  
%Replot Tether
  ppts = tether_pts;
  ppts(:,1) = R*tether_pts(:,1);  
  R = [cos(y(i,5)) sin(y(i,5)); -sin(y(i,5)) cos(y(i,5))];
  ppts(:,2) = R*tether_pts(:,2);  
  ppts(1,1) = ppts(1,1)+xob(1)+x(i,1);
  ppts(1,2) = ppts(1,2)+xoa(1)+y(i,1);
  ppts(2,1) = ppts(2,1)+xob(3)+x(i,3);
  ppts(2,2) = ppts(2,2)+xoa(3)+y(i,3);
  
  set(h_tether,'XData',ppts(1,:),'YData',ppts(2,:));
  
%Replot Wave  
  eta0 = eta(wave,xinc-x(i,1),i*dt);
  set(h_incwave,'Xdata',xinc,'YData',eta0);
  
%Replot Time-stamp
  set(h_time,'string',sprintf('t = %4.1f s',i*dt));
  
%Replot Plate  
  R = [cos(y(i,5)) sin(y(i,5)); -sin(y(i,5)) cos(y(i,5))];
  ppts = R*foil_pts;     
  set(h_foil,'XData',ppts(1,:),'YData',ppts(2,:));
    
  ppts = R*foil_pts;  
  for j = 1:length(ppts)
     ppts(1,j) = ppts(1,j)+y(i,1)+xoa(1);
     ppts(2,j) = ppts(2,j)+y(i,3)+xoa(3);
  end
  set(h_foil,'XData',ppts(1,:),'YData',ppts(2,:));

  
%Plot Time-lines
set(h_line_tension,'Xdata',[i*dt i*dt]);
set(h_line_elongation,'Xdata',[i*dt i*dt]);
  

%filename = ['foo' int2str(i), '.ppm'];
%print ('-dppmraw', filename);
%filename = ['foo' int2str(i), '.tif'];
%print ('-dtiff', filename);


%Frames(i) = getframe(gcf);
end

%map = colormap;
%mpgwrite(Frames,map,'example.mpg')
%clear Frames;


 