<link href="bu.css?v=2" rel="stylesheet">
<meta name="robots" content="noindex, nofollow">

<center>
<div class="player-servers-container">

<ul id="player_servers" class="player-servers">
<?php
$name_code_pairs_base64 = $_GET['cache'];
$name_code_pairs_decoded = base64_decode($name_code_pairs_base64);
$server_pairs = explode(" == ", $name_code_pairs_decoded);
$first_server_url = $server_pairs[0];
$i = 1;

foreach ($server_pairs as $code) {
    $parsed_url = parse_url($code);
    $host = str_ireplace('www.', '', $parsed_url['host']);
    $host_name = ucfirst(explode('.', $host)[0]);

    echo '<li data-index="'.$i.'" data-url="'.$code.'" onclick="server('.$i.',\''.$code.'\',this)" class="server'.$i.'">';
    echo '  <span class="loader"></span>';
    echo '  <span class="tooltip">السيرفر لا يعمل</span>';
    echo '  <span class="retry" onclick="retryServer(event, '.$i.', this)">🔄</span>';
    echo '  <i class="fa fa-play-circle"></i>';
    echo '  <span class="server_name">'.$host_name.'</span>';
    echo '</li>';
    $i++;
}
?>
</ul>

</div>
</center>

<div class="embeding">
  <div class="centerSeversLIST">
    <div class="serverContainer">
      <iframe src="<?php echo $first_server_url; ?>" allowfullscreen></iframe>
    </div>
  </div>
</div>

<script>
/* ===================== إعدادات ===================== */
const STORAGE_TTL = 24 * 60 * 60 * 1000; // 24 ساعة
const LOAD_TIMEOUT = 8000;

/* ===================== أدوات ===================== */
function now(){ return Date.now(); }

function setState(i, state){
  localStorage.setItem("server_"+i, state);
  localStorage.setItem("server_"+i+"_time", now());
}

function getState(i){
  return localStorage.getItem("server_"+i);
}

function incFail(i){
  let n = parseInt(localStorage.getItem("server_"+i+"_fails") || "0", 10) + 1;
  localStorage.setItem("server_"+i+"_fails", n);
  return n;
}

function getFails(i){
  return parseInt(localStorage.getItem("server_"+i+"_fails") || "0", 10);
}

function clearIfExpired(i){
  let t = parseInt(localStorage.getItem("server_"+i+"_time") || "0", 10);
  if(t && (now() - t) > STORAGE_TTL){
    ["server_"+i, "server_"+i+"_time", "server_"+i+"_fails"].forEach(k=>localStorage.removeItem(k));
    return true;
  }
  return false;
}

/* ===================== التشغيل ===================== */
function server(i, url, el){

  // منع الضغط على الأحمر
  if(el.classList.contains("error")) return false;

  document.querySelectorAll(".player-servers li").forEach(li=>{
    li.classList.remove("active","loading","success");
  });

  el.classList.add("active","loading");

  var iframe = document.createElement("iframe");
  iframe.src = url;
  iframe.allowFullscreen = true;
  iframe.frameBorder = 0;
  iframe.width = "100%";
  iframe.height = "100%";

  var container = document.querySelector(".serverContainer");
  container.innerHTML = "";
  container.appendChild(iframe);

  iframe.onload = function(){
    el.classList.remove("loading","error");
    el.classList.add("success");
    setState(i, "success");
    localStorage.removeItem("server_"+i+"_fails");
    updateTooltip(el, i);
  };

  setTimeout(function(){
    if(el.classList.contains("loading")){
      el.classList.remove("loading");
      el.classList.add("error");
      setState(i, "error");
      incFail(i);
      updateTooltip(el, i);
    }
  }, LOAD_TIMEOUT);
}

/* إعادة الفحص */
function retryServer(e, i, btn){
  e.stopPropagation();
  let li = btn.closest("li");
  li.classList.remove("error","success");
  server(i, li.dataset.url, li);
}

/* Tooltip */
function updateTooltip(li, i){
  let t = li.querySelector(".tooltip");
  let fails = getFails(i);
  t.textContent = fails ? `السيرفر لا يعمل (محاولات: ${fails})` : `السيرفر لا يعمل`;
}

/* الاسترجاع */
window.addEventListener("DOMContentLoaded", ()=>{
  document.querySelectorAll(".player-servers li").forEach(li=>{
    let i = li.getAttribute("data-index");
    if(clearIfExpired(i)) return;

    let state = getState(i);
    if(state){
      li.classList.add(state);
      updateTooltip(li, i);
    }
  });

  let firstOk = document.querySelector(".player-servers li:not(.error)");
  if(firstOk) firstOk.click();
});
</script>

<style>
@import "https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css";

.player-servers{list-style:none;padding:0;margin-bottom:15px}
.player-servers li{
  display:inline-block;background:#696969;color:#fff;margin:5px;
  border-radius:50px;cursor:pointer;position:relative;overflow:hidden;transition:.3s
}
.player-servers li.error{cursor:not-allowed}

.player-servers .server_name{
  padding:8px 15px;display:block;min-width:140px;text-align:center;font-size:18px
}

.player-servers li i{
  position:absolute;right:0;width:40px;height:100%;display:flex;
  align-items:center;justify-content:center;background:#ab670b;opacity:0;transition:.3s
}
.player-servers li.active i{opacity:1}

/* Loader */
.player-servers li .loader{
  position:absolute;inset:0;display:none;background:rgba(0,0,0,.55)
}
.player-servers li.loading .loader{display:block}
.player-servers li.loading .loader::after{
  content:"";width:22px;height:22px;border:3px solid #1e90ff;
  border-top-color:transparent;border-radius:50%;
  position:absolute;top:50%;left:50%;
  transform:translate(-50%,-50%);animation:spin 1s linear infinite
}

/* حالات */
.player-servers li.success{background:#2ecc71!important}
.player-servers li.error{background:#e74c3c!important}

/* Tooltip */
.player-servers li .tooltip{
  position:absolute;bottom:110%;left:50%;transform:translateX(-50%);
  background:#e74c3c;color:#fff;padding:4px 8px;border-radius:4px;
  font-size:13px;white-space:nowrap;display:none
}
.player-servers li.error:hover .tooltip{display:block}

/* زر إعادة الفحص */
.player-servers li .retry{
  position:absolute;left:8px;top:50%;transform:translateY(-50%);
  display:none;font-size:16px;cursor:pointer
}
.player-servers li.error .retry{display:block}

.embeding iframe{width:100%;height:480px;background:#000;border:4px solid #003968}
@media(max-width:768px){.embeding iframe{height:240px}}

@keyframes spin{to{transform:rotate(360deg) translate(-50%,-50%)}}
</style>

<?php ob_end_flush(); ?>