<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

require 'php-mailer/Exception.php';
require 'php-mailer/PHPMailer.php';
require 'php-mailer/SMTP.php';

function health_fonts_url($post_id)
{
    $fonts_url = '';
    $main_font = get_post_meta($post_id, 'page_google_font', 1);
    $main_font_title = get_post_meta($post_id, 'page_google_font_title', 1);
    $main_font_subtitle = get_post_meta($post_id, 'page_google_font_subtitle', 1);
    $health_font = health_get_option('font', get_option('health_default_font'));
    $health_font_weights = health_get_option('font_weights', get_option('health_default_font_weights'));
    $health_title = health_get_option('title_font', get_option('health_default_title'));
    $health_title_weights = health_get_option('title_font_weights', get_option('health_default_title_weights'));
    $health_subtitle = health_get_option('subtitle_font', get_option('health_default_subtitle'));
    $health_subtitle_weights = health_get_option('subtitle_font_weights', get_option('health_default_subtitle_weights'));

    $health = _x('on', 'Roboto fonts: on or off', 'health');

    if ('off' !== $health || $main_font != '' || $main_font_title != '') {
        $font_families = array();

        if ('off' !== $health) {
            $font_families[] = 'Source Sans Pro:300,400,500,600,700';
        }

        if ($main_font != '') {
            $font_families[] = $main_font;
        }

        if ($main_font_title != '') {
            $font_families[] = $main_font_title;
        }

        if ($health_font != '') {
            $cf = $health_font;
            if ($health_font_weights != '') {
                $cf .= ':' . $health_font_weights;
            }

            $font_families[] = $cf;
        }

        if ($health_title != '') {
            $cf = $health_title;
            if ($health_title_weights != '') {
                $cf .= ':' . $health_title_weights;
            }

            $font_families[] = $cf;
        }

        if ($health_subtitle != '') {
            $cf = $health_subtitle;
            if ($health_subtitle_weights != '') {
                $cf .= ':' . $health_subtitle_weights;
            }

            $font_families[] = $cf;
        }

        $query_args = array(
            'family' => str_replace('%2B', '+', urlencode(implode('|', $font_families))),
            'subset' => urlencode('latin,latin-ext'),
        );

        $fonts_url = add_query_arg($query_args, 'https://fonts.googleapis.com/css');
    }

    return esc_url_raw($fonts_url);
}

add_filter('woocommerce_enqueue_styles', 'health_load_woo_styles');
function health_load_woo_styles($styles)
{
    if (isset($styles['woocommerce-general']) && isset($styles['woocommerce-general']['src'])) {
        $styles['woocommerce-general']['src'] = get_template_directory_uri() . '/assets/woocommerce/css/woocommerce.css';
    }
    return $styles;
}

function health_load_styles()
{

    wp_enqueue_style('health-style', get_stylesheet_uri());

    /* PRIMARY CSS */
    if (health_get_option('general_settings_responsive', '1')) {
        wp_enqueue_style('health-responsive', get_template_directory_uri() . '/css/responsive.css');
    } else {
        wp_enqueue_style('health-no-responsive', get_template_directory_uri() . '/css/no-responsive.css');
    }
    wp_enqueue_style('health-fonts', health_fonts_url(get_the_ID()), array(), null);
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/assets/bootstrap/css/bootstrap.css');

    /* THEMES CSS */
    wp_enqueue_style('health-global', get_template_directory_uri() . '/css/global.css');

    // ASSETS CSS

    if (health_get_option('minify_styles_scripts') != 'yes') {
        wp_enqueue_style('font-awesome', get_template_directory_uri() . '/fonts/font-awesome/css/font-awesome.css');
        wp_enqueue_style('fancybox', get_template_directory_uri() . '/assets/fancybox/fancybox.css');
        wp_enqueue_style('owl', get_template_directory_uri() . '/assets/owl/owl.css');
        wp_enqueue_style('bxslider', get_template_directory_uri() . '/assets/bxslider/bxslider.css');
        wp_enqueue_style('flexslider', get_template_directory_uri() . '/assets/flexslider/flexslider.css');
        wp_enqueue_style('slabText', get_template_directory_uri() . '/assets/slabText/css/slabtext.css');
        wp_enqueue_style('og-grid', get_template_directory_uri() . '/assets/og-grid/og-grid.css');
        wp_enqueue_style('animsition', get_template_directory_uri() . '/assets/animsition/animsition.css');
        wp_enqueue_style('easydropdown', get_template_directory_uri() . '/assets/easydropdown/css/easydropdown.metro.css');
        wp_enqueue_style('fancybox', get_template_directory_uri() . '/assets/fancybox/fancybox.css');
        wp_enqueue_style('bxSlider', get_template_directory_uri() . '/assets/bxslider/bxslider.css');
        wp_enqueue_style('og-grid', get_template_directory_uri() . '/assets/og-grid/og-grid.css');

    } else {
        wp_enqueue_style('minify-css', get_template_directory_uri() . '/assets/minify/minify.css');
        wp_enqueue_style('font-awesome', get_template_directory_uri() . '/fonts/font-awesome/css/font-awesome.css');
    }

    /* HEADER CSS */
    wp_enqueue_style('health-header', get_template_directory_uri() . '/assets/header/header.css');
    wp_enqueue_style('health-header-yamm', get_template_directory_uri() . '/assets/header/yamm.css');

    wp_enqueue_style('health-dynamic-styles', admin_url('admin-ajax.php') . '?action=dynamic_styles&pageID=' . get_the_ID());

}
add_action('wp_enqueue_scripts', 'health_load_styles');

function health_dynamic_styles()
{
    include get_template_directory() . '/library/dynamic-styles/index.php';
    exit;
}
add_action('wp_ajax_dynamic_styles', 'health_dynamic_styles');
add_action('wp_ajax_nopriv_dynamic_styles', 'health_dynamic_styles');

function health_load_scripts()
{

    $map_api = health_get_option('map_api', 'AIzaSyBqQ_bBw186KJnMcRByvn5ffZueg88wp1E');

    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }

    /* Assets JS */

    if (health_get_option('minify_styles_scripts') != 'yes') {
        wp_enqueue_script('animsition', get_template_directory_uri() . '/assets/animsition/animsition.js', array('jquery'), '1.0', false);
        wp_enqueue_script('bootstrap', get_template_directory_uri() . '/assets/bootstrap/js/bootstrap.js', array('jquery'), '', false);
        wp_enqueue_script('slabText', get_template_directory_uri() . '/assets/slabText/js/jquery.slabtext.js', array(), '1.0', true);
        wp_enqueue_script('cssua', get_template_directory_uri() . '/assets/cssua/cssua.js', array('jquery'), '1.0', false);
        wp_enqueue_script('waypoints', get_template_directory_uri() . '/assets/waypoints/waypoints.js', array('jquery'), '1.0', false);
        wp_enqueue_script('modernizr', get_template_directory_uri() . '/assets/modernizr/modernizr.js', array('jquery'), '1.0', false);
        wp_enqueue_script('easypiechart', get_template_directory_uri() . '/assets/easypiechart/easypiechart.js', array('jquery'), '1.0', false);
        wp_enqueue_script('jarallax', get_template_directory_uri() . '/assets/jarallax/jarallax.js', array('jquery'), '1.0', false);
        wp_enqueue_script('imagesloaded', get_template_directory_uri() . '/assets/og-grid/og-grid.js', array('jquery'), '1.0', false);
        wp_enqueue_script('isotope', get_template_directory_uri() . '/assets/isotope/isotope.js', array('jquery'), '1.0', false);
        wp_enqueue_script('owl', get_template_directory_uri() . '/assets/owl/owl.js', array('jquery'), '1.0', false);
        wp_enqueue_script('cd-pricing', get_template_directory_uri() . '/assets/cd-pricing/cd-pricing.js', array('jquery'), '1.0', false);
        wp_enqueue_script('wow', get_template_directory_uri() . '/assets/wow/wow.js', array('jquery'), '1.0', false);
        wp_enqueue_script('sticky-kit', get_template_directory_uri() . '/assets/sticky-kit/sticky-kit.js', array('jquery'), '1.0', false);
        wp_enqueue_script('scrollie', get_template_directory_uri() . '/assets/scrollie/scrollie.js', array('jquery'), '1.0', false);
        wp_enqueue_script('bxslider', get_template_directory_uri() . '/assets/scrollie/scrollie.js', array('jquery'), '1.0', false);
        wp_enqueue_script('flexslider', get_template_directory_uri() . '/assets/flexslider/flexslider.js', array('jquery'), '1.0', false);
        wp_enqueue_script('easydropdown', get_template_directory_uri() . '/assets/easydropdown/js/jquery.easydropdown.js', array('jquery'), '1.0', false);
        wp_enqueue_script('fancybox', get_template_directory_uri() . '/assets/fancybox/fancybox.js', array('jquery'), '1.0', false);
        wp_enqueue_script('bxSlider', get_template_directory_uri() . '/assets/bxslider/bxslider.js', array('jquery'), '1.0', false);
        wp_enqueue_script('og-grid', get_template_directory_uri() . '/assets/og-grid/og-grid.js', array('jquery'), '1.0', false);

        wp_enqueue_script('colorbox', get_template_directory_uri() . '/assets/colorbox/jquery.colorbox-min.js', array('jquery'), '1.0', false);
        wp_enqueue_script('uniform', get_template_directory_uri() . '/assets/uniform/jquery.uniform.min.js', array('jquery'), '1.0', false);
        wp_enqueue_script('mapster', get_template_directory_uri() . '/assets/mapster/jquery.imagemapster.min.js', array('jquery'), '1.0', false);
        wp_enqueue_script('scrollbar', get_template_directory_uri() . '/assets/scrollbar/jquery.scrollbar.min.js', array('jquery'), '1.0', false);
        wp_enqueue_script('ve', get_template_directory_uri() . '/assets/validation-engine/jquery.validationEngine.js', array('jquery'), '1.0', false);
        wp_enqueue_script('ve-loc', get_template_directory_uri() . '/assets/validation-engine/jquery.validationEngine-ro.js', array('jquery'), '1.0', false);

    } else {
        wp_enqueue_script('minify-js', get_template_directory_uri() . '/assets/minify/minify.js', array('jquery'), '1.0', false);
    }

    /* HEADER JS */
    wp_enqueue_script('health-slidebar', get_template_directory_uri() . '/assets/header/slidebar.js', array('jquery'), '1.0', false);
    wp_enqueue_script('health-header', get_template_directory_uri() . '/assets/header/header.js', array('jquery'), '1.0', true);
    wp_enqueue_script('health-slidebars', get_template_directory_uri() . '/assets/header/slidebars.js', array('jquery'), '1.0', true);

    // CUSTOM SCRIPT
    wp_enqueue_script('health-custom', get_template_directory_uri() . '/js/theme.js', array(), '1.1', true);

    wp_add_inline_script('health-custom', 'jQuery(document).ready(function($) {Grid.init();});');

    // AMNISITION
    if ((health_get_option('page_loader_settings_use', 'off') == 'usemain' && is_front_page()) || health_get_option('page_loader_settings_use', 'off') == 'useall') {
        $width = health_get_option('page_loader_img_width', '56') . 'px';
        $height = health_get_option('page_loader_img_height', '56') . 'px';
        $width_2 = $width / 2;
        $height_2 = $height / 2;

        $animsition_css = "
		.animsition-loading {
		    width: $width !important;
		    height:$height !important;
		    margin-top: -" . $height_2 . "px !important;
		    margin-left: -" . $width_2 . "px !important;
		    }";
        wp_add_inline_style('health-header', $animsition_css);
    }

}
add_action('wp_enqueue_scripts', 'health_load_scripts');

add_filter('health_grid_portfolio_enq', 'health_portfolio_enqueue');
function health_portfolio_enqueue()
{
    wp_enqueue_style('health-grid');
    wp_enqueue_script('health-grid');
}

add_filter('body_class', 'health_browser_body_class');
function health_browser_body_class($classes = '')
{

    if ((get_post_meta(get_the_ID(), 'page_main_color', 1) != '' && get_post_meta(get_the_ID(), 'page_gradient_color', 1) != '')
        || (get_post_meta(get_the_ID(), 'page_main_color', 1) == '' && health_get_option('style_settings_gradient_color', get_option('health_default_gradient_color')) != '')) {
        $classes[] = 'enable-gradient';
    } else {
        $classes[] = 'disable-gradient';
    }

    return $classes;
}

//CUSTOM

function webicon_add_js_vars()
{

    wp_localize_script('health-custom', '_cabinet', array(
        'site_url' => get_template_directory_uri(),
        'ajax_url' => admin_url('admin-ajax.php'),
    ));
}
add_action('wp_enqueue_scripts', 'webicon_add_js_vars', 99);

function _popup()
{
    global $wpdb;

    $teeth_ordinal = $_GET['t'];
    $result = $wpdb->get_results("SELECT * FROM teeth WHERE ordinal = $teeth_ordinal");

    echo json_encode($result[0]);
    wp_die();
}

add_action('wp_ajax_nopriv__popup', '_popup');
add_action('wp_ajax__popup', '_popup');

function _questions()
{
    global $wpdb;

    $question_type = $_GET['questions_type_id'];

    $questions = array();
    $questions_result = $wpdb->get_results("SELECT * FROM questions WHERE type = $question_type ORDER BY weight ASC");

    foreach ($questions_result as $q_row) {
        $questions[] = array(
            'qid' => $q_row->qid,
            'name' => $q_row->name,
        );
    }

    $answers = array();
    $answers_result = $wpdb->get_results("SELECT * FROM answers ORDER BY weight ASC");

    foreach ($answers_result as $a_row) {
        $answers[] = array(
            'aid' => $a_row->aid,
            'value' => $a_row->value,
            'qid' => $a_row->qid,
        );
    }

    $output = '';

    foreach ($questions as $question) {

        $output .= '<div>
          <select name="' . $question['qid'] . '" id="q-' . $question['qid'] . '" data-label="' . $question['name'] . '">
            <option value="">--' . $question['name'] . '--</option>';

        foreach ($answers as $answer) {

            if ($answer['qid'] == $question['qid']) {
                $output .= '<option value="' . $answer['aid'] . '">' . $answer['value'] . '</option>';
            }

        }

        $output .= '</select>
        </div>';

    }

    echo $output;
    wp_die();
}

add_action('wp_ajax_nopriv__questions', '_questions');
add_action('wp_ajax__questions', '_questions');

function _submit()
{
    $emailData = $_POST['emailData'];
    $message = $emailData['clientInfo'] . "\n" . $emailData['scheduleInfo'] . "\n" . $emailData['problems'];

    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = 2; // Enable verbose debug output
        $mail->isSMTP(); // Set mailer to use SMTP
        $mail->Host = 'sico.media'; // Specify main and backup SMTP servers
        $mail->SMTPAuth = true; // Enable SMTP authentication
        $mail->Username = 'noreply@sico.media'; // SMTP username
        $mail->Password = 'cabinet!@#123'; // SMTP password
        $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587; // TCP port to connect to

        //Recipients
        $mail->setFrom($emailData['clientEmail']);
        $mail->addAddress('ilie.amariucai@gmail.com', 'Joe User'); // Add a recipient
        $mail->addReplyTo('noreply@cabinet-dentaire-paquis.ch', 'Cabinet Dentaire');

        // Content
        $mail->isHTML(true); // Set email format to HTML
        $mail->Subject = 'Reservation';
        $mail->Body = $message;

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
    wp_die();
}

add_action('wp_ajax_nopriv__submit', '_submit');
add_action('wp_ajax__submit', '_submit');