function TemperatureConversionSimpleCallback(event)
% TemperatureConversionSimpleCallback - Callback called by
% modelit.webserver.Server object for converting Celcius to Fahrenheit and
% vice versa. This example generates the HTML page on the server.
%
% CALL:
%     TemperatureConversionSimpleCallback(event)
%
% INPUT:
%     event: <modelit.webserver.HttpExchange>
%       with the request data and methods to generate a response.
%
% OUTPUT:
%     No output, a response with a HTML is return to the client.
%
% EXAMPLE:
%  server = modelit.web.server.Server(8081, @TemperatureConversionSimpleCallback).start()
%
%  % Open a webbrowser and type: http://localhost:8081/ in the address bar

%   Copyright 2023 Modelit, www.modelit.nl

switch lower(event.getRequestMethod)
    case 'get'
        value = event.getQueryValue('value');
        unit = event.getQueryValue('unit');
        event.addResponseHeader('Content-Type', 'text/html',...
            'Access-Control-Allow-Origin', '*');
        event.send(200, HTML(value, unit));
        return;

end

% Send the response back to the client
event.send(404);

%__________________________________________________________________________
function str = HTML(value, unit)
% HTML - Generate the front-end. Depending on the request parameters.
%
% CALL:
%  str = HTML(value, unit)
%
% INPUT:
%   value: <string>
%     the temperature
%   unit: <string>
%     fahrenheit or celcius. These values are defined in the select control
%     in the HTML that is generated by this function.
%
% OUTPUT:
%   str: <string>
%     the HTML code for the front-end. This HTML contains a form that is send to
%     this function with the value and unit parameters.

answer = '';
if isempty(value) || isempty(unit)
    unit = 'celcius';
    value = '';
else
    value = str2double(value);
    switch lower(unit)
        case 'celcius'
            answer = sprintf('%g degrees Celcius is %g degrees Fahrenheit', value, value*1.8 + 32) ;
        case 'fahrenheit'
            answer = sprintf('%g degrees Fahrenheit is %g degrees Celcius', value, (value - 32)/1.8);
        otherwise
            answer = '';

    end
end
str = strcat('<html>',...
    '<body>',...
    '<h1>Celcius &#x2194; Fahrenheit</h1>',...
    '<form action="./temperatureconversionsimple">',...
    '<label for="unit">Unit:</label><br>',...
    '<select name="unit" id="unit">',...
    sprintf('<option value="celcius" %s>Celcius</option>', isSelected(unit, 'celcius')),...
    sprintf('<option value="fahrenheit" %s>Fahrenheit</option>', isSelected(unit, 'fahrenheit')),...
    '</select><br>',...
    '<label for="value">Temperature:</label><br>',...
    sprintf('<input type="text" id="value" name="value" value=%g> <br><br>',value),...
    '<input type="submit" value="Convert">',...
    '</form>',...
    answer,...
    '</body>',...
    '</html>');

%__________________________________________________________________________
function str = isSelected(unit, option)
% isSelected - Returns 'selected' to set the selection of the input control
% in the HTML form.

if strcmpi(unit, option)
    str = 'selected';
else
    str = '';
end