package com.cr5w.scan;

import android.app.Instrumentation;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class CR5WScanControl {

	private boolean isRun = true;
	private boolean isOpen = true;
	
	public boolean getIsOpen() {
		return this.isOpen;
	}
	
	public void setIsOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}

	public void init(final Context context) {
		new Thread() {
			public void run() {
				try {
					while (true) {
						if (isRun && 1 == ScanHelper.getScanModel(context)) {
							Instrumentation inst = new Instrumentation();
							Log.v("cr5w-SetScanHeader", "scan: send key begin");
							inst.sendKeyDownUpSync(220);
							inst.sendKeyDownUpSync(221);
							Log.v("cr5w-SetScanHeader", "scan: send key end");
						}
						sleep(500);
					}
				} catch (Exception e) {
				}
			}

		}.start();
	}

	public void start(Context context) {
		if (!this.isOpen)
			return;
		Log.v("cr5w-SetScanHeader", "scan: start begin");
		ScanHelper.setScanSwitchLeft(context, true);
		ScanHelper.setScanSwitchRight(context, true);
		Log.v("cr5w-SetScanHeader", "scan: start end");
		if (!isRun) {
			isRun = true;
		}
	}

	public void stop(Context context) {
		Log.v("cr5w-SetScanHeader", "scan: stop begin");
		ScanHelper.setScanSwitchLeft(context, false);
		ScanHelper.setScanSwitchRight(context, false);
		Log.v("cr5w-SetScanHeader", "scan: stop end");
		isRun = false;
	}

	public void restart(Context context) {
		stop(context);
		start(context);
	}

	public void clear() {
		isRun = false;
	}
}
