<?php

use Stichoza\GoogleTranslate\GoogleTranslate;

class scrapes
{	 	
	private $Go_Curl;

    public function __construct()
    {
        $this->Go_Curl = new Go_Curl();
    }
	
	public function scrape_web_info($url) {	
		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;
		 
		
		if($opt_themes['bahasa_on']){

		$tr	= new GoogleTranslate(); 
		$bahasa	= $opt_themes['bahasa'];  

		}
		
		$url	= $_POST['wp_url'];
		
		$arr	= array();
		
		$status	= http_response($url); 
		 
		if($status === 403) {
		$html	= scrapedo($url);	
		} elseif($status === 200) {
		$html	= get_url($url);	
		}
		 
		// 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			= trim(strip_tags($this->Go_Curl->match('/<h1[^>]*class="[^"]*text-2xl[^"]*"[^>]*>(.*?)<\/h1>/msi', $html, 1)));
		//print_r($CariJudul);
		
		
		/*
		id info
		*/		 
		 
		$arr['id_ps']		= $this->Go_Curl->match('/Platform.*?<a.*?href="(.*?)".*?>.*?<\/a>/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'];
 		 
		 
		
		//$arr['GP_ID']		= $arr['title_id'];
		//$arr['GP_ID_2']		= $arr['title_id_2'];
		//$arr['GP_IDX']		= $arr['title_id'];
		//$arr['GP_IDX_2']	= $arr['title_id_2'];
		
		
        $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';
		}
		 
		 
		/*
		data info
		*/
		// === Improved Title Extraction & Cleaning ===
		$title_raw = $this->Go_Curl->match('/<title>(.*?)<\/title>/msi', $html, 1);
		$h1_raw    = trim(strip_tags($this->Go_Curl->match('/<h1[^>]*class="[^"]*text-2xl[^"]*"[^>]*>(.*?)<\/h1>/msi', $html, 1)));

		$clean_title = function($text) {
			if (empty($text)) return '';
			
			// Decode HTML entities (e.g., &#8211; → –)
			$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5);
			
			// Remove unwanted phrases (case-insensitive)
			$unwanted = ['getmodsapk.com', '–', '-', 'free', 'for Android', '+', 'Download', 'MOD APK', 'Mod Apk'];
			$text = str_ireplace($unwanted, '', $text);
			
			// Normalize whitespace
			$text = preg_replace('/\s+/', ' ', $text);
			
			// Remove extra separators around MOD features
			$text = preg_replace('/\s*[-–|•:]\s*/', ' ', $text);
			
			return trim($text);
		};

		$arr['title']     = $clean_title($title_raw);
		$arr['title_sc']  = $clean_title($h1_raw);

		// Fallback if primary title is empty
		if (empty($arr['title'])) {
			$arr['title'] = $arr['title_sc'];
		}

		// Optional: Standardize MOD brackets to [ ]
		$arr['title'] = str_replace(['(', ')'], ['[', ']'], $arr['title']);
		$arr['title_sc'] = str_replace(['(', ')'], ['[', ']'], $arr['title_sc']);

		// Final cleanup: remove stray "download" if it appears alone
		$arr['title'] = trim(str_ireplace(' download ', ' ', $arr['title']));
		$arr['title_sc'] = trim(str_ireplace(' download ', ' ', $arr['title_sc']));

		$arr['poster_post']	= $this->Go_Curl->match('/<div id="post-thumbnail".*?<img src="(.*?)\".*?>/ms', $html, 1);
		$arr['poster_sc']	= 'https://getmodsapk.com'.$arr['poster_post'];	
		
		// === Improved App Name Extraction ===
		$app_name_raw = trim( strip_tags( $this->Go_Curl->match( '/App Name.*?<p[^>]*>(.*?)<\/p>/msi', $html, 1 ) ) );

		$clean_app_name = function( $text ) {
			if ( empty( $text ) ) {
				return '';
			}

			// Decode HTML entities (e.g., &#8211; → –)
			$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );

			// Remove common unwanted junk
			$unwanted = [
				'getmodsapk.com',
				'–', '-', '—', '|',                  // separators
				'free', 'Free',
				'for Android', 'For Android',
				'+', 'Download', 'MOD', 'Mod', 'MOD APK', 'Mod Apk',
				'Premium', 'Pro', 'Unlocked'
			];

			$text = str_ireplace( $unwanted, ' ', $text );

			// Clean up extra whitespace and separators
			$text = preg_replace( '/\s*[-|•:–—]\s*/', ' ', $text );
			$text = preg_replace( '/\s+/', ' ', $text );

			return trim( $text );
		};

		$arr['app_name'] = $clean_app_name( $app_name_raw );

		// Optional fallback: Use title if app_name is still empty
		if ( empty( $arr['app_name'] ) && ! empty( $arr['title'] ) ) {
			// Extract the main app name from title (before version or MOD features)
			if ( preg_match( '/^([^[\v\d]+)/', $arr['title'], $m ) ) {
				$arr['app_name'] = trim( $m[1] );
			}
		}

		$arr['version_sc']	= $this->Go_Curl->match('/<h3.*?>Version.*?<p.*?>(.*?)<\/p>/ms', $html, 1);	
		$arr['version_sc']	= str_replace('v', '', $arr['version_sc']);	
		$arr['size_sc']		= $this->Go_Curl->match('/<h3.*?>Size.*?<p.*?>(.*?)<\/p>/ms', $html, 1);	 
		$arr['genre_sc']	= trim($this->Go_Curl->match('/<h3.*?>Category.*?<a.*?>(.*?)<\/a>/ms', $html, 1));	 
		$arr['developer_sc']	= trim($this->Go_Curl->match('/<h3.*?>Publisher.*?<p.*?>(.*?)<\/p>/ms', $html, 1)); 
		$arr['updated']		= trim($this->Go_Curl->match('/<h3.*?>Last Updated.*?<p.*?>(.*?)<\/p>/ms', $html, 1)); 
 
		$arr['updated_sc'] 	= date("M d, Y", strtotime($arr['updated']));
		
		// === Extract MOD Features from Title ===
		$title_to_search = $arr['title'] ?? $arr['title_sc'] ?? '';

		$arr['carimod'] = preg_match('/\[([^\[\]]*)\]/', $title_to_search, $matches);

		if ($arr['carimod'] && isset($matches[1])) {
			$arr['mods'] = trim($matches[1]);
			
			// Optional cleanup inside the brackets
			$arr['mods'] = html_entity_decode($arr['mods'], ENT_QUOTES | ENT_HTML5);
			$arr['mods'] = preg_replace('/\s+/', ' ', $arr['mods']);           // normalize spaces
			$arr['mods'] = trim($arr['mods'], " ,;|•-–");                      // remove trailing separators
		} else {
			$arr['mods'] = '';  // No MOD features found
		}
		 // === Improved MOD Features Section Extraction & Cleaning ===

		// Extract "Full MOD Title" (usually "MOD Features" or similar h2)
		$arr['full_mod_title'] = trim( strip_tags( $this->Go_Curl->match( 
			'/<div class="flex items-center gap-2">.*?<svg[^>]*>.*?<\/svg>\s*<h2 class="text-xl font-bold[^>]*">(.*?)<\/h2>/msi', 
			$html, 1 
		) ) );

		// Fallback regex if the SVG icon varies or structure slightly changes
		if ( empty( $arr['full_mod_title'] ) ) {
			$arr['full_mod_title'] = trim( strip_tags( $this->Go_Curl->match( 
				'/<h2 class="text-xl font-bold[^>]*">(MOD Features|What\'s New|Mod Info).*?<\/h2>/msi', 
				$html, 1 
			) ) );
		}

		// Extract the full MOD description list/content
		$full_mod_raw = $this->Go_Curl->match( 
			'/<div class="overflow-hidden transition-all duration-300 max-h-0"[^>]*>\s*<div[^>]*>(.*?)<\/div>\s*<\/div>/msi', 
			$html, 1 
		);

		// Fallback: Broader capture if the max-h-0 div is nested differently
		if ( empty( $full_mod_raw ) ) {
			$full_mod_raw = $this->Go_Curl->match( 
				'/<div class="overflow-hidden[^>]*>\s*<div[^>]*>(.*?)<\/div>\s*<\/div>/msi', 
				$html, 1 
			);
		}

		$arr['full_mod'] 		= $this->clean_mod_description( $full_mod_raw );
		
		// === Improved Article Content Extraction & Cleaning ===

		// Extract the main post content
		$article_raw = $this->Go_Curl->match( 
			'/<div class="post-content text-gray-900[^>]*">(.*?)<\/div>/msi', 
			$html, 1 
		);

		// Fallback: If the exact class changes slightly (e.g., additional classes), use a broader match
		if ( empty( $article_raw ) ) {
			$article_raw = $this->Go_Curl->match( 
				'/<div class="post-content[^>]*">(.*?)<\/div>/msi', 
				$html, 1 
			);
		}

		$arr['article_content'] = $this->clean_article_content( $article_raw );
		$arr['article_sc']      = $arr['article_content']; // Keep both if needed elsewhere

		$arr['summary_sc2']		= wp_strip_all_tags(mb_substr( $arr['article_content'], 0, 100, 'utf8' ));
		$arr['summary_sc']		= wp_strip_all_tags($this->Go_Curl->match('/<p class="font-semibold text-md">(.*?)<\/p>/ms', $html, 1)); 
		
		/*
		download link
		*/// === Improved Download Links Extraction (Updated for December 2025 getmodsapk.com structure) ===

		// Extract canonical URL (fallback to provided $url if missing)
		$arr['canonical'] = $this->Go_Curl->match( '/<link rel="canonical" href="(.*?)"/msi', $html, 1 );
		if ( empty( $arr['canonical'] ) ) {
			$arr['canonical'] = $url; // fallback
		}

		$download_page = rtrim( $arr['canonical'], '/' ) . '/download';

		// Fetch download page with proper handling
		$status_code = $this->Go_Curl->http_responsex( $download_page );

		$blocked_statuses = [ 301, 302, 303, 307, 308, 403 ];
		$success_statuses = [ 200, 201 ];

		if ( in_array( $status_code, $blocked_statuses ) ) {
			$info_download = $this->Go_Curl->scrapedox( $download_page );
		} else {
			$info_download = $this->Go_Curl->scrape_sitex( $download_page );
		}

		if ( empty( $info_download ) ) {
			$arr['downloadlinks_gma'] = $arr['apklink'] = [];
			return; // No download page content – skip rest
		}

		// Optional: Verify title (for debugging)
		// $arr['title1'] = $clean_title( $this->Go_Curl->match('/<title>(.*?)<\/title>/msi', $info_download, 1) );

		// Extract the main download options section
		$section = $this->Go_Curl->match( 
			'/<div class="space-y-4 mb-8">(.*?)<h2[^>]*>Installation Note<\/h2>/msi', 
			$info_download, 1 
		);

		if ( empty( $section ) ) {
			// Fallback broader match if "Installation Note" text changes
			$section = $this->Go_Curl->match( '/<div class="space-y-4 mb-8">(.*?)<\/div>/msi', $info_download, 1 );
		}

		if ( empty( $section ) ) {
			$arr['downloadlinks_gma'] = $arr['apklink'] = [];
			return;
		}

		// Extract all variant data
		$arr['downloadlinks_gma']      = $this->Go_Curl->match_all( '/<a href="(.*?)".*?class="download-links[^"]*"/msi', $section, 1 );

		$arr['name_downloadlinks_gma'] = $this->Go_Curl->match_all( '/<span class="text-lg font-medium text-gray-800[^>]*">(.*?)<\/span>/msi', $section, 1 );

		$arr['size_downloadlinks_gma']  = $this->Go_Curl->match_all( '/<a[^>]*class="download-links[^>]*>.*?<svg[^>]*>.*?<\/svg>\s*(.*?)\s*<\/a>/msi', $section, 1 );
		foreach ( $arr['size_downloadlinks_gma'] as &$size ) {
			$size = trim( str_replace( ['Download ', '(', ')'], '', $size ) );
		}
		unset( $size );

		$arr['notes_downloadlinks_gma'] = $this->Go_Curl->match_all( '/<div style="background-color:#f1f1f1;[^>]*">.*?<p[^>]*>(.*?)<\/p>.*?<\/div>/msi', $section, 1 );

		// Prepare final direct APK links array
		$arr['apklink'] = [];

		foreach ( $arr['downloadlinks_gma'] as $intermediate_link ) {
			if ( empty( $intermediate_link ) ) {
				$arr['apklink'][] = '';
				continue;
			}

			// Check status of intermediate link
			$link_status = $this->Go_Curl->http_responsex( $intermediate_link );

			if ( in_array( $link_status, $blocked_statuses ) ) {
				$page_content = $this->Go_Curl->scrapedox( $intermediate_link );
			} else {
				$page_content = $this->Go_Curl->scrape_sitex( $intermediate_link );
			}

			// Extract the final download button (visible or hidden)
			$final_link = $this->Go_Curl->match( 
				'/<a href="(https?:\/\/[^"]+)"[^>]*id="download-button"[^>]*>/msi', 
				$page_content, 1 
			);

			// Fallback: any large "Download" button with direct .apk link
			if ( empty( $final_link ) ) {
				$final_link = $this->Go_Curl->match( '/<a href="(https?:\/\/[^"]*\.apk[^"]*)"/msi', $page_content, 1 );
			}

			// Ultimate fallback: the intermediate link itself (some sites direct download)
			$arr['apklink'][] = $final_link ?: $intermediate_link;
		}

		// Optional: Set a primary link (first one, usually MOD APK)
		$arr['link_apk'] = $arr['apklink'][0] ?? '';

		 // === Improved Translation Block (Safe & Efficient) ===

		if ( ! empty( $opt_themes['bahasa_on'] ) && isset( $tr ) ) {
			// Set source to null for auto-detection (better accuracy), target already set earlier
			$tr->setSource( null );

			$safe_translate = function( $text ) use ( $tr ) {
				if ( empty( $text ) ) {
					return $text;
				}

				try {
					$translated = $tr->translate( $text );

					if ( $translated === null ) {
						return $text; // Fallback to original if translation fails
					}

					// Fix common HTML tag spacing issues caused by translation
					$translated = str_replace( [ '< ', ' <', '< /', '</ ', ' >' ], [ '<', '<', '</', '</', '>' ], $translated );

					return $translated;
				} catch ( Exception $e ) {
					// Log error if needed, but don't break the process
					error_log( 'Translation error: ' . $e->getMessage() );
					return $text; // Return original on error
				}
			};

			if ( ! empty( $arr['mods'] ) ) {
				$arr['mods'] = $safe_translate( $arr['mods'] );
			}

			if ( ! empty( $arr['full_mod'] ) ) {
				$arr['full_mod'] = $safe_translate( $arr['full_mod'] );
			}

			if ( ! empty( $arr['article_content'] ) ) {
				$arr['article_content'] = $safe_translate( $arr['article_content'] );
			}

			if ( ! empty( $arr['summary_sc'] ) ) {
				$arr['summary_sc'] = $safe_translate( $arr['summary_sc'] );
			}

			// Optional: Translate title too (common in multilingual themes)
			if ( ! empty( $arr['title'] ) ) {
				$arr['title'] = $safe_translate( $arr['title'] );
			}
		}
		
		
		if ( empty($arr['app_name'])) {
			$arr['error']			= NO_ID;
			echo $arr['error'];
		} 
		
		
		return $arr;
	}
		 
	private function clean_mod_description( $text )
	{
		if ( empty( $text ) ) {
			return '';
		}

		// Decode entities (&nbsp; → space, etc.)
		$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );
		$text = str_replace( '&nbsp;', ' ', $text );

		// Remove all attributes (href, target, class, id, style, etc.)
		$text = preg_replace( '/\s+(href|target|class|id|style|data-[a-z-]+)=[\'"][^\'"]*[\'"]/', '', $text );

		// Remove unwanted tags but keep <b>, <strong>, <ul>, <li>, <br> for formatting
		$text = strip_tags( $text, '<b><strong><ul><li><br><p>' );

		// Clean up leftover empty tags
		$text = preg_replace( '/<(div|center|span)[^>]*>/i', '', $text );
		$text = preg_replace( '/<\/(div|center|span)>/i', '', $text );

		// Convert <strong> to <b> (simpler for most themes)
		$text = str_replace( ['<strong>', '</strong>'], ['<b>', '</b>'], $text );

		// Normalize whitespace and line breaks
		$text = preg_replace( '/\s+/', ' ', $text );
		$text = preg_replace( '/<\s*br\s*\/?\s*>/i', "<br>", $text );
		$text = trim( $text );

		return $text;
	}
	
	private function clean_article_content( $text )
	{
		if ( empty( $text ) ) {
			return '';
		}

		// Decode HTML entities first
		$text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5 );

		// Remove comments
		$text = preg_replace( '/<!--.*?-->/s', '', $text );

		// Remove <figure> blocks (images, captions – you'll handle images separately if needed)
		$text = preg_replace( '/<figure[^>]*>.*?<\/figure>/is', '', $text );

		// Remove all attributes (href, class, id, style, target, data-*, etc.)
		$text = preg_replace( '/\s+(href|target|class|id|style|data-[a-z-]+|onclick|onload)=[\'"][^\'"]*[\'"]/', '', $text );

		// Allow safe tags for formatting (p, br, ul, ol, li, b, strong, em, blockquote, h2-h6)
		$allowed_tags = '<p><br><ul><ol><li><b><strong><em><i><blockquote><h2><h3><h4><h5><h6>';
		$text = strip_tags( $text, $allowed_tags );

		// Clean up unwanted empty tags
		$text = preg_replace( '/<(div|center|span|section)[^>]*>/i', '', $text );
		$text = preg_replace( '/<\/(div|center|span|section)>/i', '', $text );

		// Convert <strong> to <b> for simplicity
		$text = str_replace( ['<strong>', '</strong>'], ['<b>', '</b>'], $text );

		// Improve paragraph spacing: ensure new lines after </p>
		$text = preg_replace( '/<\/p>/i', "</p>\n", $text );

		// Normalize whitespace (multiple spaces/tabs to single space)
		$text = preg_replace( '/\s+/', ' ', $text );

		// Final trim
		return trim( $text );
	}
 
}