package performa.orm;

import java.util.Collection;
import oneit.logging.LoggingArea;
import oneit.objstore.StorageException;
import oneit.objstore.rdbms.filters.EqualsFilter;
import oneit.objstore.rdbms.filters.IsNotNullFilter;
import oneit.utils.CollectionUtils;
import oneit.utils.filter.CollectionFilter;
import oneit.utils.filter.Filter;
import oneit.utils.parsers.FieldException;
import performa.orm.types.CriteriaType;
import performa.utils.AnalysisEngine;


public class JobApplication extends BaseJobApplication
{
    private static final long   serialVersionUID    = 0L;
    public  static LoggingArea  LOG                 = LoggingArea.createLoggingArea("JobApplication");
    
    // This constructor should not be called
    public JobApplication ()
    {
        // Do not add any code to this, always put it in initialiseNewObject
    }

    
    public static JobApplication createNewApplication(Candidate candidate, Job job) throws StorageException, FieldException
    {
        JobApplication  jobApplication  =   createJobApplication(job.getTransaction());
        
        jobApplication.setCandidate(candidate);
        jobApplication.setJob(job);
        
        jobApplication.initACAnswers();
//        jobApplication.initCCAnswers();
        
        return jobApplication;
    }
    
    
    private void initACAnswers()
    {
        if(isIncludeAssessmentCriteria())
        {
            for(AssessmentCriteria assessmentCriteria : getJob().getAssessmentCriteriasSet())
            {
                AssessmentCriteriaAnswer    answer  =   AssessmentCriteriaAnswer.createAssessmentCriteriaAnswer(getTransaction());

                addToAssessmentCriteriaAnswers(answer);
                assessmentCriteria.addToAnswers(answer);
            }
        }
    }
    
    
    public Boolean initCCAnswers()
    {
        Boolean redirect    = Boolean.FALSE;
        
        if(this.getCultureCriteriaAnswersCount()==0)
        {
            for(CultureCriteria cultureCriteria : getJob().getCultureCriteriasSet())
            {
                redirect                        = Boolean.TRUE;
                CultureCriteriaAnswer   answer  = CultureCriteriaAnswer.createCultureCriteriaAnswer(getTransaction());

                addToCultureCriteriaAnswers(answer);
                cultureCriteria.addToAnswers(answer);
            }
        }
        
        return redirect;
    }
    
    
    public Answer getPAAnswerOrCreate(Question question, int index) throws FieldException
    {
        Filter  filter  = Answer.SearchByAll().andQuestion(new EqualsFilter<>(question));
        Answer  answer  = CollectionFilter.getFirstMatch(getProfileAssessmentAnswersSet(), filter);
        
        if(answer==null)
        {
            answer  = Answer.createAnswer(getTransaction());
            
            answer.setJobApplication(this);
            answer.setQuestion(question);
            
            if(question.getRightQuestion()!=null)
            {
                Answer  rightAnswer = Answer.createAnswer(getTransaction());
            
                rightAnswer.setJobApplication(this);
                rightAnswer.setQuestion(question);
            }
        }
        
        return answer;
    }
    
    
    public Collection<AssessmentCriteriaAnswer> getACAnswersByType(CriteriaType criteriaType)
    {
        Filter  filter  =   AssessmentCriteriaAnswer.SearchByCriteriaType().byCriteriaType(criteriaType);

        return  CollectionFilter.filter(getAssessmentCriteriaAnswersSet(), filter);
    }

    
    public Boolean selectionCompleted()
    {
        int all =   this.getAssessmentCriteriaAnswersCount();
        
        if(all>0)
        {
            Filter      filter      =   AssessmentCriteriaAnswer.SearchByAll().andAnswer(new IsNotNullFilter<>());
            Collection  selected    =   this.pipelineJobApplication().toAssessmentCriteriaAnswers(filter).toAnswer().vals();

            if(selected!=null && CollectionUtils.equals(selected.size(),all))
            {
                return Boolean.TRUE;
            }
        }
        
        return Boolean.FALSE;
    }
       
    
    public Boolean cultureCompleted()
    {
        int all =   this.getCultureCriteriaAnswersCount();
        
        if(all>0)
        {
            Filter      filter      =   CultureCriteriaAnswer.SearchByAll().andSelectedQuestion(new IsNotNullFilter<>());
            Collection  selected    =   this.pipelineJobApplication().toCultureCriteriaAnswers(filter).toSelectedQuestion().vals();

            if(selected!=null && CollectionUtils.equals(selected.size(),all))
            {
                return Boolean.TRUE;
            }
        }
        
        return Boolean.FALSE;
    }
    
    
    public Boolean assessmentCompleted()
    {
        int all =   this.getJob().getAllProfileAssessmentQuestions().size();
        
        if(all>0)
        {
            Filter      filter      =   Answer.SearchByAll().andAnswerNo(new IsNotNullFilter<>());
            Collection  selected    =   this.pipelineJobApplication().toProfileAssessmentAnswers(filter).toAnswerNo().vals();

            if(selected!=null && CollectionUtils.equals(selected.size(),all))
            {
                return Boolean.TRUE;
            }
        }
        
        return Boolean.FALSE;
    }
    
    
    public Boolean isIncludeAssessmentCriteria()
    {
       return   this.getJob().getIncludeAssessmentCriteria();
    }
    
//    TODO: Fix these hard coded values
    public int getOverallSuitability()
    {
        return 85;
    }
    
    
    public int getJobMatchPercentage()
    {
        return 85;
    }
    
    
    public int getCulturePercentage()
    {
        return 90;
    }
    
    
    public int getCriteriaPercentage ()
    {
        return 95;
    }
}