<?php include 'header.php'; 

// IP Kontrolü - Türkiye'den mi?
$is_turkey_ip = isTurkeyIP();

// user_call kontrolü - 0 ise eşleşebilir
$can_match = ($myUser['user_call'] == 0);

// Cinsiyet kontrolü - sadece erkekler eşleşebilir
$is_male = ($myUser['user_cinsiyet'] != 'kadin');

// Yasaklı kullanıcı kontrolü
$is_allowed = ($user_id != 663793);

// Arama ekranında gösterilecek rastgele 6 kadın bot kullanıcı
$randomWomen = [];
try {
    $womenQuery = $db->query("SELECT user_picture FROM kullanicilar WHERE user_bot = 1 AND user_picture != 'assets/img/kadin-avatar.png' ORDER BY RAND() LIMIT 6");
    if ($womenQuery) {
        $randomWomen = $womenQuery->fetchAll(PDO::FETCH_COLUMN);
    }
} catch (Exception $e) {
    // Hata olursa boş array kalır
}
?>

<!-- WebView video oynatma için meta taglar -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<style type="text/css">
/* No Scroll */
body.no-scroll {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
}

/* Full Height Fix */
#appCapsule.full-height {
    padding: 56px 0 70px 0 !important;
    margin: 0 !important;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    overflow: hidden !important;
    height: calc(100vh - 126px) !important;
    max-height: calc(100vh - 126px) !important;
}

/* Arka Plan */
.match-page {
    position: relative;
    height: 100%;
    background: transparent;
    overflow: hidden;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Floating Hearts Animasyonu */
.floating-hearts {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    pointer-events: none;
    z-index: 1;
}

.heart {
    position: absolute;
    font-size: 20px;
    animation: floatUp 6s ease-in infinite;
    opacity: 0;
}

.heart:nth-child(1) { left: 10%; animation-delay: 0s; font-size: 18px; }
.heart:nth-child(2) { left: 20%; animation-delay: 1s; font-size: 24px; }
.heart:nth-child(3) { left: 35%; animation-delay: 2s; font-size: 16px; }
.heart:nth-child(4) { left: 50%; animation-delay: 0.5s; font-size: 22px; }
.heart:nth-child(5) { left: 65%; animation-delay: 3s; font-size: 20px; }
.heart:nth-child(6) { left: 80%; animation-delay: 1.5s; font-size: 26px; }
.heart:nth-child(7) { left: 90%; animation-delay: 2.5s; font-size: 18px; }
.heart:nth-child(8) { left: 5%; animation-delay: 4s; font-size: 22px; }
.heart:nth-child(9) { left: 45%; animation-delay: 3.5s; font-size: 16px; }
.heart:nth-child(10) { left: 75%; animation-delay: 4.5s; font-size: 24px; }

@keyframes floatUp {
    0% {
        bottom: -50px;
        opacity: 0;
        transform: translateX(0) rotate(0deg);
    }
    10% {
        opacity: 0.6;
    }
    90% {
        opacity: 0.6;
    }
    100% {
        bottom: 100%;
        opacity: 0;
        transform: translateX(100px) rotate(45deg);
    }
}

/* Glowing Orbs */
.glow-orb {
    position: absolute;
    border-radius: 50%;
    filter: blur(60px);
    opacity: 0.3;
    animation: pulse-glow 4s ease-in-out infinite;
}

.glow-orb-1 {
    width: 200px;
    height: 200px;
    background: #ff4081;
    top: 10%;
    left: -50px;
}

.glow-orb-2 {
    width: 150px;
    height: 150px;
    background: #7c4dff;
    bottom: 20%;
    right: -30px;
    animation-delay: 2s;
}

.glow-orb-3 {
    width: 100px;
    height: 100px;
    background: #00bcd4;
    top: 50%;
    left: 50%;
    animation-delay: 1s;
}

@keyframes pulse-glow {
    0%, 100% { transform: scale(1); opacity: 0.3; }
    50% { transform: scale(1.2); opacity: 0.5; }
}

/* Ana Container */
.match-container {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 500px;
    padding: 15px;
    text-align: center;
}

/* Başlangıç Ekranı */
.match-intro {
    animation: fadeInUp 0.8s ease;
    width: 100%;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Glassmorphism Card */
.intro-card {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 30px;
    padding: 35px 25px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
    width: 100%;
}

.intro-icon {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #ff4081 0%, #f50057 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 25px;
    box-shadow: 0 10px 40px rgba(255, 64, 129, 0.5);
    animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
    0%, 100% { transform: scale(1); }
    25% { transform: scale(1.1); }
    50% { transform: scale(1); }
    75% { transform: scale(1.1); }
}

.intro-icon i {
    font-size: 45px;
    color: white;
}

.match-intro h2 {
    font-size: 20px;
    color: white;
    margin-bottom: 12px;
    font-weight: 700;
    text-shadow: 0 2px 10px rgba(0,0,0,0.3);
    white-space: nowrap;
}

.match-intro p {
    color: rgba(255, 255, 255, 0.7);
    margin-bottom: 30px;
    font-size: 15px;
    line-height: 1.5;
}

/* Ana Buton */
.match-btn {
    background: linear-gradient(135deg, #ff4081 0%, #f50057 50%, #ff4081 100%);
    background-size: 200% 200%;
    color: white;
    border: none;
    padding: 18px 45px;
    font-size: 18px;
    font-weight: 600;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 10px 30px rgba(255, 64, 129, 0.5);
    transition: all 0.3s ease;
    animation: shimmer 3s ease infinite;
    position: relative;
    overflow: hidden;
}

@keyframes shimmer {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.match-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    animation: shine 2s infinite;
}

@keyframes shine {
    0% { left: -100%; }
    50%, 100% { left: 100%; }
}

.match-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 15px 40px rgba(255, 64, 129, 0.6);
}

.match-btn:active {
    transform: translateY(0);
}

/* Arama Ekranı */
.searching-screen {
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.5s ease;
    width: 100%;
}

.search-card {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 30px;
    padding: 35px 25px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
    width: 100%;
}

.searching-text {
    font-size: 20px;
    color: white;
    margin-bottom: 10px;
    font-weight: 600;
}

.searching-subtext {
    font-size: 14px;
    color: rgba(255,255,255,0.6);
    margin-bottom: 30px;
}

.orbit-container {
    position: relative;
    width: 280px;
    height: 280px;
    margin: 0 auto;
}

.center-icon {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80px;
    height: 80px;
    background: linear-gradient(135deg, #ff4081 0%, #f50057 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 40px rgba(255, 64, 129, 0.6), 0 0 80px rgba(255, 64, 129, 0.3);
    z-index: 10;
    animation: pulse-center 1.5s ease-in-out infinite;
}

@keyframes pulse-center {
    0%, 100% { box-shadow: 0 0 40px rgba(255, 64, 129, 0.6), 0 0 80px rgba(255, 64, 129, 0.3); }
    50% { box-shadow: 0 0 60px rgba(255, 64, 129, 0.8), 0 0 100px rgba(255, 64, 129, 0.5); }
}

.center-icon i {
    font-size: 36px;
    color: white;
}

/* Radar Effect */
.radar {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 200px;
    height: 200px;
    border: 2px solid rgba(255, 64, 129, 0.3);
    border-radius: 50%;
    animation: radar-pulse 2s ease-out infinite;
}

.radar:nth-child(2) { animation-delay: 0.5s; }
.radar:nth-child(3) { animation-delay: 1s; }

@keyframes radar-pulse {
    0% {
        width: 80px;
        height: 80px;
        opacity: 1;
    }
    100% {
        width: 280px;
        height: 280px;
        opacity: 0;
    }
}

.orbit {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    animation: rotate 10s linear infinite;
}

.orbit-item {
    position: absolute;
    width: 70px;
    height: 70px;
    border-radius: 50%;
    overflow: hidden;
    border: 3px solid #ff4081;
    box-shadow: 0 6px 20px rgba(0,0,0,0.4);
    background: #222;
}

.orbit-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    animation: counterRotate 10s linear infinite;
}

/* 6 resim, 60° aralıklarla, yarıçap 105px */
.orbit-item:nth-child(1) { 
    top: calc(50% - 105px - 35px); 
    left: calc(50% - 35px); 
}
.orbit-item:nth-child(2) { 
    top: calc(50% - 52px - 35px); 
    left: calc(50% + 91px - 35px); 
}
.orbit-item:nth-child(3) { 
    top: calc(50% + 52px - 35px); 
    left: calc(50% + 91px - 35px); 
}
.orbit-item:nth-child(4) { 
    top: calc(50% + 105px - 35px); 
    left: calc(50% - 35px); 
}
.orbit-item:nth-child(5) { 
    top: calc(50% + 52px - 35px); 
    left: calc(50% - 91px - 35px); 
}
.orbit-item:nth-child(6) { 
    top: calc(50% - 52px - 35px); 
    left: calc(50% - 91px - 35px); 
}

@keyframes rotate {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

@keyframes counterRotate {
    from { transform: rotate(0deg); }
    to { transform: rotate(-360deg); }
}

/* Eşleşme Bulundu */
.match-found {
    display: none;
    animation: scaleIn 0.5s ease;
    width: 100%;
}

.found-card {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 30px;
    padding: 35px 25px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
    width: 100%;
}

.match-found .success-icon {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #00c853 0%, #00e676 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    box-shadow: 0 10px 40px rgba(0, 200, 83, 0.5);
    animation: bounce 0.6s ease;
}

@keyframes bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

.success-icon i {
    font-size: 50px;
    color: white;
}

.match-found h2 {
    font-size: 26px;
    color: white;
    margin-bottom: 10px;
    font-weight: 700;
}

.match-found p {
    color: rgba(255,255,255,0.7);
    font-size: 15px;
}

@keyframes scaleIn {
    from { transform: scale(0.5); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Eşleşme Bulunamadı */
.match-not-found {
    display: none;
    animation: scaleIn 0.5s ease;
    width: 100%;
}

.notfound-card {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 30px;
    padding: 35px 25px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
    width: 100%;
}

.notfound-icon {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    box-shadow: 0 10px 40px rgba(255, 152, 0, 0.5);
}

.notfound-icon i {
    font-size: 45px;
    color: white;
}

.match-not-found h2 {
    font-size: 24px;
    color: white;
    margin-bottom: 12px;
    font-weight: 700;
}

.match-not-found p {
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 25px;
}

.retry-btn {
    background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
    color: white;
    border: none;
    padding: 14px 35px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 8px 25px rgba(255, 152, 0, 0.4);
    transition: all 0.3s ease;
}

.retry-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 30px rgba(255, 152, 0, 0.5);
}

/* Video Görüşme Ekranı */
.video-call-screen {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: #000;
    z-index: 99999;
    flex-direction: column;
}

.video-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.video-container video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Üst Bilgi Çubuğu */
.call-top-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 22px 20px 20px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(to bottom, rgba(0,0,0,0.7), transparent);
    z-index: 10;
}

.live-badge {
    background: linear-gradient(135deg, #00c853 0%, #00e676 100%);
    color: white;
    padding: 3px 12px;
    border-radius: 15px;
    font-size: 11px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-width: 65px;
    box-shadow: 0 2px 10px rgba(0, 200, 83, 0.4);
}

.live-badge::before {
    content: '';
    width: 5px;
    height: 5px;
    background: white;
    border-radius: 50%;
    animation: liveDot 1s infinite;
}

@keyframes liveDot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.call-timer {
    background: rgba(255,255,255,0.2);
    color: white;
    padding: 3px 12px;
    border-radius: 15px;
    font-size: 11px;
    font-weight: 600;
    font-family: monospace;
    min-width: 65px;
    text-align: center;
}

/* Alt Kontrol Çubuğu */
.call-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 40px 20px;
    padding-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 18px;
    background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
}

.control-btn {
    width: 55px;
    height: 55px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 20px;
}

.control-btn:hover {
    transform: scale(1.1);
}

.control-btn.mic,
.control-btn.speaker,
.control-btn.camera {
    background: rgba(255,255,255,0.2);
    color: white;
}

.control-btn.end-call {
    background: #f44336;
    color: white;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-4px); }
    40% { transform: translateX(4px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
}

.control-btn.shake {
    animation: shake 0.4s ease;
}

/* VIP Uyarı Modal */
.vip-overlay {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.9);
    z-index: 100;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;
    text-align: center;
}

.vip-overlay.show {
    display: flex;
    animation: fadeIn 0.5s ease;
}

.vip-icon {
    font-size: 70px;
    margin-bottom: 30px;
    line-height: 1;
}

.vip-title {
    font-size: 26px;
    color: white;
    font-weight: bold;
    margin-bottom: 12px;
    margin-top: 0;
}

.vip-message {
    font-size: 16px;
    color: #aaa;
    margin-bottom: 35px;
    line-height: 1.6;
}

.vip-btn {
    background: linear-gradient(135deg, #ff4081 0%, #f50057 100%);
    color: white;
    border: none;
    padding: 16px 45px;
    font-size: 17px;
    font-weight: bold;
    border-radius: 30px;
    cursor: pointer;
    margin-bottom: 15px;
    box-shadow: 0 8px 25px rgba(255, 64, 129, 0.4);
    transition: all 0.3s ease;
}

.vip-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 12px 35px rgba(255, 64, 129, 0.5);
}

.vip-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: scale(1);
}

.vip-btn:disabled:hover {
    transform: scale(1);
    box-shadow: 0 8px 25px rgba(255, 64, 129, 0.4);
}

.vip-close {
    background: transparent;
    color: #666;
    border: 1px solid #666;
    padding: 6px 35px;
    font-size: 14px;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.vip-close:hover {
    color: white;
    border-color: white;
}
</style>

<div id="appCapsule" class="full-height">
    <div class="match-page">
        <!-- Floating Hearts -->
        <div class="floating-hearts">
            <div class="heart">💕</div>
            <div class="heart">💖</div>
            <div class="heart">💗</div>
            <div class="heart">💓</div>
            <div class="heart">💕</div>
            <div class="heart">💖</div>
            <div class="heart">💗</div>
            <div class="heart">💓</div>
            <div class="heart">💕</div>
            <div class="heart">💖</div>
        </div>

        <!-- Glowing Orbs -->
        <div class="glow-orb glow-orb-1"></div>
        <div class="glow-orb glow-orb-2"></div>
        <div class="glow-orb glow-orb-3"></div>

        <div class="match-container">
            
            <!-- Başlangıç Ekranı -->
            <div class="match-intro" id="matchIntro">
                <div class="intro-card">
                    <div class="intro-icon">
                        <i class="fa fa-heart"></i>
                    </div>
                    <h2>Yakınındaki <?php echo ($myUser['user_cinsiyet'] == 'kadin') ? 'Erkeklerle' : 'Bayanlarla'; ?> Eşleş</h2>
                    <p>Şu an aktif yüzlerce kullanıcı<br>seninle tanışmayı bekliyor!</p>
                    <button class="match-btn" onclick="startSearching()">
                        Eşleşmek için dokun
                    </button>
                </div>
            </div>

            <!-- Arama Ekranı -->
            <div class="searching-screen" id="searchingScreen">
                <div class="search-card">
                    <div class="searching-text">Eşleşme Aranıyor</div>
                    <div class="searching-subtext">Yakınındaki kullanıcılar taranıyor...</div>
                    <div class="orbit-container">
                        <!-- Radar Effect -->
                        <div class="radar"></div>
                        <div class="radar"></div>
                        <div class="radar"></div>
                        
                        <div class="center-icon">
                            <i class="fa fa-heart"></i>
                        </div>
                        <div class="orbit">
                            <?php foreach ($randomWomen as $womanPic): ?>
                            <div class="orbit-item"><img src="<?=$womanPic?>" alt=""></div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Eşleşme Bulundu -->
            <div class="match-found" id="matchFound">
                <div class="found-card">
                    <div class="success-icon">
                        <i class="fa fa-check"></i>
                    </div>
                    <h2>Eşleşme Bulundu!</h2>
                    <p>Görüntülü görüşme başlatılıyor...</p>
                </div>
            </div>

            <!-- Eşleşme Bulunamadı -->
            <div class="match-not-found" id="matchNotFound">
                <div class="notfound-card">
                    <div class="notfound-icon">
                        <i class="fa fa-map-marker"></i>
                    </div>
                    <h2>Eşleşme Bulunamadı</h2>
                    <p>Bulunduğunuz bölgede aktif kullanıcı bulunamadı. Lütfen daha sonra tekrar deneyin.</p>
                    <button class="retry-btn" onclick="resetToStart()">
                        <i class="fa fa-refresh"></i> Tekrar Dene
                    </button>
                </div>
            </div>

        </div>
    </div>
</div>

<!-- Video Görüşme Ekranı -->
<div class="video-call-screen" id="videoCallScreen">
    <div class="video-container">
        <video id="callVideo" playsinline webkit-playsinline muted autoplay x-webkit-airplay="allow"></video>
        
        <!-- Üst Bar -->
        <div class="call-top-bar">
            <div class="live-badge">Canlı</div>
            <div class="call-timer" id="callTimer">00:00</div>
        </div>

        <!-- VIP Uyarı Overlay -->
        <div class="vip-overlay" id="vipOverlay">
            <div class="vip-icon">📞</div>
            <div class="vip-title">Görüşme Duraklatıldı</div>
            <div class="vip-message">
                Görüntülü sohbete devam etmek için<br>
                Arama Paketi satın almalısınız.
            </div>
            <button class="vip-btn" id="vipBuyBtn" onclick="handleVipPurchase()">
                <i class="fa fa-phone"></i> Arama Paketi Satın Al
            </button>
            <button class="vip-close" onclick="endCall()">Daha Sonra</button>
        </div>

        <!-- Alt Kontroller -->
        <div class="call-controls">
            <button class="control-btn mic" id="micBtn" onclick="toggleMic()">
                <i class="fa fa-microphone"></i>
            </button>
            <button class="control-btn speaker" id="speakerBtn" onclick="toggleSpeaker()">
                <i class="fa fa-volume-up"></i>
            </button>
            <button class="control-btn camera" id="cameraBtn" onclick="toggleCamera()">
                <i class="fa fa-video-camera"></i>
            </button>
            <button class="control-btn end-call" onclick="shakeEndCall()">
                <i class="fa fa-phone"></i>
            </button>
        </div>
    </div>
</div>

<script type="text/javascript">
let timerInterval = null;
let seconds = 0;
const isTurkeyIP = <?php echo $is_turkey_ip ? 'true' : 'false'; ?>;
const canMatch = <?php echo $can_match ? 'true' : 'false'; ?>;
const isMale = <?php echo $is_male ? 'true' : 'false'; ?>;
const isAllowed = <?php echo $is_allowed ? 'true' : 'false'; ?>;

// Sayfa yüklendiğinde scroll'u engelle
document.body.classList.add('no-scroll');

function startSearching() {
    document.getElementById('matchIntro').style.display = 'none';
    
    const searchingScreen = document.getElementById('searchingScreen');
    searchingScreen.style.display = 'flex';
    
    // Eşleşme olacak mı kontrol et ve süreyi ayarla
    const willMatch = isTurkeyIP && canMatch && isMale && isAllowed;
    const searchTime = willMatch 
        ? Math.floor(Math.random() * 2000) + 4000  // Başarılı: 4-6 saniye
        : Math.floor(Math.random() * 6000) + 9000; // Başarısız: 9-15 saniye
    
    setTimeout(() => {
        searchingScreen.style.display = 'none';
        
        // Türk IP'si değilse VEYA user_call != 0 ise VEYA kadın kullanıcıysa VEYA yasaklı kullanıcıysa eşleşme bulunamadı göster
        if (!isTurkeyIP || !canMatch || !isMale || !isAllowed) {
            document.getElementById('matchNotFound').style.display = 'block';
            return;
        }
        
        const matchFound = document.getElementById('matchFound');
        matchFound.style.display = 'block';
        
        // Videoyu hemen arka planda yüklemeye başla
        preloadVideo();
        
        setTimeout(() => {
            startVideoCall();
        }, 2000);
        
    }, searchTime);
}

function resetToStart() {
    document.getElementById('matchNotFound').style.display = 'none';
    document.getElementById('matchIntro').style.display = 'block';
}

let preloadedVideoNum = 0;

function preloadVideo() {
    // Rastgele video seç ve yükle
    preloadedVideoNum = Math.floor(Math.random() * 86) + 1;
    const videoPath = 'assets/img/videos/video' + preloadedVideoNum + '.mp4';
    
    const video = document.getElementById('callVideo');
    video.src = videoPath;
    video.muted = true;
    video.setAttribute('muted', '');
    video.setAttribute('playsinline', '');
    video.setAttribute('webkit-playsinline', '');
    
    // Video yüklenmeye başlasın ama oynatma
    video.load();
}

function startVideoCall() {
    const header = document.querySelector('.appHeader');
    const tabbar = document.querySelector('.appBottomMenu');
    const appCapsule = document.getElementById('appCapsule');
    
    if (header) header.style.display = 'none';
    if (tabbar) tabbar.style.display = 'none';
    if (appCapsule) appCapsule.style.display = 'none';
    
    const videoScreen = document.getElementById('videoCallScreen');
    videoScreen.style.display = 'flex';
    
    const video = document.getElementById('callVideo');
    
    // Video zaten preload edildi, sadece oynat
    var playPromise = video.play();
    
    if (playPromise !== undefined) {
        playPromise.then(function() {
            console.log('Video başarıyla oynatılıyor');
        }).catch(function(error) {
            console.log('Video oynatma hatası, tekrar deneniyor...', error);
            setTimeout(function() {
                video.play().catch(function(e) {
                    console.log('İkinci deneme hatası:', e);
                });
            }, 100);
        });
    }
    
    seconds = 0;
    updateTimer();
    timerInterval = setInterval(() => {
        seconds++;
        updateTimer();
    }, 1000);
    
    video.onended = function() {
        if (timerInterval) {
            clearInterval(timerInterval);
        }
        document.getElementById('vipOverlay').classList.add('show');
    };
}

function updateTimer() {
    const mins = Math.floor(seconds / 60).toString().padStart(2, '0');
    const secs = (seconds % 60).toString().padStart(2, '0');
    document.getElementById('callTimer').textContent = mins + ':' + secs;
}

function handleVipPurchase() {
    const btn = document.getElementById('vipBuyBtn');
    
    // Zaten disabled ise işlem yapma
    if (btn.disabled) {
        return;
    }
    
    // Butonu deaktif et
    btn.disabled = true;
    btn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Lütfen Bekleyin...';
    buyJeton2('aramapaket');
    // 0.5 saniye sonra buyJeton2 çağır ve ardından endCall
    setTimeout(function() {
        endCall();
    }, 2000);
}

function endCall() {
    if (timerInterval) {
        clearInterval(timerInterval);
    }
    
    const video = document.getElementById('callVideo');
    video.pause();
    video.src = '';
    
    document.getElementById('vipOverlay').classList.remove('show');
    document.getElementById('videoCallScreen').style.display = 'none';
    
    const header = document.querySelector('.appHeader');
    const tabbar = document.querySelector('.appBottomMenu');
    const appCapsule = document.getElementById('appCapsule');
    
    if (header) header.style.display = '';
    if (tabbar) tabbar.style.display = '';
    if (appCapsule) appCapsule.style.display = '';
    
    // VIP butonunu resetle
    const vipBtn = document.getElementById('vipBuyBtn');
    if (vipBtn) {
        vipBtn.disabled = false;
        vipBtn.innerHTML = '<i class="fa fa-phone"></i> Arama Paketi Satın Al';
    }
    
    document.getElementById('matchIntro').style.display = 'block';
    document.getElementById('searchingScreen').style.display = 'none';
    document.getElementById('matchFound').style.display = 'none';
}

function shakeButton(btn) {
    btn.classList.add('shake');
    setTimeout(() => {
        btn.classList.remove('shake');
    }, 400);
}

function toggleMic() {
    shakeButton(document.getElementById('micBtn'));
}

function toggleSpeaker() {
    shakeButton(document.getElementById('speakerBtn'));
}

function toggleCamera() {
    shakeButton(document.getElementById('cameraBtn'));
}

function shakeEndCall() {
    // Sayacı durdur
    if (timerInterval) {
        clearInterval(timerInterval);
    }
    
    // Videoyu duraklat
    const video = document.getElementById('callVideo');
    if (video) {
        video.pause();
    }
    
    // VIP uyarısını göster
    document.getElementById('vipOverlay').classList.add('show');
}
</script>

<?php include 'footer.php'; ?>
