var androidPhoneNumber;
Storage.prototype.setArray = function(key, obj) {
    return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getArray = function(key) {
    return JSON.parse(this.getItem(key))
}
//null check
function nullcheck(el){
    // console.log(arguments.callee.name,"el의 값:" + el);
    if( el == "" || el == null || el == undefined || ( el != null && typeof el == "object" && !Object.keys(el).length ) ){
        return true;
    } else {
        return false;
    }
}

function toast(msg){
 
	$("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h3>"+msg+"</h3></div>")
	.css({ display: "block", 
		position: "fixed",
		padding: "12px",
        "text-align": "center",
        color: "#FFF",
        width: "270px",
        height: "50px",
        background: "#00000080",
        "line-height": "25px",
        "font-weight": "normal",
        "font-size": "11px",
        "border-radius":"25px",
		left: ($(window).width() - 284)/2,
		top: $(window).height()- 80 })
	.appendTo( ".body_wrap" ).delay( 1800 )
	.fadeOut( 100, function(){
		$(this).remove();
    });
}

//alert
function popAlert(msg){
    var alertEl = "<div class='bg_alert'>";
    alertEl += "<div class='pop_alert'>";
    alertEl += "<p>" + msg.replace("\n", "<br/>") + "</p>";
    alertEl += "<button type='button' class='close_alert'>확인</button>";
    alertEl += "</div>";
    alertEl += "</div>";
    $(".body_wrap").append(alertEl);
    setTimeout(() => {
        $(".bg_alert").addClass("on");
    }, 100);
}

//alert
function closePopAlert(){
    $(".bg_alert").removeClass("on");
    setTimeout(() => {
        $(".bg_alert").remove();
    }, 200);
}
function getRemovePlusEightTwo(el){
    var telephoneNumber = el;
    telephoneNumber = telephoneNumber.replace( '+82', '0' );
    return telephoneNumber;
}
//alert
function popAlertAction(msg,yesCallback){
    var alertEl = "<div class='bg_alert'>";
    alertEl += "<div class='pop_alert'>";
    alertEl += "<p>" + msg + "</p>";
    alertEl += "<button type='button' class='close_alert_action'>확인</button>";
    alertEl += "</div>";
    alertEl += "</div>";
    $(".body_wrap").append(alertEl);
    setTimeout(() => {
        $(".bg_alert").addClass("on");
    }, 100);
    $('.close_alert_action').click(function() {
        //alert("close_alert_action");
        $(".bg_alert").removeClass("on");
        setTimeout(() => {
            $(".bg_alert").remove();
        }, 100);
        yesCallback();
    });
}
//confirm
function popConfirmAction(msg,yesCallback){
    var alertEl = "<div class='bg_alert'>";
    alertEl += "<div class='pop_alert'>";
    alertEl += "<p>" + msg.replace("\n", "<br/>") + "</p>";
    alertEl += "<button type='button' class='ok_confirm_action'>확인</button>";
    alertEl += "<button type='button' class='close_confirm_action'>취소</button>";
    alertEl += "</div>";
    alertEl += "</div>";
    $(".body_wrap").append(alertEl);
    setTimeout(() => {
        $(".bg_alert").addClass("on");
    }, 100);
    $('.ok_confirm_action').click(function() {
        //alert("close_alert_action");
        $(".bg_alert").removeClass("on");
        setTimeout(() => {
            $(".bg_alert").remove();
        }, 100);
        yesCallback();
    });
    $('.close_confirm_action').click(function() {
        //alert("close_alert_action");
        $(".bg_alert").removeClass("on");
        setTimeout(() => {
            $(".bg_alert").remove();
        }, 100);
    });
}
//confirm
function popCallAction(msg,noCallback){
    var alertEl = "<div class='bg_alert'>";
    alertEl += "<div class='pop_alert'>";
    alertEl += "<p>" + msg + "</p>";
    alertEl += "<button type='button' class='ok_confirm_action'>확인</button>";
    alertEl += "<button type='button' class='close_confirm_action'>취소</button>";
    alertEl += "</div>";
    alertEl += "</div>";
    $(".body_wrap").append(alertEl);
    setTimeout(() => {
        $(".bg_alert").addClass("on");
    }, 100);
    $('.ok_confirm_action').click(function() {
        //alert("close_alert_action");
        $(".bg_alert").removeClass("on");
        $(".bg_alert").remove();
        // setTimeout(() => {
        //     $(".bg_alert").remove();
        // }, 100);
    });
    $('.close_confirm_action').click(function() {
        noCallback();
        $(".bg_alert").removeClass("on");
        $(".bg_alert").remove();

        // setTimeout(() => {
        //     $(".bg_alert").remove();
            
        // }, 100);
        
    });
}
$(document).on("click", ".close_alert", function(){
    $(".bg_alert").removeClass("on");
    setTimeout(() => {
        $(".bg_alert").remove();
    }, 100);
});

$(document).ready(function(){
    //alert("ready");
    //var devicePlatform = device.platform;
});

function successCallback(result) {
            
    androidPhoneNumber=getRemovePlusEightTwo(result.phoneNumber);
    window.localStorage.setItem("androidPhoneNumber", androidPhoneNumber);//저장
    //alert("androidPhoneNumber"+androidPhoneNumber);
    console.log(arguments.callee.name,"androidPhoneNumber"+androidPhoneNumber);
    console.log(arguments.callee.name,"successCallback" + JSON.stringify(result));
    checkLocation();
}

function errorCallback(error) {
    console.log(arguments.callee.name,error);
    checkLocation();
}

// Android only: check permission
function hasReadPermission() {
    window.plugins.sim.hasReadPermission(hasSuccessCallback, hasErrorCallback);
}
function hasSuccessCallback(result) { 
    
    console.log(arguments.callee.name,"hasSuccessCallback" + JSON.stringify(result));
    console.log(arguments.callee.name,"hasSuccessCallback" + result);
    if(result){
        window.plugins.sim.getSimInfo(successCallback, errorCallback);
    }else{
        requestReadPermission();
    }
}

function hasErrorCallback(error) {
    console.log(arguments.callee.name,"hasErrorCallback" + error);
    requestReadPermission();
}
// Android only: request permission
function requestReadPermission() {
    window.plugins.sim.requestReadPermission(requestSuccessCallback, requestErrorCallback);
}
function requestSuccessCallback(result) {
    //window.plugins.sim.getSimInfo(successCallback, errorCallback); 
    console.log(arguments.callee.name,"requestSuccessCallback" + JSON.stringify(result));
    if(result == "OK"){
        window.plugins.sim.getSimInfo(successCallback, errorCallback);
    }else{
        requestReadPermission();
    }
}

function requestErrorCallback(error) {
    //checkLocation();
    popAlertAction("전화에 대한 권한을 허용 하셔야 사용 하실 수 있습니다.",function() {
        //hasReadPermission()
        navigator.app.exitApp();
    });
    console.log(arguments.callee.name,"requestErrorCallback" + error);
}

//alert
function popAlert_noti(msg, msg1){

    var alertEl = "<div class='bg_alert'>";
    alertEl += "<div class='pop_alert'>";
    alertEl += "<p>" + msg + "</p>";
    alertEl += "<p>" + msg1 + "초 후에 통화가 진행됩니다. </p>";
    alertEl += "</div>";
    $(".body_wrap").append(alertEl);
    $(".bg_alert").addClass("on");



}


// console log -> native 앱에 저장
var console=(function(oldCons){
    return {
        log: function(text){
            for(let idx=0;idx< arguments.length;idx++){
                text += (arguments[idx]+' ');
            }
            oldCons.log(text);
            try {
                window.plugins.gntel.pushGLog("log", text);
            } catch (ex) {}
        },
        info: function (text) {
            for(let idx=0;idx< arguments.length;idx++){
                text += (arguments[idx]+' ');
            }
            oldCons.info(text);
            try {
                window.plugins.gntel.pushGLog("info", text);
            } catch (ex) {}
        },
        warn: function (text) {
            for(let idx=0;idx< arguments.length;idx++){
                text += (arguments[idx]+' ');
            }
            oldCons.warn(text);
            try {
                window.plugins.gntel.pushGLog("warn", text);
            } catch (ex) {}
        },
        error: function (text) {
            for(let idx=0;idx< arguments.length;idx++){
                text += (arguments[idx]+' ');
            }
            oldCons.error(text);
            try {
                window.plugins.gntel.pushGLog("error", text);
            } catch (ex) {}
        }
    };
}(window.console));
window.console = console;
