% saveColorMap.m This function save each colormap to proper folder with
% proper name and title

function [ ] = saveColorMap( recordedFileName, filterMatrixFileName, mapsFolder, lowPassCutFreq, bandpassLowCutFreq, ...
                             bandpassHighCutFreq, centerFreq, imageIndex, refName, aWeightingEnable, algorithm )

    Position                = recordedFileName( 1 : end-4 );
    if exist( 'algorithm', 'var' ) == 0
        PositionFolder      = [ recordedFileName( 1:end-4 ) '_' filterMatrixFileName( 1:end-4 ) '_' refName '_sps' ];
    else
        PositionFolder      = [ recordedFileName( 1:end-4 ) '_' filterMatrixFileName( 1:end-4 ) '_' refName '_' algorithm ];
    end
    
    if imageIndex == 1
        
        if aWeightingEnable
            title( [ 'Unfiltered map - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Aweighted [dB(A)]' ] );
        else
            title( [ 'Unfiltered map - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Unweighted [dB]' ] );
        end
        
        mapsFolderPosition  = [ mapsFolder 'byPosition/' ];
        mkdir( [ mapsFolderPosition PositionFolder ] );
        print( [ mapsFolderPosition PositionFolder filesep 'Unfiltered_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' );

        mapsFolderPosition  = [ mapsFolder 'byFrequency/' ];
        mkdir( [ mapsFolderPosition 'Unfiltered/' ] );      
        print( [ mapsFolderPosition 'Unfiltered' filesep 'Unfiltered_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' );  

    elseif imageIndex == 2 && lowPassCutFreq ~= 0
        if aWeightingEnable
            title( [ 'Low pass filtered map at ' num2str( lowPassCutFreq ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Aweighted [dB(A)]' ] );
        else
            title( [ 'Low pass filtered map at ' num2str( lowPassCutFreq ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Unweighted [dB]' ] );
        end
        
        mapsFolderPosition  = [ mapsFolder 'byPosition/' ];
        mkdir( [ mapsFolderPosition PositionFolder ] );
        print( [ mapsFolderPosition PositionFolder filesep 'LowpassFiltered_' num2str( lowPassCutFreq ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' ); 

        mapsFolderPosition  = [ mapsFolder 'byFrequency/' ];
        mkdir( [ mapsFolderPosition 'LowPass/' ] );  
        print( [ mapsFolderPosition 'LowPass' filesep 'LowpassFiltered_' num2str( lowPassCutFreq ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' ); 

    elseif imageIndex == 2 && lowPassCutFreq == 0 && bandpassLowCutFreq ~= 0 && bandpassHighCutFreq ~= 0 || ...
           imageIndex == 3 && lowPassCutFreq ~= 0 && bandpassLowCutFreq ~= 0 && bandpassHighCutFreq ~= 0
        if aWeightingEnable
            title( [ 'Bandpass filtered map between ' num2str( bandpassLowCutFreq ) 'Hz and ' num2str( bandpassHighCutFreq ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Aweighted [dB(A)]' ] );
        else
            title( [ 'Bandpass filtered map between ' num2str( bandpassLowCutFreq ) 'Hz and ' num2str( bandpassHighCutFreq ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Unweighted [dB]' ] );
        end
        
        mapsFolderPosition  = [ mapsFolder 'byPosition/' ];
        mkdir( [ mapsFolderPosition PositionFolder ] );
        print( [ mapsFolderPosition PositionFolder filesep 'BandpassFiltered_' num2str( bandpassLowCutFreq ) '_' num2str( bandpassHighCutFreq ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' ); 

        mapsFolderPosition  = [ mapsFolder 'byFrequency/' ];
        mkdir( [ mapsFolderPosition 'BandPass/' ] );  
        print( [ mapsFolderPosition 'BandPass' filesep 'BandpassFiltered_' num2str( bandpassLowCutFreq ) '_' num2str( bandpassHighCutFreq ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' );

    else

        if lowPassCutFreq == 0 && ( bandpassLowCutFreq == 0 || bandpassHighCutFreq == 0 )
            count           = 1;
        elseif lowPassCutFreq ~= 0 && ( bandpassLowCutFreq == 0 || bandpassHighCutFreq == 0 )
            count           = 2;
        elseif lowPassCutFreq == 0 && bandpassLowCutFreq ~= 0 && bandpassHighCutFreq ~= 0
            count           = 2;
        else
            count           = 3;
        end
        if aWeightingEnable
            title( [ 'Octave band filtered map, center freq. ' num2str( centerFreq( imageIndex - count ) ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Aweighted [dB(A)]' ] );
        else
            title( [ 'Octave band filtered map, center freq. ' num2str( centerFreq( imageIndex - count ) ) 'Hz - ' filterMatrixFileName(1:end-4) ' - ' recordedFileName(1:end-4) ' - ' refName ' - Unweighted [dB]' ] );
        end
        
        mapsFolderPosition  = [ mapsFolder 'byPosition/' ];
        mkdir( [ mapsFolderPosition PositionFolder ] );
        print( [ mapsFolderPosition PositionFolder filesep 'OctaveFiltered_' num2str( centerFreq( imageIndex - count ) ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' ); 

        mapsFolderPosition  = [ mapsFolder 'byFrequency/' ];
        octave              = [ 'Octave_' num2str( centerFreq( imageIndex - count ) ) ];
        mkdir( [ mapsFolderPosition octave ] ); 
        print( [ mapsFolderPosition octave filesep 'OctaveFiltered_' num2str( centerFreq( imageIndex - count ) ) '_' Position '_' filterMatrixFileName(1:end-4) '_' refName '.png' ], '-dpng' ); 

    end

end