<?php

/**
 * Śnieżna functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage rso_theme
 * @since Śnieżna 1.0
 */

define('_RSO_THEME_PATH', __DIR__);

add_theme_support('post-thumbnails');

// ---------------- Include scripts and styles -----------------------------------------------------------------------------------
function rso_register_scripts() {
    wp_enqueue_style('rso_fontawesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css');
    wp_enqueue_style('rso_jqueryui', get_template_directory_uri() . '/css/jquery-ui.css');
    wp_enqueue_style('rso_reset', get_template_directory_uri() . '/css/reset.css');
    wp_enqueue_style('rso_select2', get_template_directory_uri() . '/css/select2.css');
    wp_enqueue_style('rso_slick', get_template_directory_uri() . '/css/slick.css');
    wp_enqueue_style('rso_aos', get_template_directory_uri() . '/css/aos.css');
    wp_enqueue_style('rso_theme_style', get_template_directory_uri() . '/style.css', [], '3.15');

    wp_enqueue_script('rso_jquery_js', get_template_directory_uri() . '/js/jquery-3.7.1.min.js');
    wp_enqueue_script('rso_jqueryui_js', get_template_directory_uri() . '/js/jquery-ui.js');
    wp_enqueue_script('rso_select2_js', get_template_directory_uri() . '/js/select2.full.min.js');
    wp_enqueue_script('rso_slick_js', get_template_directory_uri() . '/js/slick.min.js');
    wp_enqueue_script('rso_aos_js', get_template_directory_uri() . '/js/aos.js');

    wp_register_script('search-apartments-js', get_template_directory_uri() . '/js/searchApartments.js', [], '1.5');
    wp_localize_script('search-apartments-js', 'search_apartments_ajax', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'noposts' => __('No older posts found', 'twentyfifteen'),
    )
    );
    wp_enqueue_script('search-apartments-js');

    wp_enqueue_script('rso_main_js', get_template_directory_uri() . '/js/main.js', [], '1.39');
}
add_action( 'wp_enqueue_scripts', 'rso_register_scripts' );

// ---------------- ACF building page ----------------------------------------------------------------------------------------
function acfOptionsPagesInit(){
    if(function_exists('acf_add_options_page')){
        acf_add_options_page([
            'page_title'    => 'Budynek',
            'menu_title'    => 'Budynek',
            'redirect'      => false,
            'capability'    => 'edit_posts'
        ]);
    }
}
add_action('acf/init', 'acfOptionsPagesInit');

// ---------------- ACF theme option page ------------------------------------------------------------------------------
if( function_exists('acf_add_options_page') ) {
    acf_add_options_page([
        'page_title'    => 'Główne ustawienia szablonu',
        'menu_title'    => 'Ust. szablonu',
        'redirect'      => false,
    ]);
}

function reg_mymenu() {
    register_nav_menu('main-menu', __('Menu główne'));
}
add_action( 'init', 'reg_mymenu');


add_image_size( 'news_list', 435, 435, true );
add_image_size( 'news_single', 899, 899, true );

function shortString($string, $max)
{
    $string = strip_tags($string);

    if (strlen($string) > $max) {
        $string = substr($string, 0, $max);

        if ($pos = strrpos($string, ' ')) {
            return substr($string, 0, $pos) . '...';
        } else {
            return substr($string, 0, $max) . '...';
        }
    } else {
        return $string;
    }
}

function remove_jquery_migrate($scripts)
{
    if (!is_admin() && isset($scripts->registered['jquery'])) {
        $script = $scripts->registered['jquery'];
        if ($script->deps) {
            // Check whether the script has any dependencies

            $script->deps = array_diff($script->deps, array('jquery-migrate'));
        }
    }
}
add_action('wp_default_scripts', 'remove_jquery_migrate');

add_action('wp_ajax_nopriv_search_apartments_ajax', 'search_apartments_ajax');
add_action('wp_ajax_search_apartments_ajax', 'search_apartments_ajax');

// paginacja
function the_paginate($query = null, $total = null, $pageName = "paged")
{
    global $wp_query;

    if ($total == null) {
        $total_query = ($query == null) ? $wp_query->max_num_pages : $query->max_num_pages;
    } else {
        $total_query = $total;
    }

    $pages = paginate_links(
        array(
            'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
            'format' => '?' . $pageName . '=%#%',
            'current' => max(1, get_query_var($pageName)),
            'total' => $total_query,
            'prev_text' => '<i class="fa fa-angle-left" aria-hidden="true"></i>',
            'next_text' => '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            'type' => 'array',
        )
    );

    if (is_array($pages)) {
        echo '<ul class="pagination">';
        $paged = (get_query_var($pageName) == 0) ? 1 : get_query_var($pageName);
        foreach ($pages as $page) {
            echo "<li>$page</li>";
        }
        echo '</ul>';
    }
}

function custom_login_styles() {
    wp_enqueue_style('custom-login-style', get_template_directory_uri() . '/css/custom-login.css');
}
add_action('login_enqueue_scripts', 'custom_login_styles');


function formatDate($str)
{
    $array = explode("-", $str);
    $month = (int)$array[1];
    $months = ['stycznia', 'lutego', 'marca', 'kwietnia', 'maja', 'czerwca', 'lipca', 'sierpnia', 'września', 'października', 'listopada', 'grudnia'];

    return $array[2] . ' ' . $months[$month - 1] . ' ' . $array[0];
}

$rso_postTypes = [
    'galeria' => [
        'menu_position' => 4,
        'show_in_menu'  => true,
        'public'        => true,
        'has_archive'   => true,
        'hierarchical'  => true,
        'menu_icon'     => 'dashicons-admin-post',
        'rewrite'       => ['slug'=>'zdjecia'],
        'labels'        => [
            'name'          => 'Galeria',
            'singular_name' => 'Galeria',
            'add_new'       => 'Dodaj wpis',
            'add_new_item'  => 'Dodaj wpis',
        ],
        'supports'      => ['title', 'thumbnail', 'editor'],
    ],
];

add_action('init', function() {
    global $rso_postTypes;

    foreach($rso_postTypes as $type => $args){
        register_post_type($type, $args);
    }
});

$rso_postTypes = [
    'mieszkania' => [
        'menu_position' => 5,
        'show_in_menu'  => true,
        'public'        => true,
        'has_archive'   => true,
        'hierarchical'  => true,
        'menu_icon'     => 'dashicons-admin-post',
        'rewrite'       => ['slug'=>'mieszkanie'],
        'labels'        => [
            'name'          => 'Mieszkania',
            'singular_name' => 'Mieszkania',
            'add_new'       => 'Dodaj wpis',
            'add_new_item'  => 'Dodaj wpis',
        ],
        'supports'      => ['title', 'thumbnail', 'editor'],
    ],
];

add_action('init', function() {
    global $rso_postTypes;

    foreach($rso_postTypes as $type => $args){
        register_post_type($type, $args);
    }
});

function getLevelStatus($levels) {

    $array = [];

    foreach($levels as $level) {
        $array[$level['id_pietra']] = [
            'free' => 0,
            'reserved' => 0,
            'sold' => 0,
            'level' => $level['nazwa_pietra']
        ];

        foreach($level['mieszkania'] as $apartment) {
            if ($apartment['status'] == 'zarezerwowane') {
                $array[$level['id_pietra']]['reserved']++;
            } elseif ($apartment['status'] == 'sprzedane') {
                $array[$level['id_pietra']]['sold']++;
            } else {
                $array[$level['id_pietra']]['free']++;
            }
        }
    }

    return $array; 
}

function search_apartments_ajax()
{
    $building = $_POST['building'];
    $level = $_POST['level'];
    $room = $_POST['room'];
    $status = $_POST['status'];
    $from = $_POST['from'];
    $to = $_POST['to'];

    include _RSO_THEME_PATH . '/parts/searchApartments.php';
}

add_action('wp_ajax_nopriv_search_apartments_ajax', 'search_apartments_ajax');
add_action('wp_ajax_search_apartments_ajax', 'search_apartments_ajax');


