WebSocketManager = {
	url:"",
	init: function(url) {
		WebSocketManager.url = url;
	},
	open: function(callback, slot) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"OPEN","Slot":"'+ slot +'"}');
		};
		wsocket.onmessage = function (evt) {
					var obj = JSON.parse(evt.data);
					wsocket.close();
					callback(obj);
		};

	},
	listReader: function(callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"LIST_READER"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode);
		};

        wsocket.onerror = evt => {
            let hideLoader = () => {
                let pageLoader = document.getElementById('page_loader');
                pageLoader.removeAttribute('class');
            };

            hideLoader();
            wsocket.close();
            Swal.fire({
                icon: 'error',
                title: 'MyKad driver/services tidak dapat dikesan, sila pasang driver',
                html: `
                <div class="mt-2">
                            <button type="button" class="btn-download-driver btn btn-outline-secondary btn-lg mb-2">
                                Download driver <i class="fa fa-download"></i>
                            </button><br>
                            <button type="button" class="btn-yes swal2-confirm swal2-styled">OK</button><br>
                </div>
                `,
                showCancelButton: false,
                showConfirmButton: false,
                willOpen: () => {
                    const yes = document.querySelector('button.btn-yes')
                    const download_driver = document.querySelector('button.btn-download-driver')

                    yes.addEventListener('click', () => {
                        swal.close();
                    })

                    download_driver.addEventListener('click', () => {
                        window.location.href = mykad_reader_driver_endpoint;
                    })
               }
            });

        };
	},
	close: function(callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"CLOSE"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			wsocket.close();
			callback(obj);
		};
	},
	verifyFPTemplate: function(callback, ThumbCode, Bas64FPLeft, Bas64FPRight) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"VERIFY_FP_TEMPLATE","ThumbCode":"'+ ThumbCode +'","Bas64FPLeft":"'+ Bas64FPLeft +'","Bas64FPRight":"'+ Bas64FPRight +'"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			wsocket.close();
			callback(obj.RespCode, obj.RespMessage);
		};

	},
	readMyKad: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"READ_MYKAD"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	readAllText: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"READ_ALL_TEXT"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	readPhoto: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"READ_PHOTO"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	readBio: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"READ_BIO"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	readMyKid: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"READ_MYKID"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioEnrol: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_ENROL"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			//var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(obj);
		};
	},
	getThumbImage: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"GET_MYKAD_THUMB_IMAGE"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	getThumbBin1: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"GET_THUMB_BIN1"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	getThumbBin2: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"GET_THUMB_BIN2"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioEnrolBin1: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_ENROL_BIN1"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			//var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(obj);
		};
	},
	FPBioEnrolBin2: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_ENROL_BIN2"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			//var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(obj);
		};
	},
	FPBioVerify: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioVerifyBin: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY_BIN"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioVerifyLeft: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY_LEFT"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioVerifyBinLeft: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY_BIN_LEFT"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioVerifyRight: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY_RIGHT"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	},
	FPBioVerifyBinRight: function (callback) {
		wsocket = new WebSocket(WebSocketManager.url);
		wsocket.onopen = function () {
			wsocket.send('{"Cmd":"FP_BIO_VERIFY_BIN_RIGHT"}');
		};
		wsocket.onmessage = function (evt) {
			var obj = JSON.parse(evt.data);
			var data = JSON.parse(obj.RespMessage);
			wsocket.close();
			callback(data, obj.RespCode, obj.RespSW12AndApiName);
		};
	}
}
