<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String temp = "";
	if(request.getAttribute("userId") != null){
		temp = request.getAttribute("userId").toString();
	}else{
		response.sendRedirect("login.page");
	}
%>
<%   String strClientIp = request.getHeader("X-Forwarded-For");  
        if (strClientIp == null || strClientIp.length() == 0 || "unknown".equalsIgnoreCase(strClientIp)) {  
            strClientIp = request.getHeader("Proxy-Client-IP");  
        }  
        if (strClientIp == null || strClientIp.length() == 0 || "unknown".equalsIgnoreCase(strClientIp)) {  
            strClientIp = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (strClientIp == null || strClientIp.length() == 0 || "unknown".equalsIgnoreCase(strClientIp)) {  
            strClientIp = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (strClientIp == null || strClientIp.length() == 0 || "unknown".equalsIgnoreCase(strClientIp)) {  
            strClientIp = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (strClientIp == null || strClientIp.length() == 0 || "unknown".equalsIgnoreCase(strClientIp)) {  
            strClientIp = request.getRemoteAddr();  
        }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>다운로드 페이지</title>
		<link rel="stylesheet" type="text/css" href="./resource/css/style.css" />
	</head>
	<body>
		<div class="text-center">
			<img src="./resource/image/download_logo_new.png" style="width:300px; padding-top: 60px;"/><br/>
		</div>
		<hr class="middle-line"/>
		<div class="text-center download-box" onclick="download('android')">
			<img src="./resource/image/android.png"/>
			<span>Android Download</span>
		</div>
		
		<div class="text-center download-box" onclick="download('ios')">
			<img src="./resource/image/apple.png" class="os-icon"/>
			<span>IOS Download</span>
		</div>
		
	</body>
	<script type="text/javascript">
		// 1: 사내개발, 2: 개발서버, 3: 운영서버
		var gServerKind = 3; 	
		var gClientIp = "<%= strClientIp %>";
		var loginId = "<%= temp%>";
		var localId = localStorage.getItem("userId");
		if(loginId != localId && loginId != "root"){
			//window.location.href =  "http://localhost:8080/mplus-deploy/login.page";
			if( gServerKind == 1)
				window.location.href =  "http://192.168.1.18:48080/mplus-deploy/login.page";
			else if( gServerKind == 2)
				window.location.href =  "https://nps.kuh.ac.kr/mplus-deploy-dev/login.page";
			else if( gServerKind == 3)
				window.location.href =  "https://nps.kuh.ac.kr/mplus-deploy/login.page";
			else
				window.location.href =  "http://192.168.1.18:48080/mplus-deploy/login.page";
		}
		
		function download(type){
			if(type=='android'){
				processDownloadLog( "android", "1.0");
				//location.href="https://smart-sev1.iseverance.com/mplus_deploy/file/mplus-android.apk";
				//location.href="http://192.168.1.18:48080/mplus-deploy/file/mplus-android.apk";
				if( gServerKind == 1)
					window.location.href =  "https://dev1.lemonhc.com/store/mcare-plus/kunkuk/android/kunkuk-local-debug.apk";
				else if( gServerKind == 2)
					window.location.href =  "https://dev1.lemonhc.com/store/mcare-plus/kunkuk/android/kunkuk-dev-debug.apk";
				else if( gServerKind == 3)
					//window.location.href =  "https://dev1.lemonhc.com/store/mcare-plus/kunkuk/android/kunkuk-product-debug.apk";
					window.location.href =  "https://nps.kuh.ac.kr/mplus-deploy/file/kunkuk-product-debug.apk";
				else
					window.location.href =  "http://192.168.1.18:48080/mplus-deploy/file/mplus-android.apk";
			}else{
				processDownloadLog( "iOS", "2.0");
				//location.href="itms-services://?action=download-manifest&url=https://smart-sev1.iseverance.com/mplus_deploy/file/manifest.plist";
				//location.href="http://192.168.1.18:48080/mplus-deploy/file/mplus-ios.ipa";
				if( gServerKind == 1)
					window.location.href =  "itms-services://?action=download-manifest&url=https://dev1.lemonhc.com/store/mcare-plus/kunkuk/ios/lemon/manifest.plist";
				else if( gServerKind == 2)
					window.location.href =  "itms-services://?action=download-manifest&url=https://dev1.lemonhc.com/store/mcare-plus/kunkuk/ios/dev/manifest.plist";
				else if( gServerKind == 3)
					//window.location.href =  "itms-services://?action=download-manifest&url=https://dev1.lemonhc.com/store/mcare-plus/kunkuk/ios/store/manifest.plist";
					window.location.href =  "itms-services://?action=download-manifest&url=https://nps.kuh.ac.kr/mplus-deploy/file/manifest.plist";
				else
					window.location.href =  "http://192.168.1.18:48080/mplus-deploy/file/mplus-ios.ipa";				
			}
		}
		
		// 로그인 이력을 기록함
		var processDownloadLog = function( appKind, appVersion){
			var param = {
					userId: loginId,
					userNm: loginId,
					appKind: appKind,
					appVersion: appVersion,
					userIp: gClientIp,
					currentDt: strYYYYMMDDHHMMSS( new Date())
					};
			inputDownloadLog( param, function( result){ console.log( result);});
		};
		
		//=================================== 공통 라이브러리를 복사함 { =================================
		//다운로드 이력 로그 전송
		var tmpJsonURL = "";
		if( gServerKind == 1)
			tmpJsonURL =  "http://192.168.1.18:48080/mcare-plus/mobile/deployment/inputDownloadLog.json";
		else if( gServerKind == 2)
			tmpJsonURL =  "https://nps.kuh.ac.kr/mcare-plus-dev/mobile/deployment/inputDownloadLog.json";
		else if( gServerKind == 3)
			tmpJsonURL =  "https://nps.kuh.ac.kr/mcare-plus/mobile/deployment/inputDownloadLog.json";
		else
			tmpJsonURL =  "http://192.168.1.18:48080/mcare-plus/mobile/deployment/inputDownloadLog.json";
		var inputDownloadLog = function(param, callback){			
			$.ajax ({
				  url : tmpJsonURL,
				  type : "POST",
				  contentType: "application/json", 
				  data: JSON.stringify(param),
				  success: function(response){
					  if(response.result!=undefined){
						  callback(response.result);
					  }else{
						  console.log(response.msg);
					  }
				  },
				  error : function(XHR, textStatus, errorThrown) { 
						//self.loading("hide");
						if (XHR.status == 0) {
							alert("You are offline!! Please Check Your Network.");
						} else if (XHR.status == 404) {
							alert("Requested URL not found.");
						} else if (XHR.status == 500) {
							alert("Internel Server Error.");
						} else if (textStatus == "parsererror") {
							alert("Error. Parsing JSON Request failed.");
						} else if (textStatus == "timeout") {
							alert("Request Time out.");
						} else {
							alert("Unknow Error. " + XHR.responseText);
						}
						console.log("code:" + XHR.status + "\n" + "message:" + XHR.responseText + "\n" + "error:" + errorThrown);
				  },
		          beforeSend: function(){  
		        	  //self.loading("show");
		          },
		          complete:function(){  
		        	  //self.loading("hide");
		          }
			});
		};
		//=================================== 공통 라이브러리를 복사함 } =================================
			
		// 날짜를 YYYYMMDDHHMMSS 로 출력 처리
		var pad2 = function(n) { return n < 10 ? '0' + n : n };
		var strYYYYMMDDHHMMSS =  function( date){
			//var date = new Date();
			//console.log(date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2( date.getDate()) + pad2( date.getHours()) + pad2( date.getMinutes()) + pad2( date.getSeconds()));
			return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2( date.getDate()) + pad2( date.getHours()) + pad2( date.getMinutes()) + pad2( date.getSeconds());
		};
	</script>
</html>