classdef CodedItemCollection < handle
    
    properties
        arrayOfCodedItems
    end
    
    
    methods (Abstract)
        fcc(obj);
    end
    
    
    methods
        % public
        
        function resetscores(obj)
            nItems=obj.getarraysize;
            for iItem=1:nItems,
                obj.getelem(iItem).setscore(0);
            end
        end
        
        function o=getelem(obj,index)
            o=obj.arrayOfCodedItems{index};
        end
        
        function addmetrics(obj,mcoll)
            nItems=obj.getarraysize;
            for iItem=1:nItems,
                obj.arrayOfCodedItems{iItem}.addmetrics(mcoll);
            end
        end
        
        function calcmetrics(obj)
            nItems=obj.getarraysize;
            for iItem=1:nItems,
                obj.arrayOfCodedItems{iItem}.calcmetrics;
            end
        end
        
        
        function obj=plus(obj,obj2)
            if isa(obj2,'CodedItem')
                nLast=obj.getarraysize;
                obj.arrayOfCodedItems{nLast+1}=obj2;
            elseif isa(obj2,'CodedItemCollection')
                obj.arrayOfCodedItems=[obj.arrayOfCodedItems obj2.arrayOfCodedItems];
            else
                error('Wrong object type')
            end
            
        end
        
        
        function n=getarraysize(obj)
            n=length(obj.arrayOfCodedItems);
        end
        
        
        function pairs=makepairs(obj)
            n=obj.getarraysize;
            
            %allpairs=[kron([1:1:n],ones(1,n)); kron(ones(1,n),[1:1:n])].';
            allpairs=[];
            for ii=1:n,
                for jj=ii+1:n,
                    allpairs=[allpairs; ii jj];
                end
            end
            npairs=size(allpairs,1);
            
            validpairs=[];
            for iPair=1:npairs,
                i1=allpairs(iPair,1);
                i2=allpairs(iPair,2);
                
                if strcmp(obj.getelem(i1).getreference.getfilename,obj.getelem(i2).getreference.getfilename)
                    validpairs=[validpairs; i1 i2];
                end
                
            end
            
            npairs=size(validpairs,1);
            randind=randperm(npairs);
            
            pairs=validpairs(randind,:);
            
            
        end
        
        function referencenames=getuniquereferencenames(obj)
            n=obj.getarraysize;
            for iT=1:n,
                referencenames{iT}=obj.getelem(iT).getreference.getfilename;
            end
            referencenames=unique(referencenames);
        end
        
        function codernames=getuniquecodernames(obj)
            nI=obj.getarraysize;
            for iT=1:nI
                codernames{iT}=obj.getelem(iT).getcoderparams.name;
            end
            codernames=unique(codernames);
        end
        
        
        function [ref,stat_m]=makestatistics(obj)
            referencenames=obj.getuniquereferencenames;
            codernames=obj.getuniquecodernames;
            
            nR=length(referencenames);
            nC=length(codernames);
            nI=obj.getarraysize;
            for iR=1:nR
                currname=referencenames{iR};
                ref(iR).name=currname;
                
                for iC=1:nC,
                    counter=1;
                    for iT=1:nI,
                        if strcmp(obj.getelem(iT).getreference.getfilename,currname)
                            icoder=find(strcmp(obj.getelem(iT).getcoderparams.name,...
                                codernames));
                            if icoder==iC
                                ref(iR).coder(icoder).name=obj.getelem(iT).getcoderparams.name;
                                nmetrics=size(obj.getelem(iT).getmetrics,2);
                                currmetrics=obj.getelem(iT).getmetrics;
                                for iN=1:nmetrics,
                                    ref(iR).coder(icoder).data(iN,counter)=currmetrics(iN).value;
                                    stat_m(iR,icoder,iN,counter)=currmetrics(iN).value;
                                    ref(iR).coder(icoder).metricname{iN}=currmetrics(iN).name;
                                end
                             
                                ref(iR).coder(icoder).data(nmetrics+1,counter)=obj.getelem(iT).getscore;
                                ref(iR).coder(icoder).data(nmetrics+2,counter)=obj.getelem(iT).getfilesize;
                                
                                stat_m(iR,icoder,nmetrics+1,counter)=obj.getelem(iT).getscore;
                                stat_m(iR,icoder,nmetrics+2,counter)=obj.getelem(iT).getfilesize;
                                
                                counter=counter+1;
                            end
                        end
                    end
                end
                
            end
            
            
        end
        % private
    end
    
    
end
