function varargout = addControllers(varargin)
% addControllers  add new controller
%   addControllers opens up a window to add a new controller  

%   Project: IMPACT
%   Title of Code: addControllers
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @addControllers_OpeningFcn, ...
                   'gui_OutputFcn',  @addControllers_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 addControllers is made visible.
function addControllers_OpeningFcn(hObject, eventdata, handles, varargin)
% load Icon
addIMPACTIcon(hObject);
hFig = findobj('type', 'figure', 'tag', 'controller_design');
oldHandles = guidata(hFig);
handles.oldHandles = oldHandles;

mode = varargin{1};
switch mode
    case 'OK' % use current controller
        cEdits = {'ptoCont','floaterImpedance','sparImpedance','floaterFexc','sparFexc',...
            'ptoContX','floaterImpedanceX','sparImpedanceX','floaterFexcX','sparFexcX'};
        for iCont=1:numel(cEdits)
            stEdit = eval(sprintf('get(oldHandles.edit_%s, ''String'');',cEdits{iCont}));
            eval(sprintf('set(handles.edit_%s, ''String'',stEdit);',cEdits{iCont}));
        end       
    otherwise

end

nCs = getCNum(oldHandles);
nCs = nCs+1;
str = sprintf('Controller %d', nCs);
set(handles.edit_name, 'String', str);

% Choose default command line output for addControllers
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes addControllers wait for user response (see UIRESUME)
% uiwait(handles.figure1);
movegui(hObject, 'center');


% --- Outputs from this function are returned to the command line.
function varargout = addControllers_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;


function [nCs, strCs]=getCNum(oldHandles)
% Get number of controller group

strCs = get(oldHandles.popupmenu_controller, 'String');
if ~iscell(strCs)
    strCs = {strCs};
end
strCs(cellfun('isempty', strCs)) = [];
nCs = numel(strCs);




function edit_ptoCont_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'PTO Controller');


% --- Executes during object creation, after setting all properties.
function edit_ptoCont_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_floaterImpedance_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Floater Impedance');


% --- Executes during object creation, after setting all properties.
function edit_floaterImpedance_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_floaterFexc_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Gtar');


% --- Executes during object creation, after setting all properties.
function edit_floaterFexc_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
success = checkVal(handles);
if ~success
    return
end

%% set controllers
oldHandles = handles.oldHandles;
if isempty(oldHandles)
    close;
end

% controller group string
[nCs, strCs] = getCNum(oldHandles);
nCs = nCs+1;
strGroup = get(handles.edit_name, 'String');
if isempty(strGroup)
    errordlgMLS('Please enter a name for this controller group.');
    return;
end
strCs = [strCs; strGroup];
set(oldHandles.popupmenu_controller, 'String', strCs, 'Value', nCs);

% controller tf
stPtoCont = get(handles.edit_ptoCont, 'String');
set(oldHandles.edit_ptoCont, 'String', stPtoCont);
stFloaterImpedance = get(handles.edit_floaterImpedance, 'String');
set(oldHandles.edit_floaterImpedance, 'String', stFloaterImpedance);
stSparImpedance = get(handles.edit_sparImpedance, 'String');
set(oldHandles.edit_sparImpedance, 'String', stSparImpedance);
stFloaterFexc = get(handles.edit_floaterFexc, 'String');
set(oldHandles.edit_floaterFexc, 'String', stFloaterFexc);
stSparFexc = get(handles.edit_sparFexc, 'String');
set(oldHandles.edit_sparFexc, 'String', stSparFexc);

stPtoContX = get(handles.edit_ptoContX, 'String');
set(oldHandles.edit_ptoContX, 'String', stPtoContX);
stFloaterImpedanceX = get(handles.edit_floaterImpedanceX, 'String');
set(oldHandles.edit_floaterImpedanceX, 'String', stFloaterImpedanceX);
stSparImpedanceX = get(handles.edit_sparImpedanceX, 'String');
set(oldHandles.edit_sparImpedanceX, 'String', stSparImpedanceX);
stFloaterFexcX = get(handles.edit_floaterFexcX, 'String');
set(oldHandles.edit_floaterFexcX, 'String', stFloaterFexcX);
stSparFexcX = get(handles.edit_sparFexcX, 'String');
set(oldHandles.edit_sparFexcX, 'String', stSparFexcX);
% GS
hFig = findobj('type', 'figure', 'tag', 'controller_design');
newHandles = guidata(hFig);

%%
% controllers
Cs.name = strGroup;
Cs.ptoCont = stPtoCont;
Cs.floaterImpedance = stFloaterImpedance;
Cs.sparImpedance = stSparImpedance;
Cs.floaterFexc = stFloaterFexc;
Cs.sparFexc = stSparFexc;


Cs.floaterImpedanceX = stFloaterImpedanceX;
Cs.sparImpedanceX = stSparImpedanceX;
Cs.floaterFexcX = stFloaterFexcX;
Cs.sparFexcX = stSparFexcX;
Cs.ptoContX = stPtoContX;
newHandles.Design.Cs(nCs) = Cs;

guidata(hFig, newHandles);
close(ancestor(hObject, 'figure'));

function success = checkVal(handles)
% This is used to check controller transfer functions

success = 0;

%% check tf
stCell{1,1} = get(handles.edit_ptoCont, 'String');
stCell{2,1} = get(handles.edit_floaterImpedance, 'String');
stCell{3,1} = get(handles.edit_sparImpedance, 'String');
stCell{4,1} = get(handles.edit_floaterFexc, 'String');
stCell{5,1} = get(handles.edit_sparFexc, 'String');
stCell{6,1} = get(handles.edit_ptoContX, 'String');
stCell{7,1} = get(handles.edit_floaterImpedanceX, 'String');
stCell{8,1} = get(handles.edit_sparImpedanceX, 'String');
stCell{9,1} = get(handles.edit_floaterFexcX, 'String');
stCell{10,1} = get(handles.edit_sparFexcX, 'String');

stCell{1,2} = 'Pto controller (C1)';
stCell{2,2} = 'Floater impedance';
stCell{3,2} = 'Spar impedance';
stCell{4,2} = 'Floater excitation';
stCell{5,2} = 'Spar excitation';
stCell{6,2} = 'Pto controller X (C2)';
stCell{7,2} = 'Floater impedance X';
stCell{8,2} = 'Spar impedance X';
stCell{9,2} = 'Floater excitation X';
stCell{10,2} = 'Spar excitation X';


for m=1:size(stCell,1);
    str = stCell{m, 1};
    name = stCell{m, 2};
    if ~(checkTf(str, name))
        return;
    end
end



%% success
success = 1;

% --- Executes on button press in pushbutton_cancel.
function pushbutton_cancel_Callback(hObject, eventdata, handles)
close;


function edit_name_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_name_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit_sparImpedance_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Spar Impedance');



% --- Executes during object creation, after setting all properties.
function edit_sparImpedance_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end






function edit_sparFexc_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Spar Excitation');


% --- Executes during object creation, after setting all properties.
function edit_sparFexc_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit_floaterImpedanceX_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Floater impedance X');

% --- Executes during object creation, after setting all properties.
function edit_floaterImpedanceX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_floaterFexcX_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Floater excitation X');

% --- Executes during object creation, after setting all properties.
function edit_floaterFexcX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_sparImpedanceX_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Spar impedance X');

% --- Executes during object creation, after setting all properties.
function edit_sparImpedanceX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_sparFexcX_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'Spar excitation X');


% --- Executes during object creation, after setting all properties.
function edit_sparFexcX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_ptoContX_Callback(hObject, eventdata, handles)
checkTf(get(hObject, 'String'), 'X Controller (C2)');


% --- Executes during object creation, after setting all properties.
function edit_ptoContX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
