function varargout = datacontroller(varargin)
% DATACONTROLLER MATLAB code for datacontroller.fig
%      DATACONTROLLER, by itself, creates a new DATACONTROLLER or raises the existing
%      singleton*.
%
%      H = DATACONTROLLER returns the handle to a new DATACONTROLLER or the handle to
%      the existing singleton*.
%
%      DATACONTROLLER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DATACONTROLLER.M with the given input arguments.
%
%      DATACONTROLLER('Property','Value',...) creates a new DATACONTROLLER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before datacontroller_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to datacontroller_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help datacontroller

% Last Modified by GUIDE v2.5 12-Jun-2013 16:05:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @datacontroller_OpeningFcn, ...
                   'gui_OutputFcn',  @datacontroller_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before datacontroller is made visible.
function datacontroller_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to datacontroller (see VARARGIN)

% Choose default command line output for datacontroller
handles.output = hObject;
handles.setboxes = @setboxes;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes datacontroller wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = datacontroller_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in c1.
function c1_Callback(hObject, eventdata, handles)
% hObject    handle to c1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 1, get(hObject, 'String'));

% --- Executes on button press in c2.
function c2_Callback(hObject, eventdata, handles)
% hObject    handle to c2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 2, get(hObject, 'String'));

% --- Executes on button press in c3.
function c3_Callback(hObject, eventdata, handles)
% hObject    handle to c3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 3, get(hObject, 'String'));

% --- Executes on button press in c4.
function c4_Callback(hObject, eventdata, handles)
% hObject    handle to c4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 4, get(hObject, 'String'));

% --- Executes on button press in c5.
function c5_Callback(hObject, eventdata, handles)
% hObject    handle to c5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 5, get(hObject, 'String'));

% --- Executes on button press in c6.
function c6_Callback(hObject, eventdata, handles)
% hObject    handle to c6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 6, get(hObject, 'String'));

% --- Executes on button press in c7.
function c7_Callback(hObject, eventdata, handles)
% hObject    handle to c7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 7, get(hObject, 'String'));

% --- Executes on button press in c8.
function c8_Callback(hObject, eventdata, handles)
% hObject    handle to c8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 8, get(hObject, 'String'));


% --- Executes on button press in c9.
function c9_Callback(hObject, eventdata, handles)
% hObject    handle to c9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 9, get(hObject, 'String'));


% --- Executes on button press in c10.
function c10_Callback(hObject, eventdata, handles)
% hObject    handle to c10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 10, get(hObject, 'String'));


% --- Executes on button press in c11.
function c11_Callback(hObject, eventdata, handles)
% hObject    handle to c11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 11, get(hObject, 'String'));


% --- Executes on button press in c12.
function c12_Callback(hObject, eventdata, handles)
% hObject    handle to c12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updatebox(handles, hObject);
x = get(handles.figure1, 'UserData');
x.updatetemporaryplot(get(handles.plotlist, 'Value'), 12, get(hObject, 'String'));


% --- Executes on selection change in plotlist.
function plotlist_Callback(hObject, eventdata, handles)
% hObject    handle to plotlist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns plotlist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from plotlist
val = get(hObject, 'Value');
if val == 5
	set(handles.uipanel2, 'SelectedObject', handles.radiobutton2);
else
    set(handles.uipanel2, 'SelectedObject', handles.radiobutton1);
end
x = get(handles.figure1, 'UserData');
x.setguibuttons(handles, val);

% --- Executes during object creation, after setting all properties.
function plotlist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to plotlist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes when selected object is changed in uipanel2.
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel2 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
if hObject == handles.radiobutton2 % Individual Channels selected
    set(handles.plotlist, 'Value', 5);
    x.setguibuttons(handles, 5);
else
    set(handles.plotlist, 'Value', 1); %TODO: maybe make this smarter
    x.setguibuttons(handles, 1);
end

function setboxes(handles, avechan, subchan)
for i = 1:12
    setbox(getboxnum(handles, i), 'Off');
end
for i = 1:length(subchan)
    if subchan(i) ~= 0
        setbox(getboxnum(handles, subchan(i)), 'Subtract');
    end
end
for i = 1:length(avechan)
    if avechan(i) ~= 0
        setbox(getboxnum(handles, avechan(i)), 'On');
    end
end

function setbox(box, state)
switch state
    case 'On'
        set(box, 'String', 'On');
        set(box, 'BackgroundColor', 'green');
    case 'Off'
        set(box, 'String', 'Off');
        set(box, 'BackgroundColor', [0.3, 0.3, 0.3]);
    case 'Subtract'
        set(box, 'String', 'Subtract');
        set(box, 'BackgroundColor', 'red');
end

function box = getboxnum(handles, num)
switch num
    case 1
        box = handles.c1;
    case 2
        box = handles.c2;
    case 3
        box = handles.c3;
    case 4
        box = handles.c4;
    case 5
        box = handles.c5;
    case 6
        box = handles.c6;
    case 7
        box = handles.c7;
    case 8
        box = handles.c8;
    case 9
        box = handles.c9;
    case 10
        box = handles.c10;
    case 11
        box = handles.c11;
    case 12
        box = handles.c12;
end

function updatebox(handles, box)
if get(handles.plotlist, 'Value') < 5
    switch get(box, 'String')
        case 'On'
            set(box, 'String', 'Off');
            set(box, 'BackgroundColor', [0.3, 0.3, 0.3]);
        case 'Off'
            set(box, 'String', 'Subtract');
            set(box, 'BackgroundColor', 'red');
        case 'Subtract'
            set(box, 'String', 'On');
            set(box, 'BackgroundColor', 'green');
    end
else
    switch get(box, 'String')
        case 'On'
            set(box, 'String', 'Off');
            set(box, 'BackgroundColor', [0.3, 0.3, 0.3]);
        case 'Off'
            set(box, 'String', 'On');
            set(box, 'BackgroundColor', 'green');
    end
end
        


% --- Executes on button press in updatebutton.
function updatebutton_Callback(hObject, eventdata, handles)
% hObject    handle to updatebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.updatecurrentplots();


% --- Executes on button press in cancelbutton.
function cancelbutton_Callback(hObject, eventdata, handles)
% hObject    handle to cancelbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
set(hObject, 'Visible', 'Off');

