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

<script type="text/javascript">
	//<![CDATA[
	var kcptunInfo;
	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 kcptunCheckBtn = document.getElementById('_kcptun-check_btn');
		var kcptunDetailElm = document.getElementById('_kcptun-check_btn-detail');
	};

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

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

	function onUpdateSuccess_kcptun(btn) {
		alert(updateSuccessText);

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

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

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

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

	function onBtnClick_kcptun(btn) {
		if(kcptunInfo === undefined) {
			checkUpdate_kcptun(btn);
		} else {
			doUpdate_kcptun(btn);
		}
	}

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

		addPageNotice_kcptun();

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

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

			if(json.code) {
				kcptunInfo = undefined;
				onRequestError_kcptun(btn, json.error);
			} else {
				if(json.update) {
					kcptunInfo = 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_kcptun(btn) {
		btn.disabled = true;
		btn.value = downloadingText;

		addPageNotice_kcptun();

		var kcptunUpdateUrl = '<%=url([[admin]], [[services]], [[bypass]], [[kcptun_update]])%>';
		// Download file
		XHR.get(kcptunUpdateUrl, {
			token: tokenStr,
			url: kcptunInfo ? kcptunInfo.url.download : ''
		}, function(x,json) {
			if(json.code) {
				removePageNotice_kcptun();
				onRequestError_kcptun(btn, json.error);
			} else {
				btn.value = decompressioningText;

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

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

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