<%
local api = require "luci.model.cbi.passwall.api.api"
local hysteria_version = api.get_hysteria_version()
-%>

<script type="text/javascript">
	//<![CDATA[
	var hysteriaInfo;
	var tokenStr = '<%=token%>';
	var manuallyUpdateText = '<%:Manually 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 hysteriaCheckBtn = document.getElementById('_hysteria-check_btn');
		var hysteriaDetailElm = document.getElementById('_hysteria-check_btn-detail');
	};

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

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

	function onUpdateSuccess_hysteria(btn) {
		alert(updateSuccessText);

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

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

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

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

	function onBtnClick_hysteria(btn) {
		if(hysteriaInfo === undefined) {
			checkUpdate_hysteria(btn);
		} else {
			doUpdate_hysteria(btn);
		}
	}

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

		addPageNotice_hysteria();

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

		XHR.get('<%=api.url("hysteria_check")%>', {
			token: tokenStr,
			arch: ''
		}, function(x,json) {
			removePageNotice_hysteria();

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

		addPageNotice_hysteria();

		var hysteriaUpdateUrl = '<%=api.url("hysteria_update")%>';
		// Download file
		XHR.get(hysteriaUpdateUrl, {
			token: tokenStr,
			url: hysteriaInfo ? hysteriaInfo.url.download : ''
		}, function(x,json) {
			if(json.code) {
				removePageNotice_hysteria();
				onRequestError_hysteria(btn, json.error);
			} else {
				btn.value = decompressioningText;
					// Move file to target dir
					XHR.get(hysteriaUpdateUrl, {
						token: tokenStr,
						task: 'move',
						file: json.file
					}, function(x,json) {
						removePageNotice_hysteria();
						if(json.code) {
							onRequestError_hysteria(btn, json.error);
						} else {
							onUpdateSuccess_hysteria(btn);
						}
					},300)
				}
			},300)
		}
	//]]>
</script>

<div class="cbi-value">
	<label class="cbi-value-title">Hysteria
		<%:Version%>
	</label>
	<div class="cbi-value-field">
		<div class="cbi-value-description">
			<span>【 <%=hysteria_version ~= "" and hysteria_version or translate("Null") %> 】</span>
			<!--<input class="btn cbi-button cbi-button-apply" type="button" id="_hysteria-check_btn" onclick="onBtnClick_hysteria(this);" value="<%:Manually update%>" />
			<span id="_hysteria-check_btn-detail"></span>-->
		</div>
	</div>
</div>