//Put your custom functions and variables in this file

function SeSImageFinder()
{
}

SeSImageFinder.prototype = {
	_interactive: false,
	_threshold : 0.95,
	_scales: [0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0],
	_baseX: 0,
	_baseY: 0,
	
	Matches:[],
	
	_FindImages: function(srcBitmap, templateImage, threshold, scales, outputImage, display)
	{
		var _WshShell = new ActiveXObject("WScript.Shell");
		var _fso = new ActiveXObject("Scripting.FileSystemObject");
		var _exePath = g_helper.ResolvePath("ImageFinder\\RapiseImage.exe");
		if(!_exePath)
		{
			_exePath = g_helper.ResolvePath("..\\Extensions\\ImageFinder\\RapiseImage\\Release\\RapiseImage.exe");
		}
    
		var tempfolder = _fso.GetSpecialFolder(2);
		var resJson = tempfolder+"\\ResJson.json"
		var cmdOutput = tempfolder+"\\RapiseFinder.log"
		g_helper.DeleteFile(resJson);

		
		threshold = threshold||0.9;
		
		var cmd = '"'+_exePath+'" -t '+threshold + ' -i "'+srcBitmap+'" -f "'+templateImage+'" -o "'+resJson+'" ';
		
		if(outputImage)
		{
			cmd += ' -r "'+outputImage+'" ';
			g_helper.DeleteFile(resJson);
		}
		
		if(display)
		{
			cmd += " -dv ";
		}
		
		if(typeof scales == "number")
		{
			scales = [scales];
		}
		
		if(scales&&scales.length)
		{
			for(var si=0;si<scales.length;si++)
			{
				cmd+= " -s "+scales[si];
			}
		}
		
		if(l2) Log2('CMD: '+cmd);
		cmd+= ' > "'+cmdOutput+'"';
		_WshShell.Run(cmd, 0, true);
		this.Matches = [];
		var result=g_helper.FileRead(resJson);
		if (result)
	    {
		    this.Matches = JSON.parse(result).matches;
	    } else {
	    	Tester.Assert('Error processing Image finder tool: '+cmd, false, [new SeSReportText('Command line', cmd), new SeSreportText('Output', g_helper.FileRead(cmdOutput)) ]);
	    	return 0;
	    }
	    
	    if( outputImage )
	    {
			var iw1 = new ActiveXObject("SeSWrappers.Utils.ImageWrapper");
			iw1.Load(outputImage);
			Tester.Message('Image', [new SeSReportImage(iw1, "Matched image")])
	    }
	    return this.Matches.length;
	},
	
	
	
	DoFindInObject: function(/**objectid|SeSObject*/sesObj, /**string*/tplImagePath, /**Boolean*/includeInReport)/**Number*/
	{
		var obj = sesObj;
		if(typeof sesObj=="string")
		{
			obj = SeS(sesObj);
		}
		
		var imageWrapper = obj.GetBitmap();
		var res = this.DoFindInImage(imageWrapper, tplImagePath, includeInReport);
		this._baseX = obj.GetX();
		this._baseY = obj.GetY();
		return res;

	},
	
	DoFindInImage: function(/**ImageWrapper*/imageWrapper, /**string*/tplImagePath, /**Boolean*/includeInReport)/**Number*/
	{
		this._baseX = 0;
		this._baseY = 0;

		var _fso = new ActiveXObject("Scripting.FileSystemObject");
		var tempfolder = _fso.GetSpecialFolder(2);
		
		var srcBitmap = tempfolder+"\\SrcBitmap.png";
		imageWrapper.Save(srcBitmap);
		var templateImage = tplImagePath;
		var reportImage = null;
		if(includeInReport) reportImage = tempfolder + '\\MatchBitmap.png';
		var threshold = this._threshold;
		var scales = this._scales;
		var display = this._interactive;
	
		var res = this._FindImages(srcBitmap, templateImage, threshold, scales, reportImage, display);
		return res;
	},
	
	DoClickOnMatch: function(/**Nubmer*/dx,/**Number*/dy,/**Number*/index, clickType)
	{
		index = index || 0;
		if(this.Matches&&this.Matches.length>index)
		{
			var m = this.Matches[index];
			var mx = this._baseX+m.x;
			var my = this._baseY+m.y;
			if(dx)
			{
				mx+=dx;
			} else {
				mx+=(m.w>>1);
			}
			
			if(dy)
			{
				my+=dy;
			} else {
				my+=(m.h>>1);
			}
			Global.DoMouseMove(mx, my);
			Global.DoClick();
			if(l2) Log2("DoClickOnMatch clicks on x: "+mx+" y: "+my);
			return true;
		}
		Tester.Assert('ImageFinder.DoClickOnMatch failed: Unable to find a match #'+index, false);
		return false;
	},
	
	SetThreshold: function(/**Number*/t)
	{
		this._threshold = t;
	}
}

var ImageFinder = new SeSImageFinder();