function handles = View(m)
%VIEW  a GUI representation of the signal model

% build the GUI
handles = initGUI();
%onChangedF(handles, m);    % populate with initial values
onChangeImg(handles,m)
% observe on model changes and update view accordingly
% (tie listener to model object lifecycle)
%addlistener(m, 'f','PostSet', @(o,e) onChangedF(handles,m));  %@(o,e) onChangedF(handles,e.AffectedObject));
addlistener(m, 'displayedImg','PostSet', @(o,e) onChangeImg(handles,m));
%     addlistener(m, 'EventFrequencyChanged', @(o,e) onChangedF(handles,m));  %@(o,e) onChangedF(handles,e.AffectedObject));
end
function handles = initGUI()
% initialize GUI controls
hFig = figure('Menubar','none','Visible','off');

% figure panel
hPanelTop= uipanel('Parent',hFig,'BackgroundColor','white',...
    'Position',[0 0.2 1 1]);
hAx = axes('Parent',hPanelTop,'DataAspectRatio',[1 1 1]);
% hSlid = uicontrol('Parent',hPanelTop, 'Style','slider', ...
%     'Min',1, 'Max',10, 'Value',5, 'Position',[20 20 200 20]);
hLine = line('XData',NaN, 'YData',NaN, 'Parent',hAx, ...
    'Color','r', 'LineWidth',2);
hImg = image('Parent',hAx,'cData',[]);
% button panel
hPanel= uipanel('Parent',hFig,'BackgroundColor','white',...
    'Position',[0 0 1 0.2]);
hTabgp= uitabgroup(hPanel,'Position',[.05 .05 0.9 0.9]); % R2014b
hTab1= uitab('Parent',hTabgp, 'Title', 'Tabnr. 1','Tag','hTab1'); % R2014b
hTab2= uitab('Parent',hTabgp, 'Title', 'Tabnr. 2','Tag','hTab2'); % R2014b

% buttons hTab1
buttonsString1 = {'switchImg','a2','a3','a4','a5','a6'};
buttonsString2 = {'SSH beagle','Disconnect Socket','b3','b4','b5',};
layout = [2,Inf];
fillTabWithButtons(hTab1,buttonsString1,layout)
fillTabWithButtons(hTab2,buttonsString2,layout)



% customize
set(hAx,'YTick',[],'XTick',[],'XColor', 'none','YColor','none')
hAx.Position = hAx.OuterPosition;
%xlabel(hAx, 't [s]')
%ylabel(hAx, 'Amplitude')

% return a structure of GUI handles
% handles = struct('fig',hFig, 'ax',hAx, 'line', hLine, 'img',hImg, ...
%     'slider',hSlid,'figurePanel',hPanelTop,'buttonPanel',hPanel,'tab1',hTab1,'tab2',hTab2);
mouse.buttonDown = 0;
mouse.buttonPulse = 1;
handles = struct('fig',hFig, 'ax',hAx, 'line', hLine, 'img',hImg, ...
    'figurePanel',hPanelTop,'buttonPanel',hPanel,'tab1',hTab1,'tab2',hTab2,...
    'mouse',mouse);
end

function fillTabWithButtons(tab,buttonsString,layout)
% debug layout:
% buttonsString = cellstr(strcat(tab.Tag,'_',num2str(1:length(buttonsString),'%d').'));

layout = [layout(1),ceil(length(buttonsString)/layout(1))];
height = 1/layout(1);
width = 1/layout(2);
pos1 = linspace(0,1,layout(1)+1);
pos2 = linspace(0,1,layout(2)+1);
[pos1,pos2]=meshgrid(pos1(1:end-1),pos2(1:end-1));
pos = [pos2(:),flip(pos1(:))];
% childrens are numbered
for i = length(buttonsString):-1:1
    hButton= uicontrol( 'style','pushbutton', 'Parent',tab,...
        'string', buttonsString{i},...
        'units', 'normalized' ,...
        'position' , [pos(i,:) width height],...
        'tag',num2str(i),...
        'callback' , 'set(Text1,''string'',''Done'')');
    
end
end

function onChangedF(handles,model)
%     % respond to model changes by updating view
%     if ~ishghandle(handles.fig), return, end
set(handles.line, 'XData',model.t, 'YData',model.x)
set(handles.slider, 'Value',model.f);
disp('plot updated')
model.f
drawnow
end
function onChangeImg(handles,model)
set(handles.img,'cData',model.displayedImg)
end


