#include "common.h"
#include <string>
#include "stdarg.h"
int string_split(std::string str, std::string pattern,std::vector<std::string> &out)
{
	std::string::size_type pos;
	int cnt = 0;
	str += pattern;
	int size = str.size();

	for (int i = 0; i<size; i++)
	{
		pos = str.find(pattern, i);
		if (pos<size)
		{
			cnt++;
			std::string s = str.substr(i, pos - i);
			out.push_back(s);
			i = pos + pattern.size() - 1;
		}
	}
	return cnt;
}

std::string format(const char *pszFmt, ...)
{
    std::string str;
    va_list args;
    va_start(args, pszFmt);
    {
        int nLength = _vscprintf(pszFmt, args);
        nLength += 1;
        std::vector<char> chars(nLength);
        _vsnprintf(chars.data(), nLength, pszFmt, args);
        str.assign(chars.data());
    }
    va_end(args);
    return str;
}

void DrawSelectRects(cv::Mat input,DisplayLabelConf &t_DisplayLabelConf,int Cnt)
{
	if (t_DisplayLabelConf.Flag[Cnt] & DisplayLabel_Type_Bit) {
		if (t_DisplayLabelConf.leftButtonDownFlag) {
			cv::rectangle(
				input,
				cv::Point(
					t_DisplayLabelConf.originalPoint.x * input.cols,
					t_DisplayLabelConf.originalPoint.y * input.rows),
				cv::Point(
					t_DisplayLabelConf.processPoint.x * input.cols,
					t_DisplayLabelConf.processPoint.y * input.rows),
				cv::Scalar(0, 0, 255),
				4);
		}
		for (int i = 0; i < t_DisplayLabelConf.RectVet[Cnt].size(); i++) {
			cv::rectangle(
				input,
				cv::Point(
					t_DisplayLabelConf.RectVet[Cnt][i].TL.x * input.cols,
					t_DisplayLabelConf.RectVet[Cnt][i].TL.y * input.rows),
				cv::Point(
					t_DisplayLabelConf.RectVet[Cnt][i].BR.x * input.cols,
					t_DisplayLabelConf.RectVet[Cnt][i].BR.y * input.rows),
				cv::Scalar(0, 0, 255),
				4);
		}
	}
}

bool CheckSelectRects(
	cv::Mat input,
	std::vector<std::vector<std::pair<int, cv::Rect> > > &VecRects,
	int VecCnt, 
	DisplayLabelConf &t_DisplayLabelConf, 
	int LabelCnt
)
{
	if (VecCnt < VecRects.size())
	{
		for (int i = 0; i < VecRects[VecCnt].size(); i++)
		{
			for(int j = 0; j < t_DisplayLabelConf.RectVet[LabelCnt].size(); j++)
			{
				cv::Rect LocalRect = VecRects[VecCnt][i].second;
				cv::Rect CheckRect = cv::Rect(
					cv::Point(
					t_DisplayLabelConf.RectVet[LabelCnt][j].TL.x* input.cols,
					t_DisplayLabelConf.RectVet[LabelCnt][j].TL.y* input.rows
					), 
					cv::Point(
					t_DisplayLabelConf.RectVet[LabelCnt][j].BR.x* input.cols,
					t_DisplayLabelConf.RectVet[LabelCnt][j].BR.y* input.rows
					)
				);

				if(LocalRect != (LocalRect & CheckRect))
				{
					return true;
				}
			}
		}
	}
	return false;
}