function helpview(path, varargin)
%HELPVIEW Displays an HTML file in the Help browser.
%
%SYNTAX
%
%  helpview(short_name, topic_id)
%
%ARGUMENTS
%
%  short_name
%    Short name for a product or installed support package. For example,
%      matlab
%      beagleboard
%
%  topic_id
%    The topic_id is an identifying string in the documentation source
%    for the HTML section to open. Coordinate with the writer to define
%    the ID, which the writer should include in an anchor element. IDs 
%    must be unique within a product.
%
%EXAMPLES
%
%    helpview('matlab','panning');
%    helpview('matlab','matlab_data_types');

%   Copyright 1984-2020 The MathWorks, Inc.

% Make sure that we can support the doc command on this platform.
if ~usejava('mwt')
    error(message('MATLAB:doc:UnsupportedPlatform',upper(mfilename)));
end

wintype = '';
parent = '';

if nargin < 1
    error(message('MATLAB:helpview:NotEnoughInputArgs'));
end

if isstring(path) && isscalar(path)
    path = char(path);
end

stringInputs = cellfun(@(x)isstring(x) && isscalar(x), varargin);
if any(stringInputs)
    varargin(stringInputs) = cellfun(@char, varargin(stringInputs), 'UniformOutput', false);
end

if ~ischar(path) || strcmp(path, '')
    error(message('MATLAB:helpview:InvalidFirstArg'));
end

% Get input parameters separated from size and location
% Note:  this is to be backwards compatible
[inputArgs, csh_size, csh_location] = getInputArgs(varargin);
helpTopicMap = matlab.internal.doc.csh.HelpTopicMap.fromTopicPath(path);

if ~exists(helpTopicMap) && ~startsWith(path,"mapkey:")
    % Direct path to a documentation file.
    help_path = normalize_path(path);
elseif isempty(inputArgs)
    error(message('MATLAB:helpview:TopicIdRequired'));
else
    topic_id = inputArgs{1};
    inputArgs(1) = [];

    help_path = mapTopic(helpTopicMap, topic_id);
    if isempty(help_path) || help_path == ""
        errorId = 'MATLAB:helpview:TopicPathDoesNotExist';
        errorMsg = getString(message('MATLAB:helpview:TopicIdDoesNotExist', topic_id, path));
        error(errorId, '%s', errorMsg);
    end
end

if ~isempty(inputArgs)
    wintype = inputArgs{1};
    if ~ischar(wintype)
        if exists(helpTopicMap)
            error(message('MATLAB:helpview:InvalidThirdParameter'));
        else
            error(message('MATLAB:helpview:InvalidSecondParameter'));
        end
    end
end    

% Get parent argument, if specified.
if (length(inputArgs) > 1)
    parent = inputArgs{2};
end
        
% Call the help viewer.

% Use the appropriate viewer, based on window type.
if strcmp(wintype,'CSHelpWindow')
    openCSHelpWindow(help_path, parent, csh_size, csh_location);
else
    % Otherwise, use the main Help window.
    web(help_path, '-helpbrowser');
end

%--------------------------------------------------------------------------

function normal_path = normalize_path(path)
    if (strfind(path, 'http') == 1)
       normal_path = path; 
       return;
    end
    
    % Ensures that path uses the correct separator
    % for the current platform.
    %
    normal_path = regexprep(path, '[\\/]', filesep);

%--------------------------------------------------------------------------

function [inputArgs, csh_size, csh_location] = getInputArgs(originalInputs)

% init parameters
csh_size = [];
inputArgs = [];
csh_location = [];

i = 1;
while i <= length(originalInputs)
    if ~ischar(originalInputs{i})
       % avoid non character
       inputArgs{end+1} = originalInputs{i};
    elseif strcmp(originalInputs{i}, 'position')
        i = i + 1;
        if i > length(originalInputs)
            error(message('MATLAB:helpview:UnspecifiedPosition'));
        elseif ~(length(originalInputs{i}) == 4)
            error(message('MATLAB:helpview:InvalidPosition'));
        else
            % [left bottom width height]
            scrsz = get(0,'ScreenSize');
            v = originalInputs{i};
            csh_size = [v(3) v(4)];
            % watch out for small screen size, otherwise use default
            if (scrsz(4)-v(2)-v(4)) > -1 
               csh_location = [v(1) (scrsz(4)-v(2)-v(4))];
            elseif (scrsz(4)-v(4)) > -1
               csh_location = [v(1) (scrsz(4)-v(4))]; 
            end
        end
    elseif strcmp(originalInputs{i}, 'size')
        i = i + 1;
        if i > length(originalInputs)
            error(message('MATLAB:helpview:UnspecifiedSize'));
        else
            csh_size = originalInputs{i};
        end
    elseif strcmp(originalInputs{i}, 'location')
        i = i + 1;
        if i > length(originalInputs)
            error(message('MATLAB:helpview:UnspecifiedLocation'));
        else
            csh_location = originalInputs{i};
        end
    else
        inputArgs{end+1} = originalInputs{i};
    end
    i = i + 1;
end

% validate size
if ~isempty(csh_size)
    if ~(length(csh_size) == 2)
            error('MATLAB:helpview:InvalidSize', '%s', getString(message('MATLAB:helpview:SizeMustBeWidthHeight'))); 
    elseif min(csh_size) < 1
        error('MATLAB:helpview:InvalidSize', '%s', getString(message('MATLAB:helpview:SizeMustBePositive')));
    end
end

% validate location
if ~isempty(csh_location)
    if ~(length(csh_location) == 2)
        error('MATLAB:helpview:InvalidLocation', '%s', getString(message('MATLAB:helpview:LocationMustBeXY')));
    elseif min(csh_location) < 0
        error('MATLAB:helpview:InvalidLocation', '%s', getString(message('MATLAB:helpview:LocationMustBePositive')));
    end
end

%--------------------------------------------------------------------------

function openCSHelpWindow(path, parent, csh_size, csh_location)

normal_path = normalize_path(path);
openHelpViewPage(normal_path, parent, csh_size, csh_location);

% End of helpview.m
