<?php 

use Stichoza\GoogleTranslate\GoogleTranslate;

class scrapes
{	

	private $Go_Curl;

    public function __construct()
    {
        $this->Go_Curl = new Go_Curl();
    }

	public function scrape_web_info($sources) 
	{
		require_once get_template_directory().'/libs/addons/inc/ssl.php';      

		@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');

		 
		global $opt_themes;
		 
		// === Google Translate Setup ===
        $translator     = null;
        $targetLanguage = null;
        if (!empty($opt_themes['bahasa_on']) && !empty($opt_themes['bahasa'])) {
            $translator     = new GoogleTranslate();
            $targetLanguage = $opt_themes['bahasa']; // e.g. 'id', 'es', 'ru', 'pt', etc.
            $translator->setTarget($targetLanguage);
        }
		
		/* 
		if($opt_themes['bahasa_on']){
		$tr				= new GoogleTranslate(); 
		$bahasa			= $opt_themes['bahasa'];  
		} 
		*/
		 
		$url				= $_POST['wp_url'];
		
		$arr				= array();
		
		$links				= scrape_site($url);	
		$html				= $links; 
		
		// Asumsikan $html sudah berisi konten halaman dari Go_Curl

		// 1. Ekstrak isi <title> (full title)
		$raw_titles = $this->Go_Curl->match('/<title>(.*?)<\/title>/msi', $html, 1);

		if (empty($raw_titles)) {
			// fallback jika regex gagal
			$raw_titles = '';
		}

		// 2. Cleaning agresif tapi aman
		$raw_titles = str_replace(
			['download', 'Download', 'DOWNLOAD', 'Downloads', 'downloads', 
			 'apk', 'Apk', 'APK', 'Apks', 'APKS'],
			'',
			$raw_titles
		);

		$clean_titles = $raw_titles;

		// Hapus prefix "Download " (case insensitive)
		$clean_titles = preg_replace('/^download\s+/i', '', $clean_titles);

		// Hapus suffix " for Android" (case insensitive, termasuk variasi spasi)
		$clean_titles = preg_replace('/\s+for\s+Android$/i', '', $clean_titles);

		// Hapus kata-kata tidak perlu: download, apk, mod, dll.
		
		//$clean_titles = preg_replace('/\b(download|downloads|apk|apks|mod|hack|premium|pro|full|unlimited)\b\s*/i', ' ', $clean_titles);

		// Hapus semua isi dalam kurung beserta kurungnya ((), [], {}, <>)
		$clean_titles = preg_replace('/\s*[\(\[\{<].*?[\)\]\}>]\s*/i', ' ', $clean_titles);

		// Hapus versi angka (1.9.12, v1.0, 2.0.1, dll.)
		$clean_titles = preg_replace('/\b(v?[\d\.\_]+)\b/i', ' ', $clean_titles);

		// Hapus tanda hubung akhir atau garbage lain (misal: " - 5play.org")
		$clean_titles = preg_replace('/\s*[-–—].*$/', '', $clean_titles);

		// Bersihkan spasi berlebih dan trim
		$clean_titles = trim(preg_replace('/\s+/', ' ', $clean_titles));

		// Hasil akhir: $clean_titles siap dipakai untuk search Play Store ID
		 

		$CariJudul 			= $clean_titles; 
		
		$h1_raw2			= str_replace(array('&#8211;'), '', $this->Go_Curl->match('/<h1 itemprop="name" class="title">(.*?)<\/h1>/ms', $html, 1));
		//print_r($CariJudul);
		
        // === Google Play Package ID ===
		$arr['id_ps']		= $this->Go_Curl->match('/<meta itemprop="url" content="(.*?)">/ms', $html, 1);		
		$arr['id_ps']		= $this->Go_Curl->validate_gplay_id($arr['id_ps']);

		$arr['title_id']	= $arr['id_ps'];
		$arr['title_id_2']	= $arr['id_ps'];
 		 
		
        $gp_id 				= !empty($arr['id_ps']) ? $arr['id_ps'] : ($arr['title_id'] ?? $arr['title_id_2'] ?? '');		
		
		if ( !empty($gp_id) ) {			
			$titleId		= $gp_id; 
		} else {			  
			$titleId 		= $this->Go_Curl->get_playstore_id_by_title_google($h1_raw2);
		}
		 
		
		//$titleId 			= $gp_id; 
		
		$arr['GP_ID'] 		= $titleId; 
		
			
		if ( !empty($titleId) ) {
			require_once get_template_directory().'/libs/addons/includes/playstore.php';
		}
		 
		 
		$arr['title']		= str_replace(array('5play', 'Download '), '', $this->Go_Curl->match('/<title>(.*?)\ for .*?<\/title>/ms', $html, 1));
		
		$arr['title_sc']	= str_replace(array('&#8211;'), '', $this->Go_Curl->match('/<h1 itemprop="name" class="title">(.*?)<\/h1>/ms', $html, 1));
		$arr['title_sc']	= preg_replace('/\s+/', ' ', $arr['title_sc']);

        foreach (['title', 'title_sc'] as $key) {
            $arr[$key] = str_replace(['[', ']'], ['(', ')'], $arr[$key]);
            $arr[$key] = str_replace(['(', ')'], ['[', ']'], $arr[$key]);
            $arr[$key] = str_replace(['download', 'for Android'], '', $arr[$key]);
            $arr[$key] = trim(preg_replace('/\s+/', ' ', $arr[$key]));
        }

 
        // === Title Extraction & Cleaning ===
        $raw_title 			= $this->Go_Curl->match('/<title>(.*?)\ for .*?<\/title>/ms', $html, 1);
        $h1_title  			= $this->Go_Curl->match('/<h1 itemprop="name" class="title">(.*?)<\/h1>/ms', $html, 1);
		
        // === Basic App Info ===
        $arr['app_name']   	= trim(str_replace(['APK', 'Apk', 'apk'], '', $h1_title ?? ''));	 
	 
		$arr['poster_sc']	= $this->Go_Curl->match('/<figure class="appicon">.*?<img.*?src="(.*?)".*?itemprop="image">/ms', $html, 1);
		$arr['poster_web']	= $arr['poster_sc'];  
		
		/* 
		echo '<pre>';		
		print_r($arr['poster_sc']);
		echo '</pre>';	
		*/
		
		$arr['version_sc']	= $this->Go_Curl->match('/<span class="spec-cont" itemprop="softwareVersion">(.*?)<\/span>/ms', $html, 1);
		
		$arr['genre_sc']	= $this->Go_Curl->match('/<meta itemprop="applicationCategory" content="(.*?)">/ms', $html, 1);
		
        // === Description Cleaning ===
        $description = $this->Go_Curl->match('/<div class="desc text expand-text">(.*?)<button type="button" class="expand-btn"/ms', $html, 1) ?? '';
        if ($description) {
            $description = preg_replace('/<figure[\s\S]*?<\/figure>/is', '', $description);
            $description = preg_replace('/\s+(href|target|class|id|style)=[\'"][^\'"]*[\'"]/i', '', $description);
            $description = preg_replace('/<!--[\s\S]*?-->/', '', $description);
            $description = str_replace(['<div>', '</div>', '<center>', '</center>', '<span>', '</span>', '&nbsp;'], '', $description);
            $description = str_replace(['<strong>', '</strong>'], ['<b>', '</b>'], $description);
            $description = trim(preg_replace('/\s+/', ' ', $description));
        }
        $arr['article_sc']      = $description;
        $arr['article_content'] = $description;


        // === Summaries ===
        $arr['summary_sc2'] = wp_strip_all_tags(mb_substr($arr['article_content'] ?? '', 0, 160, 'UTF-8'));
        $arr['summary_sc']  = wp_strip_all_tags($this->Go_Curl->match('/<p class="font-semibold text-md">(.*?)<\/p>/ms', $html, 1) ?? '');
 
        // === Mod Features from Title ===
        $arr['mods'] = '';
        if (preg_match('/\[([^\]]*)\]/', $arr['title'] ?? '', $matches)) {
            $mod_text    = str_replace('MOD ', '', trim($matches[1]));
            $arr['mods'] = $mod_text;
        }
 
		 /* 
		echo '<pre>';		
		print_r($arr['mods']);
		echo '</pre>';	
		 */
		 
		
        // === Full Mod Info Block ===
        $full_mod = $this->Go_Curl->match('/<div class="download-box-info tab-panel" data-tab="1"[^>]*>(.*?)<\/article>/ms', $html, 1) ?? '';
        if ($full_mod) {
            $full_mod = preg_replace('/\s+(class|id|style)=[\'"][^\'"]*[\'"]/i', '', $full_mod);
            $full_mod = str_replace(['&nbsp;', '</div>'], '', $full_mod);
            $full_mod = trim(preg_replace('/\s+/', ' ', $full_mod));
            $arr['full_mod'] = $full_mod;
        } else {
            $arr['full_mod'] = '';
        }


		/* 
		echo '<pre>';		
		print_r($arr['full_mod']);
		echo '</pre>';	
		*/
		/*
		download link
		*/
		 
        // === Download Links Page ===
        $arr['canonical']           = $this->Go_Curl->match('/<link rel="canonical" href="(.*?)">/ms', $html, 1);
        $arr['download_links_page'] = $this->Go_Curl->match('/<div class="download-line btn btn-fill">.*?<a.*?href="(.*?)".*?>/ms', $html, 1);

        $arr['downloadlinks_gma']      = [];
        $arr['name_downloadlinks_gma'] = [];
        $arr['size_downloadlinks_gma'] = [];
        $arr['link_apk']               = '';

        if (!empty($arr['download_links_page'])) {
            $download_page_html = scrape_site($arr['download_links_page']);

            if ($download_page_html) {
                $cdn_section = $this->Go_Curl->match('/<section class="page-cdn">(.*?)<div class="faq-list">/ms', $download_page_html, 1);

                if ($cdn_section) {
					/* 
                    $arr['downloadlinks_gma']      = $this->Go_Curl->match_all('/<a href="(.*?)".*?id="btn-download-cdn".*?>/ms', $cdn_section, 1) ?? [];


					$arr['name_downloadlinks_gma'] = $this->Go_Curl->match_all('/<h1 class="title">Download (.*?)<\/h1>/ms', $cdn_section, 1) ?? []; //remove NAME : 5play
					
					*/		
					// === Ambil semua link download CDN ===
					$arr['downloadlinks_gma'] = $this->Go_Curl->match_all(
						'/<a[^>]+href=["\']([^"\']+)["\'][^>]*id=["\']btn-download-cdn["\'][^>]*>/i',
						$cdn_section,
						1
					) ?? [];

					// Jika tidak ada link, stop di sini
					if (empty($arr['downloadlinks_gma'])) {
						return; // atau log error jika perlu
					}
					 
					// === Ambil nama APK dari <h1 class="title">Download Nama File.apk</h1> ===
					$title_raw = $this->Go_Curl->match(
						'/<h1[^>]+class=["\']title["\'][^>]*>\s*Download\s+([^<]+)<\/h1>/i',
						$cdn_section,
						1
					);

					// Fallback 1: jika ada (size) atau spasi ekstra
					if (!$title_raw) {
						$title_raw = $this->Go_Curl->match(
							'/<h1[^>]+class=["\']title["\'][^>]*>\s*Download\s+([^<(]+?)(?:\s*\(|<\/h1>)/i',
							$cdn_section,
							1
						);
					}

					// Fallback 2: lebih longgar jika struktur berubah
					if (!$title_raw) {
						$title_raw = $this->Go_Curl->match(
							'/<h1[^>]+class=["\']title["\'][^>]*>\s*Download\s+(.+?)(?:\s*\(|<\/h1>|$)/is',
							$cdn_section,
							1
						);
					}

					$clean_title = $title_raw ? trim($title_raw) : '';

					// === Bersihkan nama secara menyeluruh ===
					if ($clean_title) {
						// Hilangkan ekstensi file di akhir
						$clean_title = preg_replace('/\.[a-zA-Z0-9]{2,5}$/i', '', $clean_title);

						// Hilangkan semua variasi "5play" (dari akhir atau tengah)
						$clean_title = preg_replace('/\s*[:\-–—]\s*5play(?:\.org|\.ru)?\b/i', '', $clean_title);
						$clean_title = preg_replace('/\s*-t\b/i', '', $clean_title);
						$clean_title = preg_replace('/\s*-t-5play\b/i', '', $clean_title);
						$clean_title = preg_replace('/\s*t-5play\b/i', '', $clean_title);
						$clean_title = preg_replace('/[-_]?5play(?:\.org|\.ru)?\b/i', '', $clean_title);

						// Hilangkan ukuran file di akhir
						$clean_title = preg_replace('/\s*\([\d.,\s]*?(MB|GB|KB)\)$/i', '', $clean_title);

						// Bersihkan spasi berlebih
						$clean_title = trim(preg_replace('/\s+/', ' ', $clean_title));
						$clean_title = str_replace(['-', '_'], ' ', $clean_title);
					}

					// === Fallback super akurat dari nama file di URL ===
					if (empty($clean_title) || strlen($clean_title) < 4 || stripos($clean_title, '5play') !== false || strtoupper($clean_title) === 'APK') {
						if (!empty($arr['downloadlinks_gma'][0])) {
							$url = $arr['downloadlinks_gma'][0];
							$filename = basename(parse_url($url, PHP_URL_PATH));
							$name_base = pathinfo($filename, PATHINFO_FILENAME); // tanpa ekstensi

							// Hilangkan semua variasi 5play dari nama file
							$name_base = preg_replace('/[-_]?5play(?:[-_]?[a-z0-9]+)?$/i', '', $name_base);
							$name_base = preg_replace('/\b5play\b/i', '', $name_base);

							// Format cantik: ganti - dan _ jadi spasi, capitalize
							$name_base = str_replace(['-', '_'], ' ', $name_base);
							$clean_title = ucwords(trim($name_base));

							// Jika masih jelek, fallback default
							if (empty($clean_title) || strlen($clean_title) < 4) {
								$clean_title = 'Download APK';
							}
						} else {
							$clean_title = 'Download APK';
						}
					}

					// === Final touch: pastikan nama terlihat profesional ===
					$clean_title = trim($clean_title);
					if ($clean_title === '' || $clean_title === ' ') {
						$clean_title = 'Download APK';
					}
					
					$clean_title = ucwords(strtolower($clean_title));

					// === Isi nama untuk semua link (multiple mirror) ===
					$arr['name_downloadlinks_gma'] = array_fill(0, count($arr['downloadlinks_gma']), $clean_title);
										
					//print_r($clean_title);

					// === Ambil ukuran file (contoh: "17.05 Mb") ===
					$size_matches = $this->Go_Curl->match_all(
						'/<span class="btn-cont">\s*(\d+(?:\.\d+)?\s*(?:Mb|GB|KB))\s*<\/span>/i',
						$cdn_section,
						1
					);

					// Jika tidak ketemu di btn-cont, coba dari teks "Download (17.05 Mb)"
					if (empty($size_matches)) {
						$size_matches = $this->Go_Curl->match_all(
							'/Download\s*\(([0-9.]+\s*(?:Mb|GB|KB))\)/i',
							$cdn_section,
							1
						);
					}

					// Bersihkan dan isi size (satu per link)
					$arr['size_downloadlinks_gma'] = [];
					foreach ($arr['downloadlinks_gma'] as $i => $link) {
						$size = $size_matches[$i] ?? ($size_matches[0] ?? '');
						$arr['size_downloadlinks_gma'][] = trim($size);
					}
					
                    /* $sizes 			= $this->Go_Curl->match_all('/<span class="btn-cont">(.*?)<\/span>/ms', $cdn_section, 1) ?? [];
					 
					Array ( [0] => Download from Telegram [1] => Download (17.05 Mb) [2] => Back to Description [3] => Wait 10 ) 
					 
                    $arr['size_downloadlinks_gma'] = array_map(fn($s) => trim(str_replace(['Download ', '(', ')'], '', $s)), $sizes); */
					
					//$arr['size_downloadlinks_gma'] = $size_matches;
					 
					
                    if (!empty($arr['downloadlinks_gma'])) {
                        $arr['link_apk'] = end($arr['downloadlinks_gma']); // usually the fastest/preferred link
                    }
                }
            }
        }
		  
		 
		 
		return $arr;
	}
	
	
    /**
     * Multi-layer Cloudflare bypass function (2025-ready)
     * Tries 4 methods in order until success
     */
    private function fetchWithCloudflareBypass(string $url): ?string
    {
        $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0';

        $headers = [
            'Accept'                    => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language'           => 'en-US,en;q=0.9',
            'Accept-Encoding'           => 'gzip, deflate, br',
            'Connection'                => 'keep-alive',
            'Upgrade-Insecure-Requests' => '1',
            'Sec-Fetch-Dest'            => 'document',
            'Sec-Fetch-Mode'            => 'navigate',
            'Sec-Fetch-Site'            => 'none',
        ];

        // 1. wp_remote_get with modern headers
        $response = wp_remote_get($url, [
            'timeout'   => 30,
            'user-agent'=> $user_agent,
            'headers'   => $headers,
            'sslverify' => false,
        ]);

        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
            $body = wp_remote_retrieve_body($response);
            if ($this->isNotCloudflareChallenge($body)) {
                return $body;
            }
        }

        // 2. Your existing scrape_site() function
        if (function_exists('scrape_site')) {
            $html = scrape_site($url);
            if ($html && $this->isNotCloudflareChallenge($html)) {
                return $html;
            }
        }

        // 3. Direct Go_Curl (if method exists)
        if (method_exists($this->Go_Curl, 'geturl')) {
            $html = $this->Go_Curl->geturl($url);
            if ($html && $this->isNotCloudflareChallenge($html)) {
                return $html;
            }
        }

        // 4. FlareSolverr-compatible solver (Byparr, Solvearr, etc.) – SELF-HOSTED
        $solver_url = 'http://127.0.0.1:8191/v1'; // Change if your solver runs elsewhere

        $payload = wp_json_encode([
            'cmd'        => 'request.get',
            'url'        => $url,
            'maxTimeout' => 90000,
        ]);

        $solver_response = wp_remote_post($solver_url, [
            'headers' => ['Content-Type' => 'application/json'],
            'body'    => $payload,
            'timeout' => 120,
        ]);

        if (!is_wp_error($solver_response)) {
            $data = json_decode(wp_remote_retrieve_body($solver_response), true);
            if (isset($data['status']) && $data['status'] === 'ok' && !empty($data['solution']['response'])) {
                return $data['solution']['response'];
            }
        }

        // All attempts failed
        return null;
    }

    /**
     * Detect common Cloudflare challenge pages
     */
    private function isNotCloudflareChallenge(string $html): bool
    {
        $indicators = [
            'Checking your browser',
            'cf-browser-verification',
            'Attention Required!',
            'cloudflare.com',
            'DDoS protection',
            'ray-id',
            'cf-chl-bypass',
            'turnstile',
            'cf-turnstile-response',
        ];

        foreach ($indicators as $indicator) {
            if (stripos($html, $indicator) !== false) {
                return false;
            }
        }
        return true;
    }
	

}