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

<script type="text/javascript">
	//<![CDATA[
	var brookInfo;
	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 brookCheckBtn = document.getElementById('_brook-check_btn');
		var brookDetailElm = document.getElementById('_brook-check_btn-detail');
	};

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

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

	function onUpdateSuccess_brook(btn) {
		alert(updateSuccessText);

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

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

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

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

	function onBtnClick_brook(btn) {
		if (brookInfo === undefined) {
			checkUpdate_brook(btn);
		} else {
			doUpdate_brook(btn);
		}
	}

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

		addPageNotice_brook();

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

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

			if (json.code) {
				brookInfo = undefined;
				onRequestError_brook(btn, json.error);
			} else {
				if (json.has_update) {
					brookInfo = json;
					btn.disabled = false;
					btn.value = clickToUpdateText;
					btn.placeholder = clickToUpdateText;

					if (ckeckDetailElm) {
						var urlNode = '';
						if (json.remote_version) {
							urlNode = '<em style="color:red;">' + json.remote_version + '</em>';
							if (json.html_url) {
								urlNode = '<a href="' + json.html_url + '" target="_blank">' + urlNode + '</a>';
							}
						}
						ckeckDetailElm.innerHTML = urlNode;
					}
				} else {
					btn.disabled = true;
					btn.value = noUpdateText;
				}
			}
		}, 300);
	}

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

		addPageNotice_brook();

		var brookUpdateUrl = '<%=api.url("brook_update")%>';
		// Download file
		XHR.get(brookUpdateUrl, {
			token: tokenStr,
			url: brookInfo ? brookInfo.data.browser_download_url : '',
			size: brookInfo ? brookInfo.data.size / 1024 : null
		}, function (x, json) {
			if (json.code) {
				removePageNotice_brook();
				onRequestError_brook(btn, json.error);
			} else {
				btn.value = decompressioningText;
				// Move file to target dir
				XHR.get(brookUpdateUrl, {
					token: tokenStr,
					task: 'move',
					file: json.file
				}, function (x, json) {
					removePageNotice_brook();
					if (json.code) {
						onRequestError_brook(btn, json.error);
					} else {
						onUpdateSuccess_brook(btn);
					}
				}, 300)
			}
		}, 300)
	}
//]]>
</script>

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