function cdef = editMiller(cdef, P)
% submenu for editing the Miller indices
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

clc
sel = 1;

while sel ~= 0
    %% main menu
    list = {'Add Face'};
    if isfield(cdef, 'millerDefined') && ~isempty(cdef.millerDefined)
        list{end+1} = 'Delete Face';
        list{end+1} = 'Summary';
    end
    disp('/---------------------------------->');
    disp('| Subsection: Edit Miller Indices');
    sel = selectFromList(list, 'Back');
    if sel == 0
        continue;
    end
    
    
    switch list{sel}
        
        case 'Add Face'
            %% add face  
            if ~isfield(cdef, 'millerDefined')
                cdef.millerDefined = [];
            end
            
            cdef.millerDefined(end+1,:) = selectMillerVector;
            disp(' ');
            completeMillerMatrixFromForm = ...
                executeExternBorchertFunction('form2face', ...
                cdef.millerDefined(end,:), P(2, cdef.sym.defined));
            strings = convertMillerToString(...
                completeMillerMatrixFromForm);
            
            if ~iscell(strings)
                disp('  No additional facets are created due to symmetry.');
            else
                disp('  This gives the following set of faces: ');
                str = '';
                for i = 1:length(strings)
                    str = [str ', ' strings{i}];
                end
                str(1:2) = '  ';
                disp(str);
            end
            pause
            clc
            
        case 'Delete Face'
            %% delete face
            disp(' ');
            disp('Please select the Miller indices to delete');
            list = {};
            for i = 1:size(cdef.millerDefined, 1)
                list{i} = convertMillerToString(...
                    cdef.millerDefined(i, :));
            end
            delindex = selectFromList(list, 'none');
            if delindex ~=0
                cdef.millerDefined(delindex, :) = [];
                disp('..face deleted');
            end
            clc

        case 'Summary'
            %% print summary
            printMillerSummary(cdef, P)
            pause
            clc
    end
end