% TODO: turn into class or package


% imageDir = 'A:\AOSShare\IARPA-CORE3D\Workspace\Results\Metrics25D\2017APR03_ISPRS\gridMax\subMesh';
imageDir = 'A:\AOSShare\IARPA-CORE3D\Workspace\Results\Metrics25D\2017APR03_ISPRS\gridMax\fullCloud';

 ppt = actxserver('PowerPoint.Application');
 
 % Create New Presn
ppt.Activate;
 
pres = ppt.Presentations.Add;

pptLayout = pres.SlideMaster.CustomLayouts;
layoutNames = cell(1,pptLayout.Count);
for p = 1:pptLayout.Count
    layoutNames{p} = pptLayout.Item(p).Name;
end
layoutTitleOnly = pptLayout.Item(6);

%%
pptTrue  = 'msoTrue';
pptFalse = 'msoFalse';

colorFun = @(r,g,b)r+g*256+b*256*256;


imageList = {'ref_dsm.png'
             'ref_dtm.png'
             'ref_hgt.png'
             'ref_mask.png'
             'ref_mask_hgt.png'
             };

thisSlide = pres.Slides.AddSlide(pres.Slides.Count+1,layoutTitleOnly);

% AddPicture(        FileName, LinkToFile,  SaveWithDocument, Left,   Top,    Width, Height)
% AddPicture(handle, string,   MsoTriState, MsoTriState,      single, single, Variant(Optional))
hPicts = cell(1, numel(imageList));
for loopImageList = 1:numel(imageList)
    imageFilename= fullfile(imageDir, imageList{loopImageList});
    thisPic = thisSlide.Shapes.AddPicture(imageFilename, pptFalse, pptTrue, 1 , 1);
    thisPic.PictureFormat.CropTop = 30;
    thisPic.PictureFormat.CropBottom = 30;
    thisPic.PictureFormat.CropLeft = 30;
    thisPic.PictureFormat.CropRight = 30;

    thisPic.Top = 200;
    thisPic.Left = 300;

    thisPic.LockAspectRatio = true;
    thisPic.Width = 200;

     thisPic.Line.Weight = 0;
%     thisPic.Line.Weight = 20;
%     thisPic.Line.ForeColor.RGB = colorFun(200,0,220);

    hPicts{loopImageList} = thisPic;
end
    
%%

nrows = 2; ncols = 3;
for p = 1:numel(hPicts)
    thisPic = hPicts{p};
    
    c = mod(p-1, ncols) + 1;
    r = floor((p - 1)/ncols) + 1;
    
    thisPic.Top = 150 + 200*(r - 1);
    thisPic.Left = 30 + 220*(c - 1);
    
    thisPic.LockAspectRatio = true;
    thisPic.Width = 200;
   
    thisPic.PictureFormat.CropTop = 5;
    thisPic.PictureFormat.CropBottom = 5;
    thisPic.PictureFormat.CropLeft = 20;
    thisPic.PictureFormat.CropRight = 30;
    
end

thisSlide.Shapes.Title.TextFrame.TextRange.Text = 'Reference Data';



%%  model pre-processing images

modelList = {'S2P', 'JHUAPL', 'ASP-SDRDIS', 'RSP'};

for loopModel = 1:numel(modelList)

    modelName = modelList{loopModel};
    
    imageList = {'_subclouds.png'
                 '_submeshes.png'
                 '_raster.png'
                 '_raster_inpainted_hgt.png'
                 };


    thisSlide = pres.Slides.AddSlide(pres.Slides.Count+1,layoutTitleOnly);
    hPicts = cell(1, numel(imageList));
    for loopImageList = 1:numel(imageList)
        imageFilename = fullfile(imageDir, sprintf('%s%s', modelName, imageList{loopImageList}));
        thisPic = thisSlide.Shapes.AddPicture(imageFilename, pptFalse, pptTrue, 1 , 1);
        thisPic.PictureFormat.CropTop = 30;
        thisPic.PictureFormat.CropBottom = 30;
        thisPic.PictureFormat.CropLeft = 30;
        thisPic.PictureFormat.CropRight = 30;

        thisPic.Top = 200;
        thisPic.Left = 300;

        thisPic.LockAspectRatio = true;
        thisPic.Width = 200;

         thisPic.Line.Weight = 0;
    %     thisPic.Line.Weight = 20;
    %     thisPic.Line.ForeColor.RGB = colorFun(200,0,220);

        hPicts{loopImageList} = thisPic;
    end

    %%  Format Slide
    nrows = 2; ncols = 2;
    for p = 1:numel(hPicts)
        thisPic = hPicts{p};

        c = mod(p-1, ncols) + 1;
        r = floor((p - 1)/ncols) + 1;

        thisPic.Top = 120 + 200*(r - 1);
        thisPic.Left = 60 + 250*(c - 1);

        thisPic.LockAspectRatio = true;
        thisPic.Width = 220;

        thisPic.PictureFormat.CropTop = 5;
        thisPic.PictureFormat.CropBottom = 0;
        thisPic.PictureFormat.CropLeft =  10;
        thisPic.PictureFormat.CropRight = 15;

    end

    thisSlide.Shapes.Title.TextFrame.TextRange.Text = sprintf('%s:  Data Preprocessing', modelName);

    
    
  %%   model pre-processing images

    imageList = {
            '_result.png'          
            '_result_error.png'         
             '_result_error_clipped.png'            
             '_result_label.png'
             };
  
         
    thisSlide = pres.Slides.AddSlide(pres.Slides.Count+1,layoutTitleOnly);
    hPicts  = cell(1, numel(imageList));
    hLabels = cell(1, numel(imageList));
    for loopImageList = 1:numel(imageList)
        imageFilename = fullfile(imageDir, sprintf('%s%s', modelName, imageList{loopImageList}));
        thisPic = thisSlide.Shapes.AddPicture(imageFilename, pptFalse, pptTrue, 1 , 1);
        thisPic.PictureFormat.CropTop = 30;
        thisPic.PictureFormat.CropBottom = 30;
        thisPic.PictureFormat.CropLeft = 30;
        thisPic.PictureFormat.CropRight = 30;

        thisPic.Top = 200;
        thisPic.Left = 300;

        thisPic.LockAspectRatio = true;
        thisPic.Width = 200;

        thisPic.Line.Weight = 0;
        
        hPicts{loopImageList} = thisPic;
    end
    
    

      %%  Format Slide
    
    thisPic = hPicts{1};
    
    thisPic.PictureFormat.CropTop = 125;
    thisPic.PictureFormat.CropBottom = 155;
    thisPic.PictureFormat.CropLeft =  40;
    thisPic.PictureFormat.CropRight = 20;
    
    thisPic.Top = 320;
    thisPic.Left = 10;

    thisPic.LockAspectRatio = true;
    thisPic.Width = 700;


      
    nrows = 1; ncols = 3;
    for p = 2:numel(hPicts)
        thisPic = hPicts{p};

        c = mod(p-1-1, ncols) + 1;
        r = floor((p - 1)/ncols) + 1;

        thisPic.Top = 120;
        thisPic.Left = 15 + 235*(c - 1);

        thisPic.LockAspectRatio = true;
        thisPic.Width = 230;

        thisPic.PictureFormat.CropTop = 5;
        thisPic.PictureFormat.CropBottom = 0;
        thisPic.PictureFormat.CropLeft =  10;
        thisPic.PictureFormat.CropRight = 15;

    end

    thisSlide.Shapes.Title.TextFrame.TextRange.Text = sprintf('%s:  Results', modelName);
end
    %%
 
data = load(fullfile(imageDir, 'results.mat')); 
thisSlide = pres.Slides.AddSlide(pres.Slides.Count+1,layoutTitleOnly);
thisSlide.Shapes.Title.TextFrame.TextRange.Text = sprintf('Results');

numRows = numel(fieldnames(data.metrics2D))*2;
numCols = numel(data.modelNames);

thisTable = thisSlide.Shapes.AddTable(numRows+1, numCols+1, 120, 30, 500, 350);
      


tableData = cell(numRows+1, numCols+1);

tableData(1,2:end) = data.modelNames;
tableData(2:end,1) = [fieldnames(data.metrics2D); fieldnames(data.metrics3D)];

fields = fieldnames(data.metrics2D);
for c = 1:numCols
    for r = 1:numRows
        
        if r < 9
            tableData{r+1, c+1} = data.metrics2D(c).(fields{r});
        else
            tableData{r+1, c+1} = data.metrics3D(c).(fields{r-8});
        end
    end
end
 
for c = 1:(numCols+1)
    for r = 1:(numRows+1)
        thisCell = thisTable.Table.Cell(r,c);
        if r == 1 & c == 1
            continue
        end
        thisData = tableData{r,c};
        if ischar(thisData)
            thisCell.Shape.TextFrame.TextRange.Text =  thisData;
            thisCell.Shape.TextFrame.TextRange.Font.Size = 11;
        else
            thisCell.Shape.TextFrame.TextRange.Text =  sprintf('%0.3f', thisData);

            
            if thisData == max([tableData{r,2:end}])
%                 thisCell.Shape.TextFrame.TextRange.Font.Bold = true;
            end
            thisCell.Shape.TextFrame.TextRange.Font.Size = 10;
        end
        
        
%         thisCell
    end
end

thisTable.Top = 130;

%% Table 2 - 3D v 3D

 
thisSlide = pres.Slides.AddSlide(pres.Slides.Count+1,layoutTitleOnly);
thisSlide.Shapes.Title.TextFrame.TextRange.Text = sprintf('Results');

numRows = numel(fieldnames(data.metrics2D));
numCols = numel(data.modelNames)*2;

thisTable = thisSlide.Shapes.AddTable(numRows+1, numCols+1, 120, 20, 520, 350);
      


tableData = cell(numRows+1, numCols+1);

tableData(1,2:end) = [data.modelNames' data.modelNames'];
tableData(2:end,1) = [fieldnames(data.metrics2D)];

fields = fieldnames(data.metrics2D);
for c = 1:numCols
    for r = 1:numRows
        
        if c < 5
            tableData{r+1, c+1} = data.metrics3D(c).(fields{r});
        else
            tableData{r+1, c+1} = data.metrics3Ds(c-4).(fields{r});
        end
    end
end
 
for c = 1:(numCols+1)
    for r = 1:(numRows+1)
        thisCell = thisTable.Table.Cell(r,c);
        if r == 1 & c == 1
            continue
        end
        thisData = tableData{r,c};
        if ischar(thisData)
            thisCell.Shape.TextFrame.TextRange.Text =  thisData;
            thisCell.Shape.TextFrame.TextRange.Font.Size = 11;
        else
            thisCell.Shape.TextFrame.TextRange.Text =  sprintf('%0.3f', thisData);

            
            if thisData == max([tableData{r,2:end}])
%                 thisCell.Shape.TextFrame.TextRange.Font.Bold = true;
            end
            thisCell.Shape.TextFrame.TextRange.Font.Size = 10;
        end
        
        
%         thisCell
    end
end

thisTable.Top = 130;
    
%%

saveFilename = fullfile(imageDir, 'results.pptx');
pres.SaveAs(saveFilename)

% pres.Close

      
    %%
%     
% % AddTextbox(Orientation, Left, Top, Width, Height)
% % AddTextbox(handle, MsoTextOrientation, single, single, single, single)
% % msoTextOrientationDownward 
% % msoTextOrientationHorizontal 
% % msoTextOrientationUpward 
% % msoTextOrientationVertical 
% thisText = thisSlide.Shapes.AddTextbox('msoTextOrientationDownward', 100 , 100, 10, 100);
% thisText.TextFrame.TextRange.Text = 'This is a Textbox';
% 
% 
% thisText = thisSlide.Shapes.AddTextbox('msoTextOrientationHorizontal', 100 , 100, 100, 10);
% thisText.TextFrame.TextRange.Text = 'This is a Textbox';
% thisText.Width = 200;
% thisText.Height = 100;
% thisText.Top = 120;
% thisText.Left = 300;
% thisText.Fill.ForeColor.RGB = colorFun(200,0,0);
% thisText.Line.Weight = 12;
% thisText.Line.ForeColor.RGB = colorFun(0,222,0);
% 
% %
% % Save = void Save(handle)
% % SaveAs = void SaveAs(handle, string, Variant(Optional))
% % SaveCopyAs = void SaveCopyAs(handle, string, Variant(Optional))
% saveFilename = 'test.pptx';
% pres.SaveAs(saveFilename)
% 
%  pres.Close
