/*******************************************************************
 * Function Name: CalculateRgnPriorProbs
 * Return Type 	: int
 * Created On	: Jul 14, 2013
 * Created By 	: hrushi
 * Comments		: Calculate the Region Prior Probs
 * Arguments	: const string TrainFolderPath
 *******************************************************************/
int PriorProb::CalculateRgnPriorProbs( const fs::path TrainFolderPath, const Args& args)
{

	Labels AllLabels( TrainFolderPath, ALL_LABEL_FILENAME);
	Labels Dragged_Lbl(TrainFolderPath, DRAG_CARRY_LABEL_FILE);
	Labels Worn_Lbl(TrainFolderPath, WORN_CARRY_LABEL_FILE);

	unsigned int NumPersonCntrs(0);
	unsigned int NumWornCntrs(0);
	unsigned int NumDraggedCntrs(0);

	for(tdef_LabelMap::const_iterator itr = AllLabels.GetLabelMapItrBegin(); itr != AllLabels.GetLabelMapItrEnd(); itr++ )
	{
		fs::path RelativeImgPath = itr->first;
		fs::path TrackImgPath 	= AllLabels.GetComplateImgFilePath( RelativeImgPath.string() );

		fs::path MaskImgPath = FileSystem::GetMaskImgPath(args.GetMaskFolderName(), TrackImgPath, args.GetTrackFolderName() );
		ContourMap CntrMap = Detect::GetSegments(TrackImgPath, MaskImgPath, false, args);

		if( CntrMap.GetNumContours() > 1 ) //
		{
			unsigned int NumCtrs = CntrMap.GetNumContours() - 1; // Get Number of contours that are not background
			m_NumCntrs += NumCtrs;

			if( itr->second == NO_CARRIED_OBJECT )
			{
				NumPersonCntrs += NumCtrs;
			}
			else
			{
				// NumPerson Cntrs will be the number of contours -1; -1 for the any of the two labels below
				NumPersonCntrs  += NumCtrs - 1;
				if( Dragged_Lbl.AlreadyPresent(RelativeImgPath) != UN_INITIALIZED_LABEL )
				{
					NumDraggedCntrs++;
				}
				else if( Worn_Lbl.AlreadyPresent(RelativeImgPath) != UN_INITIALIZED_LABEL )
				{
					NumWornCntrs++;
				}

			}
		}
	}

	m_PriorProb_DraggedObj	= (double)NumDraggedCntrs / m_NumCntrs;
	m_PriorProb_WornObj 	= (double)NumWornCntrs 	  / m_NumCntrs;
	m_PriorProb_PersonRgn	= (double)NumPersonCntrs  / m_NumCntrs;

	return EXIT_SUCCESS;
}