function demo(hObject, ~, handles)

% #########################################################################
%#######################   GUI   ###########################
%##########################################################################

handles.fh = figure('units','pixels',...
    'position',[450 200 188 450],...
    'menubar','none',...
    'name','BeWish',...
    'numbertitle','off',...
    'resize','off');

    uicontrol('Style','edit',...                   
    'Position',[11 310 168 100],...
    'string','',...
    'backgroundcolor',[.95 .95 .95]);

    uicontrol('style','text',...                   
    'Position',[14 390 100 15],...
    'fontweight','bold',...
    'fontsize',9,...
    'string','Excel-Tables');

    handles.exc_1 = uicontrol('style','push',...         
    'units','pix',...
    'position',[20 355 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','load 1. table',...
    'callback',@exc_1_call);

    handles.exc_2 = uicontrol('style','push',...         
    'units','pix',...
    'position',[20 315 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','load 2. table',...
    'callback',@exc_2_call);

    uicontrol('Style','edit',...                   
    'Position',[11 240 168 60],...
    'string','',...
    'backgroundcolor',[.95 .95 .95]);

    uicontrol('style','text',...                  
    'Position',[15 280 147 15],...
    'fontweight','bold',...
    'fontsize',9,...
    'string','AB-cdefghi  compare');

    handles.df = uicontrol('style','push',...             
    'units','pix',...
    'position',[20 245 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','Start',...
    'callback',@df_call);

    uicontrol('Style','edit',...                   
    'Position',[11 170 168 60],...
    'string','',...
    'backgroundcolor',[.95 .95 .95]);

    uicontrol('style','text',...                   
    'Position',[15 210 144 15],...
    'fontweight','bold',...
    'fontsize',9,...
    'string','no function for now');

    handles.ki = uicontrol('style','push',...            
    'units','pix',...
    'position',[20 175 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','no function for now',...
    'callback',@ki_call);

    uicontrol('Style','edit',...                   
    'Position',[11 100 168 60],...
    'string','',...
    'backgroundcolor',[.95 .95 .95]);

    uicontrol('style','text',...                   
    'Position',[15 140 147 15],...
    'fontweight','bold',...
    'fontsize',9,...
    'string','no function for now');

    handles.ml = uicontrol('style','push',...            
    'units','pix',...
    'position',[20 105 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','no function for now',...
    'callback',@ml_call);


    uicontrol('Style','edit',...                   
    'Position',[11 10 168 60],...
    'string','',...
    'backgroundcolor',[.95 .95 .95]);

    uicontrol('style','text',...                   
    'Position',[15 50 135 15],...
    'fontweight','bold',...
    'fontsize',9,...
    'string','no function for now');

    handles.start = uicontrol('style','push',...         
    'units','pix',...
    'position',[20 15 150 30],...
    'fontsize',10,...
    'fontweight','bold',...
    'string','no function for now',...
    'callback',@pre_call);

set(gcf,'color',[0.57 0.69 0.75]);

%##########################################################################
%#######################        Load files       ##########################
%##########################################################################

function exc_1_call (hObject, eventdata)
[filename_xlsx_1, pathname_xlsx_1] = uigetfile({'*.xlsx'},'load 1. table');
file = fullfile(pathname_xlsx_1, filename_xlsx_1);
handles.pathname_xlsx_1 = pathname_xlsx_1;

% read 1. table
[num_1,txt_1,raw_1] = xlsread(file);
handles.raw_1 = raw_1;
handles.DF_table_1 = readtable(file);

handles.excel_1 = actxserver('Excel.Application'); 

% handles.excel_1.Visible = true;    

handles.workbook_1 = handles.excel_1.Workbooks.Open(file);

handles.worksheet_1 = handles.workbook_1.Worksheets.Item(1);

handles.length_1 = size(handles.raw_1);                 
handles.length_1 = sprintf('%d',handles.length_1);      
handles.M_end_1 = (strcat('C6:','C', handles.length_1));  

handles.excelRange_1 = get(handles.worksheet_1,'Range', handles.M_end_1);       
handles.Cell_Inhalt_M_1 = handles.excelRange_1.Value;                   
 
N = length(handles.raw_1);
all_i = 6:N;
handles.ColorIndex = zeros(size(all_i));

for k = 1:numel(all_i)          
 i = all_i(k);
 m = strcat('C',num2str(i));
 handles.ColorIndex_1(k) = handles.worksheet_1.Range(m).Interior.ColorIndex;
 handles.ColorIndex_1 = transpose(handles.ColorIndex_1);
end

handles.eSheet = handles.excel_1.ActiveWorkbook.Sheets;           
handles.eSheet1 = Item(handles.eSheet,1);
handles.eNewSheet = Add(handles.eSheet,[],handles.eSheet1);
handles.workbook_1.Worksheets.Item(2).Name = 'deleted';

end

function exc_2_call (hObject, eventdata)
    
[handles.filename_xlsx_2, handles.pathname_xlsx_2] = uigetfile({'*.xlsx';'*.xls'},'load 2. table');
file = strcat(handles.pathname_xlsx_2, handles.filename_xlsx_2);

[num_2,txt_2,raw_2] = xlsread(file);
handles.raw_2 = raw_2;
handles.DF_table_2 = readtable(file);

handles.excel_2 = actxserver('Excel.Application'); 

% handles.excel_2.Visible = true;    

handles.workbook_2 = handles.excel_2.Workbooks.Open(file);

handles.worksheet_2 = handles.workbook_2.Worksheets.Item(1);

% ColorIndex
N = length(handles.raw_2);
all_i = 6:N;
handles.ColorIndex_2 = zeros(size(all_i));

for k = 1:numel(all_i)          
 i = all_i(k);
 m = strcat('C',num2str(i));
 handles.ColorIndex_2(k) = handles.worksheet_2.Range(m).Interior.ColorIndex;
 handles.ColorIndex_2 = transpose(handles.ColorIndex_2);
end
end

%##########################################################################
%#####################         Comparison          ########################
%##########################################################################

function  [] = df_call (hObject, eventdata)

handles.ID_1 = handles.raw_1(6:end,2);     
handles.ID_2 = handles.raw_2(6:end,2);     

handles.ID_find = ismember(handles.ID_1, handles.ID_2);          
handles.ID_find = double(handles.ID_find);                  
[ID_row_zeros] = find(handles.ID_find == 0);   
ID_row_zeros = ID_row_zeros + 5 ;
handles.ID_deleted = isempty(ID_row_zeros);           

ID_row_zeros_str = string(num2str(ID_row_zeros));
ID_deleted_cells = strcat('C', ID_row_zeros_str);


%##########   copy the first 5 rows    ############
handles.worksheet_1.Rows.Item(1).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A1').PasteSpecial(13);
handles.worksheet_1.Rows.Item(2).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A2').PasteSpecial(13);
handles.worksheet_1.Rows.Item(3).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A3').PasteSpecial(13);
handles.worksheet_1.Rows.Item(4).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A4').PasteSpecial(13);
handles.worksheet_1.Rows.Item(5).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A5').PasteSpecial(13);
%##########

%##########   paste deleted rows to the 2nd sheet    ##############

% handles.worksheet_1.Rows.Item(ID_row_zeros(1,1)).Copy;
% handles.workbook_1.Worksheets.Item(2).Range('A6:A8').PasteSpecial(13);

handles.worksheet_1.Rows.Item(ID_row_zeros(1,1)).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A6').PasteSpecial(13);

handles.worksheet_1.Rows.Item(ID_row_zeros(2,1)).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A7').PasteSpecial(13);

handles.worksheet_1.Rows.Item(ID_row_zeros(3,1)).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A8').PasteSpecial(13);

handles.worksheet_1.Rows.Item(ID_row_zeros(4,1)).Copy;
handles.workbook_1.Worksheets.Item(2).Range('A9').PasteSpecial(13);

% handles.workbook_1.Worksheets.Item(2).Rows.Item(10).EntireRow.Delete;


% Date and calendar week
handles.datum = datetime;       % date
handles.kw = week (handles.datum);      % calendar week
handles.kw = string(handles.kw);

% save as
handles.new_name = strcat(handles.pathname_xlsx_1,'KW',handles.kw,{' '}, {'-'}, {' '}, 'ABCDEFG', {' '}, {'-'}, {' '}, datestr(now,'dd.mm.yyyy'),'.xlsx');
handles.new_name = char(handles.new_name);
handles.workbook_1.SaveAs(handles.new_name);
%end

% Close Workbook
handles.workbook_1.Close();

% Quit Excel
handles.excel_1.Quit();
 
% open the new table
winopen(handles.new_name);
end
end


