<%
local xray_version = require "luci.model.cbi.bypass.api".get_xray_version()
-%>

<script type="text/javascript">
	//<![CDATA[
	var xrayInfo;
	var tokenStr = '<%=token%>';
	var manuallyUpdateText = '<%:Check update%>';
	var noUpdateText = '<%:It is the latest version%>';
	var updateSuccessText = '<%:Update successful%>';
	var clickToUpdateText = '<%:Click to update%>';
	var inProgressText = '<%:Updating...%>';
	var unexpectedErrorText = '<%:Unexpected error%>';
	var updateInProgressNotice = '<%:Updating, are you sure to close?%>';
	var downloadingText = '<%:Downloading...%>';
	var decompressioningText = '<%:Unpacking...%>';
	var movingText = '<%:Moving...%>';

	window.onload = function() {
		var xrayCheckBtn = document.getElementById('_xray-check_btn');
		var xrayDetailElm = document.getElementById('_xray-check_btn-detail');
	};

	function addPageNotice_xray() {
		window.onbeforeunload = function(e) {
			e.returnValue = updateInProgressNotice;
			return updateInProgressNotice;
		};
	}

	function removePageNotice_xray() {
		window.onbeforeunload = undefined;
	}

	function onUpdateSuccess_xray(btn) {
		alert(updateSuccessText);

		if(btn) {
			btn.value = updateSuccessText;
			btn.placeholder = updateSuccessText;
			btn.disabled = true;
		}

		window.setTimeout(function() {
			window.location.reload();
		}, 1000);
	}

	function onRequestError_xray(btn, errorMessage) {
		btn.disabled = false;
		btn.value = manuallyUpdateText;

		if(errorMessage) {
			alert(errorMessage);
		}
	}

	function onBtnClick_xray(btn) {
		if(xrayInfo === undefined) {
			checkUpdate_xray(btn);
		} else {
			doUpdate_xray(btn);
		}
	}

	function checkUpdate_xray(btn) {
		btn.disabled = true;
		btn.value = inProgressText;

		addPageNotice_xray();

		var ckeckDetailElm = document.getElementById(btn.id + '-detail');

		XHR.get('<%=url([[admin]], [[services]], [[bypass]], [[xray_check]])%>', {
			token: tokenStr,
			arch: ''
		}, function(x,json) {
			removePageNotice_xray();

			if(json.code) {
				xrayInfo = undefined;
				onRequestError_xray(btn, json.error);
			} else {
				if(json.update) {
					xrayInfo = json;
					btn.disabled = false;
					btn.value = clickToUpdateText;
					btn.placeholder = clickToUpdateText;
					
					if(ckeckDetailElm) {
						var urlNode = '';
						if(json.version) {
							urlNode = '<em style="color:red;">最新版本号：' + json.version + '</em>';
							if(json.url && json.url.html) {
								urlNode = '<a href="' + json.url.html + '" target="_blank">' + urlNode + '</a>';
							}
						}
					ckeckDetailElm.innerHTML = urlNode;
					}
				} else {
					btn.disabled = true;
					btn.value = noUpdateText;
				}
			}
		},300);
	}

	function doUpdate_xray(btn) {
		btn.disabled = true;
		btn.value = downloadingText;

		addPageNotice_xray();

		var xrayUpdateUrl = '<%=url([[admin]], [[services]], [[bypass]], [[xray_update]])%>';
		// Download file
		XHR.get(xrayUpdateUrl, {
			token: tokenStr,
			url: xrayInfo ? xrayInfo.url.download : ''
		}, function(x,json) {
			if(json.code) {
				removePageNotice_xray();
				onRequestError_xray(btn, json.error);
			} else {
				btn.value = decompressioningText;

				// Extract file
				XHR.get(xrayUpdateUrl, {
					token: tokenStr,
					task: 'extract',
					file: json.file,
					subfix: xrayInfo ? xrayInfo.type : ''
				}, function(x,json) {
					if(json.code) {
						removePageNotice_xray();
						onRequestError_xray(btn, json.error);
					} else {
						btn.value = movingText;

						// Move file to target dir
						XHR.get(xrayUpdateUrl, {
							token: tokenStr,
							task: 'move',
							file: json.file
						}, function(x,json) {
							removePageNotice_xray();
							if(json.code) {
								onRequestError_xray(btn, json.error);
							} else {
								onUpdateSuccess_xray(btn);
							}
						},300)
					}
				},300)
			}
		},300)
	}
	//]]>
</script>

<div class="cbi-value">
	<label class="cbi-value-title">Xray
		<%:Version%>
	</label>
	<div class="cbi-value-field">
		<div class="cbi-value-description">
			<span>【 <%=xray_version%> 】</span>
			<input class="cbi-button cbi-input-apply" type="button" id="_xray-check_btn" onclick="onBtnClick_xray(this);" value="<%:Check update%>" />
			<span id="_xray-check_btn-detail"></span>
		</div>
	</div>
</div>