setTimeout(getWallet(),500);

$(document).on('click','.refreshBalances',function () {
		getWallet();
});

$(document).on('click','.refreshBalance',function () {
		var gameSLug = $(this).attr('initial-balance');

		var wallet = getWallet();

		if (gameSLug != undefined) {
			if (wallet) {
			setTimeout(getGameBalance(gameSLug),500);
			}
		}
});

$(document).find(".walletCalibrate").rotate({
	bind:
	{
		click: function(){
			$(this).rotate({ angle:0,animateTo:360,easing: $.easing.easeInOutExpo })

			if($(this).attr('disabled') != 'disabled'){
				walletCalibrate();
			}

			$(this).attr('disabled','1');

		}
	}
});

function getWallet() {
	
	var result;

	$.ajax({
		headers :{
			'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
		},
		url:location.origin+'/user/wallet/getting',
		method:'get',
		async: false,
		beforeSend: function () {
			$(document).find('.initBeforeMount[for=wallet]').prop('hidden',false);
			$(document).find('.initAfterMount[for=wallet]').prop('hidden',true);
		},
		success:function (response) {
			$('.loadwallet').html('');
			
			$(document).find(".walletCalibrate").removeAttr('disabled');

			if(response.success == true){
                var gameWallets = '';
                var total ='';
				if(Object.keys(response.data.data.game_wallets).length > 0) {
					$.each(response.data.data.game_wallets,function (key,value) {
            var balanceValue = value.balance;
            var mainWallet = response.data.data.main_wallet;

            if(value.game_slug == "ubo"){
                balanceValue = parseFloat(balanceValue) + parseFloat(mainWallet);
								$(document).find('#sport-balance').val(value.balance);
			}
			if(value.game_slug == "mickeymouse"){
								$(document).find('#mickeymouse-balance').val(value.balance);
            }
            balanceValue = balanceValue;
					gameWallets += '<div class="wallet-detail d-flex justify-content-between">\
						                    <div>'+ value.game_name +'</div>\
											<div>'+ toIdrNoDecimal(balanceValue) +'</div>\
									</div>';

				    $(document).find('.singleWallet[initial-balance='+value.game_slug+']').text(toIdrNoDecimal(balanceValue));
					});

					$(document).find('.gameWallet').html(gameWallets);
				}else{
					$(document).find('.gameWallet').html(
									'<div class="wallet-detail d-flex justify-content-between">\
										<div>No game wallet</div>\
										<div>-</div>\
									</div>'
    	            );
                }
                    
                total +='<div style="color:white; font-size:13px;font-style:italic;padding-top:10px;" class="wallet-detail d-flex justify-content-between">\
                            <div style="color:rgb(211, 227, 75);">JUMLAH</div>\
                            <div>'+ toIdrNoDecimal(response.data.data.total_wallet) +'</div>\
                        </div>';

                $(document).find('.totalWallet').html("  "+toIdrNoDecimal(response.data.data.total_wallet));
				$(document).find('.totalWallets').html(total);

				// wallet for ubo game
					$(document).find('.ubo-wallet').val(response.data.data.main_wallet);

			}else if(response.errors){
				$.each(response.errors,function (key,value) {
					defaultAlert(value,'error');
				});
			}
			setTimeout(function () {
				$(document).find('.initBeforeMount[for=wallet]').prop('hidden', true);
				$(document).find('.initAfterMount[for=wallet]').prop('hidden',false);
			},1000);
			
			result = true;

        },
        error: function(e){
			console.log("Internal error contact customer service");
			result = false;
        }
	})

	return result;
}

function walletCalibrate() {
	$.ajax({
		headers :{
			'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
		},
		url:location.origin+'/user/wallet/walletcalibrate',
		method:'post',
		beforeSend: function () {
			$(document).find('.initBeforeMount[for=wallet]').prop('hidden',false);
			$(document).find('.initAfterMount[for=wallet]').prop('hidden',true);
		},
		success:function (response) {
			getWallet()
		},
		error: function(e){
				console.log("Internal error calibrate contact customer service");
		}
	})
}

function getGameBalance(gameSlug) {
  $.ajax({
    headers: {
      'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
    },
    url:location.origin+"/game/gamebalance",
    method:'post',
		data:{
			gameSlug:gameSlug,
		},
    beforeSend:function () {
      $(document).find('.initBeforeMount[for=initSingleBalance]').prop('hidden', false);
      $(document).find('.initAfterMount[for=initSingleBalance]').prop('hidden', true);
    },
    success:function (response) {
			if(response.success == true){

				$(document).find('.initAfterMount[balance=game]').text(toIdrNoDecimal(response.data));

			}else if(response.errors){
				$.each(response.errors,function (key,value) {
					defaultAlert(value,'error');
				});
			}
    }
  });
}

// global calibrate and get amount and submit withdraw_value
window.golbalcalibrate = function(password) {
	
	$.ajax({
		headers :{
			'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
		},
		url:location.origin+'/user/wallet/calibratewithgetwallet',
		method:'post',
		beforeSend: function () {
			$(document).find('.initBeforeMount[for=wallet]').prop('hidden',false);
			$(document).find('.initAfterMount[for=wallet]').prop('hidden',true);
		},
		success:function (response) {

			if (response.success) {
				var amount = response.data.data.main_wallet;

				window.golbalwithdraw(amount,password)
			}else {
				console.log(response);
			}
		},
		error: function(e){
				console.log("Internal error calibrate contact customer service");
		}
	})
}

