#ifndef TEMPLATEMATCHING_H_
#define TEMPLATEMATCHING_H_

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

namespace tempMatch {

	enum class err {
		ok = 0,
		badRange = 1,
		wrongPath = 2,
		cannotOpen = 3
	};
	
	/**
	* @class TemplateMatching
	* @brief Search for template in diferrent scales of image
	* @author Matej Roman (student of BUT)
	*/
	class TemplateMatching {

	public:
		
		TemplateMatching() :
			_scaleInvariant(false),
			_threshold(0.f),
			_speedReduce(5),
			_findRectLeft(0),
			_findRectTop(0),
			_findRectRight(0),
			_findRectBottom(0),
			_reduceTemp(1.f),
			_cannyThreshold1(200.0),
			_cannyThreshold2(200.0),
			_err(err::ok), 
			_temp(cv::Size(0,0), CV_8U) 
		{}

		bool setTemplate(const char* path);

		// Image must be in CV_8U
		bool findTemplateInImage(const char* path);

		// Image must be in CV_8U
		bool findTemplateInImage(cv::Mat image);

		// you can resize cols and row by number
		void resizeTemp(float number);

		// Setters

		void setScaleInvariant() { _scaleInvariant = true; }
		void resetScaleInvariant() { _scaleInvariant = false; }

		//default thr = 0
		bool setThreshold(float thr);

		// you can resize template, default number = 1
		void setResizeTemplate(float number);

		bool setSpeed(int percent);
		void setCannyThreshold(double threshold1, double threshold2);


		// Getters

		int getError() { return (int)_err; }

		cv::Mat getImage() { return _image; }

		void getRectangle(int& left, int& top, int& right, int&bottom);

		

	private:

		bool _scaleInvariant;

		float _threshold;
		int _speedReduce;     // in percents
		int _findRectLeft;
		int _findRectTop;
		int _findRectRight;
		int _findRectBottom;

		float _reduceTemp;

		double _cannyThreshold1;
		double _cannyThreshold2;

		err _err;

		cv::Mat _temp;
		cv::Mat _image;
		cv::Mat _edges;

		void _transformLinear2D(int& x, int& y, int& xReal, int& yReal, float inlox, float inupx, float outlox, float outupx, float inloy, float inupy, float outloy, float outupy);

		bool _loadImage(const char* path, cv::Mat& image);

		int _findTemplate();
	};

}

#endif
