<?php
/**
 * Post template part: Post recipe block
 */

// Get post recipe settings
$post_recipe_image = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_image', true );
$post_recipe_title = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_title', true );
$post_recipe_summary = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_summary', true );
$post_recipe_equipment = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_equipment', true );
$post_recipe_equipment = !empty($post_recipe_equipment) ? explode("\n", str_replace("\r", "", $post_recipe_equipment)) : '';
$post_recipe_ingredients = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_ingredients', true );
$post_recipe_ingredients = !empty($post_recipe_ingredients) ? explode("\n", str_replace("\r", "", $post_recipe_ingredients)) : array();
$post_recipe_notes = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_notes', true );
$post_recipe_instructions_group = get_post_meta( get_the_ID(), '_cookandmeal_recipe_instructions_group', true );

// Icons
$post_recipe_prep_time = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_prep_time', true );
$post_recipe_time = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_time', true );
$post_recipe_servings = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_servings', true );
$post_recipe_total_time = get_post_meta( get_the_ID(), '_cookandmeal_post_recipe_total_time', true );

if(isset($post_recipe_image) && ($post_recipe_image !== '')) {
  $post_recipe_image_style = 'background-image: url('.$post_recipe_image.');';
  $post_recipe_class = ' with-bg';
} else {
  $post_recipe_image_style = '';
  $post_recipe_class = '';
}

$instructions = array();

foreach ( (array) $post_recipe_instructions_group as $key => $value ) {

    $instruction_title = !empty($value['instruction_title']) ? $value['instruction_title'] : '';
    $instruction_text = !empty($value['instruction_text']) ? $value['instruction_text'] : '';

    $instructions[] = array(
        'title' => $instruction_title,
        'text' => $instruction_text
    );

}
?>
<div class="post-recipe-block<?php echo esc_attr($post_recipe_class); ?>" id="post-recipe-block" vocab="https://schema.org/" typeof="Recipe">

    <?php if(!empty($post_recipe_image_style)): ?>
    <div class="post-recipe-image cookandmeal-parallax" data-style="<?php echo esc_attr($post_recipe_image_style); ?>" data-speed="0.1">
        <?php do_action('cookandmeal_social_share'); // this action called from plugin ?>
        <img src="<?php echo esc_url($post_recipe_image); ?>" class="rdf-hidden" alt="<?php echo esc_attr($post_recipe_title); ?>" property="image"/>
    </div>
    <?php endif; ?>

    <div class="post-recipe-header"><h3 property="name"><?php echo esc_html($post_recipe_title); ?></h3><div class="rdf-hidden" property="author"><?php echo get_the_author_meta( 'user_nicename' ); ?></div></div>
    <?php if(!empty($post_recipe_summary)): ?>
    <div class="post-recipe-summary clearfix">
        <?php echo wp_kses_post($post_recipe_summary); ?>
    </div>
    <?php endif; ?>
    <div class="post-recipe-icons-row">
        <?php if($post_recipe_prep_time): ?>
        <div class="post-recipe-icon">
            <i class="fa fa-cutlery" aria-hidden="true"></i>
            <div class="post-recipe-icon-details">
                <div class="post-recipe-icon-title"><?php esc_html_e('prep time', 'cookandmeal'); ?></div>
                <div class="post-recipe-icon-value"><?php echo esc_html($post_recipe_prep_time); ?></div>
            </div>
        </div>
        <?php endif; ?>
        <?php if($post_recipe_time): ?>
        <div class="post-recipe-icon">
            <i class="fa fa-clock-o" aria-hidden="true"></i>
            <div class="post-recipe-icon-details">
                <div class="post-recipe-icon-title"><?php esc_html_e('cooking time', 'cookandmeal'); ?></div>
                <div class="post-recipe-icon-value"><?php echo esc_html($post_recipe_time); ?></div>
            </div>
        </div>
        <?php endif; ?>
        <?php if($post_recipe_servings): ?>
        <div class="post-recipe-icon">
            <i class="fa fa-users" aria-hidden="true"></i>
            <div class="post-recipe-icon-details">
                <div class="post-recipe-icon-title"><?php esc_html_e('servings', 'cookandmeal'); ?></div>
                <div class="post-recipe-icon-value"><?php echo esc_html($post_recipe_servings); ?></div>
            </div>
        </div>
        <?php endif; ?>
        <?php if($post_recipe_total_time): ?>
        <div class="post-recipe-icon">
            <i class="fa fa-check-square-o" aria-hidden="true"></i>
            <div class="post-recipe-icon-details">
                <div class="post-recipe-icon-title"><?php esc_html_e('total time', 'cookandmeal'); ?></div>
                <div class="post-recipe-icon-value"><?php echo esc_html($post_recipe_total_time); ?></div>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <?php if(count($post_recipe_equipment) > 0 || count($post_recipe_ingredients) > 0): ?>
    <div class="post-recipe-details">
        <?php if(count($post_recipe_equipment) > 0): ?>
        <div class="post-recipe-details-column">
            <h4><?php esc_html_e('Equipment', 'cookandmeal'); ?></h4>
            <ul>
                <?php
                foreach ($post_recipe_equipment as $value) {
                    echo '<li><i class="fa fa-check" aria-hidden="true"></i><p>'.wp_kses_post($value).'</p></li>';
                }
                ?>
            </ul>
        </div>
        <?php endif; ?>
        <?php if(count($post_recipe_ingredients) > 0): ?>
        <div class="post-recipe-details-column">

            <h4><?php esc_html_e('Ingredients', 'cookandmeal'); ?></h4>
            <ul>
                <?php
                foreach ($post_recipe_ingredients as $value) {
                    echo '<li><i class="fa fa-check" aria-hidden="true"></i><p property="recipeIngredient">'.wp_kses_post($value).'</p></li>';
                }
                ?>
            </ul>

        </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <?php $i = 0; if(count($instructions) > 0): ?>
    <div class="post-recipe-instructions-group">
        <h4><?php esc_html_e('Instructions', 'cookandmeal'); ?></h4>
        <?php foreach ($instructions as $instruction): ?>
        <?php $i++; ?>
        <div class="post-recipe-instruction">
            <div class="post-recipe-step"><?php echo esc_html($i); ?></div>
            <div class="post-recipe-instruction-content">
                <?php if(!empty($instruction['title'])): ?>
                <div class="post-recipe-instruction-title"><h4 property="recipeInstructions"><?php echo wp_kses_post($instruction['title']); ?></h4></div>
                <?php endif; ?>
                <?php if(!empty($instruction['text'])): ?>
                <div class="post-recipe-instruction-text" property="recipeInstructions"><?php echo wp_kses_post($instruction['text']); ?></div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <?php if(!empty($post_recipe_notes)): ?>
    <div class="post-recipe-notes">
        <h4><?php esc_html_e('Notes', 'cookandmeal'); ?></h4>
        <?php echo wp_kses_post($post_recipe_notes); ?>
    </div>
    <?php endif; ?>
</div>
