const STATE_WAIT_FINGER_DOWN = 1;
const STATE_WAIT_FINGER_LEAVE = 2;
const STATE_FINGER_DOWN = 3;
const STATE_FINGER_LEAVE = 4;
const STATE_EXTRACT_TEMPLATE = 5;
const STATE_DOWNLOAD_TEMPLATE = 6;
const STATE_EXTRACT_TEMPLATE_FAIL = 100;
const STATE_EXTRACT_TEMPLATE_DIRTY = 101;
const STATE_EXTRACT_TEMPLATE_POINTS_FEW = 102;
const STATE_EXTRACT_TEMPLATE_MERGE_FAIL = 103;


const ErrorReceiveDataErr         = 1;
const ErrorEnrollTimeout          = 2;
const ErrorDeviceNotFound         = 3;
const ErrorEmptyFail              = 4;


var mafp = {
	isConnected: false,
    ws: null,
    enrollCount: 6,
    enrollTimeout: 30 * 1000,
    callback: null,
    isWorking: false,
	init(conf) {
        if (conf && conf.enrollCount) {
            this.enrollCount = conf.enrollCount;
        }
        if (conf && conf.enrollTimeout) {
            this.enrollTimeout = conf.enrollTimeout;
        }
		return new Promise((resolve,reject) => {
			var that = this;
			if (this.isConnected) {
				resolve();
				return;
            }

			var timeout = setTimeout(() => {
				if (!that.isConnected) {
                    reject("Connection timeout");
                }
			}, 2000)
            var port = 9897;
            var address = 'ws://localhost:' + port + '/';
            that.ws = new WebSocket(address);
            that.ws.addEventListener('open', function() {
                that.isConnected = true;
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = 0;
                }
                resolve()
            });
            that.ws.addEventListener('close', function() {
                console.log('Connection lost');
                that.isConnected = false;
                reject("Connection lost");
            });
            that.ws.addEventListener('message', function(e) {
                let resp = JSON.parse(e.data);
                if (that.callback) {
                    that.callback(resp);
                    if(resp.err || resp.data) {
                        that.isWorking = false;
                        that.callback = null;
                    }
                }
            });
		});
    },
    _sendMessage(msg) {
        this.ws.send(JSON.stringify(msg));
    },
    startEnroll(callback=((status, data)=>{})) {
        this.callback = callback;
        this.isWorking = true;
        this.init().then(() => {
            this._sendMessage({
                cmd: "enrollStart",
                config: {
                    enrollCount: this.enrollCount,
                    enrollTimeout: this.enrollTimeout
                }
            });
        }).catch(err => {
            this.callback = null;
            this.isWorking = false;
            callback({
                err: err,
                state: 0,
                step:0
            })
        })

    },
    cancelEnroll() {
        this.callback = null;
        this.isWorking = false;
        this._sendMessage({
            cmd: "enrollCancel"
        });
    }

};