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

<script type="text/javascript">
	//<![CDATA[
	var v2rayInfo;
	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 v2rayCheckBtn = document.getElementById('_v2ray-check_btn');
		var v2rayDetailElm = document.getElementById('_v2ray-check_btn-detail');
	};

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

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

	function onUpdateSuccess_v2ray(btn) {
		alert(updateSuccessText);

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

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

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

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

	function onBtnClick_v2ray(btn) {
		if(v2rayInfo === undefined) {
			checkUpdate_v2ray(btn);
		} else {
			doUpdate_v2ray(btn);
		}
	}

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

		addPageNotice_v2ray();

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

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

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

		addPageNotice_v2ray();

		var v2rayUpdateUrl = '<%=api.url("v2ray_update")%>';
		// Download file
		XHR.get(v2rayUpdateUrl, {
			token: tokenStr,
			url: v2rayInfo ? v2rayInfo.url.download : ''
		}, function(x,json) {
			if(json.code) {
				removePageNotice_v2ray();
				onRequestError_v2ray(btn, json.error);
			} else {
				btn.value = decompressioningText;

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

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

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