<?php
/**
 *
 * Main controller
 *
 * @package halloween-panda
 * @subpackage _controllers
 * @since Halloween Panda 1.0.0
 */

namespace PixHalloweenPanda\Controllers;

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

require_once PIX_HAL_PAN_PATH . '_models/hp-main.php';
use PixHalloweenPanda\Models\Main as PixHalPanMainModel;

/**
 * Class Halloween Panda Controller
 *
 * @category Controller
 * @package  halloween-panda
 * @author  IV | PIXOLETTE
 * @license  http://www.pixollete.com
 * @link     http://www.pixollete.com
 */
class Main {

	/**
	 *
	 * Init function.
	 */
	function init_backend() {
		// Init model to retrive stored options data.
		$hp_model = new PixHalPanMainModel();
		$hp_options = json_decode( $hp_model->options_data );

		if ( isset( $_GET['section'] ) ) {
			$tab_section = $_GET['section'];
		} else {
			$tab_section = 'general';
		}

		if ( isset( $_POST['pix-hp-section'] ) ) {
			$errors = false;
			switch ( $_POST['pix-hp-section'] ) {
				case 'pix-hp-general':
					if ( in_array( $_POST['px_hp_display_type'], array( 'pumpkins', 'ghosts', 'carrots', 'dark_world', 'animation' ) ) ) {
						$data['px_hp_display_type'] = sanitize_text_field( $_POST['px_hp_display_type'] );
					} else {
						$errors['px_hp_display_type'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					if ( in_array( $_POST['px_hp_is_active'], array( 'yes', 'no' ) ) ) {
						$data['px_hp_is_active'] = sanitize_text_field( $_POST['px_hp_is_active'] );
					} else {
						$errors['px_hp_is_active'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					if ( in_array( $_POST['px_hp_display_position'], array( 'bottom', 'top' ) ) ) {
						$data['px_hp_display_position'] = sanitize_text_field( $_POST['px_hp_display_position'] );
					} else {
						$errors['px_hp_display_position'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					if ( in_array( $_POST['px_hp_is_fixed'], array( 'yes', 'no' ) ) ) {
						$data['px_hp_is_fixed'] = sanitize_text_field( $_POST['px_hp_is_fixed'] );
					} else {
						$errors['px_hp_is_fixed'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					$data['px_hp_pumpkin_fall'] = $hp_options->px_hp_pumpkin_fall;

					$data['px_hp_display_popup'] = $hp_options->px_hp_display_popup;
					$data['px_hp_popup_type'] = $hp_options->px_hp_popup_type;
					$data['px_hp_popup_position'] = $hp_options->px_hp_popup_position;
					break;
				case 'pix-hp-monsterfall':
					if ( in_array( $_POST['px_hp_pumpkin_fall'], array( 'yes', 'no' ) ) ) {
						$data['px_hp_pumpkin_fall'] = sanitize_text_field( $_POST['px_hp_pumpkin_fall'] );
					} else {
						$errors['px_hp_pumpkin_fall'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					$data['px_hp_display_type'] = $hp_options->px_hp_display_type;
					$data['px_hp_is_active'] = $hp_options->px_hp_is_active;
					$data['px_hp_display_position'] = $hp_options->px_hp_display_position;
					$data['px_hp_is_fixed'] = $hp_options->px_hp_is_fixed;

					$data['px_hp_display_popup'] = $hp_options->px_hp_display_popup;
					$data['px_hp_popup_type'] = $hp_options->px_hp_popup_type;
					$data['px_hp_popup_position'] = $hp_options->px_hp_popup_position;
					break;
				case 'pix-hp-popups':
					if ( in_array( $_POST['px_hp_display_popup'], array( 'yes', 'no' ) ) ) {
						$data['px_hp_display_popup'] = sanitize_text_field( $_POST['px_hp_display_popup'] );
					} else {
						$errors['px_hp_display_popup'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					if ( intval( $_POST['px_hp_popup_type'] ) && in_array( $_POST['px_hp_popup_type'], array( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16' ) ) ) {
						$data['px_hp_popup_type'] = sanitize_text_field( $_POST['px_hp_popup_type'] );
					} else {
						$errors['px_hp_popup_type'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}
					if ( in_array( $_POST['px_hp_popup_position'], array( 'right', 'left' ) ) ) {
						$data['px_hp_popup_position'] = sanitize_text_field( $_POST['px_hp_popup_position'] );
					} else {
						$errors['px_hp_popup_position'] = esc_html( __( 'Invalid type', 'halloween-panda' ) );
					}

					$data['px_hp_display_type'] = $hp_options->px_hp_display_type;
					$data['px_hp_is_active'] = $hp_options->px_hp_is_active;
					$data['px_hp_display_position'] = $hp_options->px_hp_display_position;
					$data['px_hp_is_fixed'] = $hp_options->px_hp_is_fixed;

					$data['px_hp_pumpkin_fall'] = $hp_options->px_hp_pumpkin_fall;
					break;
			}
			if ( ! $errors ) {
				$hp_model->save_options( $data );
				$hp_model->init();
				$hp_options = json_decode( $hp_model->options_data );
			}
		}
		include PIX_HAL_PAN_PATH . '_views/hp-backend.php';
	}

}
