<?php
declare(strict_types=1);

/**
 * OnePanel B3: action handler group: apps
 * Generated from legacy action.php blocks for maintainability.
 */

function op_handle_apps(string $actionKey): void {
  switch ($actionKey) {
    case 'updateintro':
      $path = "";
      	if (isset($_FILES['mp4file']['name']) && $_FILES['mp4file']['name'] != "") {
      		//first checkthe extension
      		$allowed = array('mp4');
      		$filename = $_FILES['mp4file']['name'];
      		$ext = pathinfo($filename, PATHINFO_EXTENSION);
      		if (!in_array(strtolower($ext), $allowed)) {
      			echo 'Only MP4 files allowed, filename not allowed: ' . $_FILES['mp4file']['name'];
      			exit;
      		} else {
      			$path = time() . "." . $ext;
      			move_uploaded_file($_FILES['mp4file']['tmp_name'], "intro-videos/" . $path);
      		}
      	}
      	$active = false;
      	if ($_POST['active'] == "on") {
      		$active = true;
      	}
      	saveIntro($path, $active);
      	header("Location: dashboard.php?intro");
      	exit;
      return;
    case 'updatefemto':
      $showLive = false;
      	if ($_POST['showLive'] == "on") {
      		$showLive = true;
      	}
      	$showSeries = false;
      	if ($_POST['showSeries'] == "on") {
      		$showSeries = true;
      	}
      	$showVOD = false;
      	if ($_POST['showVOD'] == "on") {
      		$showVOD = true;
      	}
      	$showEPG = false;
      	if ($_POST['showEPG'] == "on") {
      		$showEPG = true;
      	}
      	$showContentUpdate = false;
      	if ($_POST['showContentUpdate'] == "on") {
      		$showContentUpdate = true;
      	}
      
      	saveFemtoOptions($showLive, $showSeries, $showVOD, $showEPG, $showContentUpdate, $_POST['proxyTraffic']);
      	header("Location: dashboard.php?femto-settings");
      	exit;
      return;
    case 'updatesmarters':
      saveSmartersOptions($_POST['proxyTraffic']);
      	header("Location: dashboard.php?smarters-settings");
      	exit;
      return;
    case 'updatesmartersbackgrounds':
      for($i=1; $i<=7; $i++)
      	{
      		$path = "";
      		$filePost = 'img' . $i . 'file';
      		$captionPost = 'img' . $i . 'caption';
      		if (isset($_FILES[$filePost]['name']) && $_FILES[$filePost]['name'] != "") {
      			//first checkthe extension
      			$allowed = array('jpeg', 'png', 'jpg');
      			$filename = $_FILES[$filePost]['name'];
      			$ext = pathinfo($filename, PATHINFO_EXTENSION);
      			if (!in_array(strtolower($ext), $allowed)) {
      				echo 'Only PNG and JPG/JPEG allowed, filename not allowed: ' . $_FILES[$filePost]['name'];
      				exit;
      			} else {
      				$path = $i . time() . "." . $ext;
      				move_uploaded_file($_FILES[$filePost]['tmp_name'], "ad-images/" . $path);
      			}
      		}
      		saveSmartersBg($i, $path, $_POST[$captionPost]);
      	}
      	header("Location: dashboard.php?smarters-backgrounds");
      	exit;
      return;
    case 'updatetivi':
      saveTiviOptions($_POST['proxyTraffic']);
      	header("Location: dashboard.php?tivimate-settings");
      	exit;
      return;
    case 'updateimplayer':
      saveImPlayerOptions($_POST['proxyTraffic']);
      	header("Location: dashboard.php?implayer-settings");
      	exit;
      return;
    case 'updateltq':
      saveLTQOptions($_POST['proxyTraffic']);
      	header("Location: dashboard.php?ltq-settings");
      	exit;
      return;
    case 'updatexciptvplayer':
      $exo_buffer = $_POST['exo_buffer'];
      	$exo_zoom = $_POST['exo_zoom'];
      	$exo_hw = $_POST['exo_hw'] == "on";
      	$exo_subtitles = $_POST['exo_subtitles'] == "on";
      	$exo_volume = $_POST['exo_volume'];
      	$vlc_buffer = $_POST['vlc_buffer'];
      	$vlc_zoom = $_POST['vlc_zoom'];
      	$vlc_hw = $_POST['vlc_hw'] == "on";
      	$vlc_subtitles = $_POST['vlc_subtitles'] == "on";
      	$vlc_volume = $_POST['vlc_volume'];
      	$player_live = $_POST['player_live'];
      	$player_epg = $_POST['player_epg'];
      	$player_vod = $_POST['player_vod'];
      	$player_series = $_POST['player_series'];
      
      	saveXCIPTVOptionsPlayer($exo_buffer, $exo_zoom, $exo_hw, $exo_subtitles, $exo_volume, $vlc_buffer, $vlc_zoom, $vlc_hw, $vlc_subtitles, $vlc_volume, $player_live, $player_epg, $player_vod, $player_series);
      
      	saveSmartersOptions($proxyTraffic);
      	header("Location: dashboard.php?xciptv-player-settings");
      	exit;
      return;
    case 'updatexciptvinterface':
      $show_live = $_POST['show_live'] == "on";
      	$show_epg = $_POST['show_epg'] == "on";
      	$show_vod = $_POST['show_vod'] == "on";
      	$show_series = $_POST['show_series'] == "on";
      	$show_catchup = $_POST['show_catchup'] == "on";
      	$show_radio = $_POST['show_radio'] == "on";
      	$show_multi = $_POST['show_multi'] == "on";
      	$show_favorite = $_POST['show_favorite'] == "on";
      	$show_account = $_POST['show_account'] == "on";
      	$show_reminders = $_POST['show_reminders'] == "on";
      	$show_record = $_POST['show_record'] == "on";
      	$show_vpn = $_POST['show_vpn'] == "on";
      	$show_message = $_POST['show_message'] == "on";
      	$show_update = $_POST['show_update'] == "on";
      	$show_expiry = $_POST['show_expiry'] == "on";
      	$theme = $_POST['theme'];
      	saveXCIPTVOptionsButtons($show_live, $show_epg, $show_vod, $show_series, $show_catchup, $show_radio, $show_multi, $show_favorite, $show_account, $show_reminders, $show_record, $show_vpn, $show_message, $show_update, $show_expiry, $theme);
      	saveXCIPTVOptionsButtonsPortal("2", $show_live = $_POST['show_live2'] == "on", $_POST['show_epg2'] == "on", $_POST['show_vod2'] == "on", $_POST['show_series2'] == "on", $_POST['show_catchup2'] == "on", $_POST['show_radio2'] == "on", $_POST['show_multi2'] == "on", $_POST['show_favorite2'] == "on", $_POST['show_account2'] == "on");
      	saveXCIPTVOptionsButtonsPortal("3", $show_live = $_POST['show_live3'] == "on", $_POST['show_epg3'] == "on", $_POST['show_vod3'] == "on", $_POST['show_series3'] == "on", $_POST['show_catchup3'] == "on", $_POST['show_radio3'] == "on", $_POST['show_multi3'] == "on", $_POST['show_favorite3'] == "on", $_POST['show_account3'] == "on");
      	saveXCIPTVOptionsButtonsPortal("4", $show_live = $_POST['show_live4'] == "on", $_POST['show_epg4'] == "on", $_POST['show_vod4'] == "on", $_POST['show_series4'] == "on", $_POST['show_catchup4'] == "on", $_POST['show_radio4'] == "on", $_POST['show_multi4'] == "on", $_POST['show_favorite4'] == "on", $_POST['show_account4'] == "on");
      	saveXCIPTVOptionsButtonsPortal("5", $show_live = $_POST['show_live5'] == "on", $_POST['show_epg5'] == "on", $_POST['show_vod5'] == "on", $_POST['show_series5'] == "on", $_POST['show_catchup5'] == "on", $_POST['show_radio5'] == "on", $_POST['show_multi5'] == "on", $_POST['show_favorite5'] == "on", $_POST['show_account5'] == "on");
      	header("Location: dashboard.php?xciptv-interface-settings");
      	exit;
      return;
    case 'updatexciptvapp':
      $app_name = $_POST['app_name'];
      	$app_build = $_POST['app_build'];
      	$app_identifier = $_POST['app_identifier'];
      	$login_type = $_POST['login_type'];
      	$login_accounts_button = $_POST['login_accounts_button'] == "on";
      	$log_settings_button = $_POST['log_settings_button'] == "on";
      	$announcements = $_POST['announcements'] == "on";
      	$messages = $_POST['messages'] == "on";
      	$update_user_info = $_POST['update_user_info'] == "on";
      	$developer_name = $_POST['developer_name'];
      	$developer_contact = $_POST['developer_contact'];
      	$signup_url = $_POST['signup_url'];
      	$login_logo = $_POST['login_logo'] == "on";
      	$app_logs = $_POST['app_logs'] == "on";
      	$show_expiry = $_POST['show_expiry'] == "on";
      	$category_count = $_POST['category_count'] == "on";
      	$load_last_channel = $_POST['load_last_channel'] == "on";
      	$proxy_traffic = $_POST['proxy_traffic'];
      	$user_agent = $_POST['user_agent'];
      	$licv4_method = $_POST['licv4_method'];
      	$licv3_key = $_POST['licv3_key'];
      	$licv3_iv = $_POST['licv3_iv'];
      
      	saveXCIPTVOptionsApp($_POST['app_language'],$_POST['support_email'],$_POST['support_phone'], $app_name, $app_build, $app_identifier, $login_type, $login_accounts_button, $log_settings_button, $announcements, $messages, $update_user_info, $developer_name, $developer_contact, $signup_url, $login_logo, $app_logs, $category_count, $user_agent, $load_last_channel, $proxy_traffic, $licv4_method, $licv3_key, $licv3_iv);
      	header("Location: dashboard.php?xciptv-app-settings");
      	exit;
      return;
    case 'updatesports':
      saveSportsOptions($_POST['api_key'], $_POST['header_name'], $_POST['border_colour'], $_POST['background_colour'], $_POST['text_colour']);
      	header("Location: dashboard.php?sports");
      	exit;
      return;
    case 'updatepurple':
      savePurpleOptions($_POST['proxy_traffic'], $_POST['startup_msg'], $_POST['allow_4k'] == "on", $_POST['vpn'] == "on", $_POST['vpn_login_screen'] == "on", $_POST['allow_cast'] == "on",  $_POST['remote_support'] == "on", $_POST['wifi_option'] == "on", $_POST['setting_option'] == "on", $_POST['app_list_status'] == "on", $_POST['epg_timeshift'] == "on", $_POST['catchup'] == "on", $_POST['epg_catchup'] == "on", $_POST['recording'] == "on", $_POST['multi_recording'] == "on", $_POST['intro_video'] == "on", $_POST['theme_change_allow'] == "on", $_POST['multi_profile'] == "on",  $_POST['server_selection'] == "on", $_POST['about_description'], $_POST['about_developed'], $_POST['about_name'], $_POST['about_skype'], $_POST['about_telegram'], $_POST['about_whatsapp']);
      	header("Location: dashboard.php?purple-settings");
      	exit;
      return;
    case 'updatepurpleinterface':
      savePurpleInterfaceOptions($_POST['app_img'] == "on", $_POST['app_logo'], $_POST['app_mobile_icon'], $_POST['app_tv_banner'],  $_POST['splash_image'], $_POST['back_image'], $_POST['background_auto_change'] == "on", $_POST['background_mannual_change'] == "on", $_POST['background_orverlay_enable'] == "on" ? $_POST['background_orverlay_color_code'] : '', $_POST['background_url1'], $_POST['background_url2'], $_POST['background_url3'], $_POST['background_url4']);
      	header("Location: dashboard.php?purple-interface-settings");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

// ---- B3.2 normalized wrappers ----

function handle_apps_updateintro(array $post): void {
  _op_auth_required();
  $url = op_post_url($post, 'intro', 2048);
  op_try(function() use ($url) { updateIntro($url); }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updatefemto(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updateFemto')) {
      updateFemto(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updatesmarters(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updateSmarters')) {
      updateSmarters(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updateiptvapp(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updateIptvapp')) {
      updateIptvapp(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updatesports(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updateSports')) {
      updateSports(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updatepurple(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updatePurple')) {
      updatePurple(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}

function handle_apps_updatepurpleinterface(array $post): void {
  _op_auth_required();
  // Legacy expects raw POST fields; keep but validate common url fields if present
  op_try(function() use ($post) {
    if (function_exists('updatePurpleInterface')) {
      updatePurpleInterface(...array_values($post)); // best-effort; legacy functions may ignore extra
    }
  }, 'dashboard.php?apps', 'Updated.');
}
