<?php

/*
 * Name: Image+Text+Button
 * Section: content
 * Description: Image, title, text and call to action all in one
 */

/** @var array $options */
/** @var array $composer */

$defaults = array(
    'order' => '',
    'title' => 'An Awesome Title',
    'text' => 'This is just a simple text you should change',
    'font_family' => '',
    'font_size' => '',
    'font_weight' => '',
    'font_color' => '',
    'title_font_family' => '',
    'title_font_size' => '',
    'title_font_weight' => '',
    'title_font_color' => '',
    'title_font_align' => 'center',
    'layout' => 'full',
    'button_url' => '',
    'button_label' => __('Click Here', 'newsletter'),
    'button_background' => '',
    'button_font_color' => '',
    'button_font_family' => '',
    'button_font_size' => '',
    'button_font_weight' => '',
    'block_padding_top' => 30,
    'block_padding_bottom' => 30,
    'block_padding_left' => 0,
    'block_padding_right' => 0,
    'block_background' => '',
);

$options = array_merge($defaults, $options);

if (!empty($options['schema'])) {
    if ($options['schema'] === 'dark') {
        $options['block_background'] = '#000000';
        $options['title_font_color'] = '#ffffff';
        $options['font_color'] = '#ffffff';
        $options['button_font_color'] = '#ffffff';
        $options['button_background'] = '#96969C';
    }

    if ($options['schema'] === 'bright') {
        $options['block_background'] = '#ffffff';
        $options['title_font_color'] = '#000000';
        $options['font_color'] = '#000000';
        $options['button_font_color'] = '#ffffff';
        $options['button_background'] = '#256F9C';
    }

    if ($options['schema'] === 'orangeblue') {
        $options['block_background'] = '#16537e';
        $options['title_font_color'] = '#ffffff';
        $options['title_font_align'] = 'left';
        $options['title_font_size'] = '40px';
        $options['title_font_family'] = 'Times New Roman, Times, serif';

        $options['font_align'] = 'left';
        $options['button_align'] = 'left';

        $options['font_color'] = '#f1c232';
        $options['button_font_color'] = '#444444';
        $options['button_background'] = '#fff';
        $options['block_padding_top'] = 30;
        $options['block_padding_bottom'] = 30;
        $options['block_padding_left'] = 30;
        $options['block_padding_right'] = 30;
    }
}

$layout = $options['layout'];
$order = $options['order'];

if ($layout == 'full') {
    $options = array_merge(array('block_padding_left' => 0, 'block_padding_right' => 0), $options);
} else {
    $options = array_merge(array('block_padding_left' => 15, 'block_padding_right' => 15), $options);
}

$title_style = TNP_Composer::get_title_style($options, 'title', $composer);

$text_style = TNP_Composer::get_text_style($options, '', $composer);

$layout = $options['layout'];

$button_options = $options;
$button_options['button_font_family'] = empty($options['button_font_family']) ? $global_button_font_family : $options['button_font_family'];
$button_options['button_font_size'] = empty($options['button_font_size']) ? $global_button_font_size : $options['button_font_size'];
$button_options['button_font_color'] = empty($options['button_font_color']) ? $global_button_font_color : $options['button_font_color'];
$button_options['button_font_weight'] = empty($options['button_font_weight']) ? $global_button_font_weight : $options['button_font_weight'];
$button_options['button_background'] = empty($options['button_background']) ? $global_button_background_color : $options['button_background'];

$media = false;

if (!empty($options['image']['id'])) {
    if ($layout === 'full') {
        $image_width = $composer['width'] - $options['block_padding_left'] - $options['block_padding_right'];
        $media = tnp_resize_2x($options['image']['id'], [$image_width, 0]);
    } else {
        $td_width = round(($composer['width'] - $options['block_padding_left'] - $options['block_padding_right'] - 20) / 2);
        //$image_width = 300 - $options['block_padding_left'];
        $media = tnp_resize_2x($options['image']['id'], [$td_width, 0]);
    }
    if ($media) {
        if (!empty($options['image_alt'])) {
            $media->alt = $options['image_alt'];
        } else if (!empty($options['title'])) {
            $media->alt = $options['title'];
        } else {
            $alt_texts = array('picture', 'image', 'pic', 'photo');
            $media->alt = $alt_texts[array_rand($alt_texts)];
        }
        $media->link = $options['button_url'];
    }
} else {
    if (!empty($options['image_url'])) {
        $media = new TNP_Media();
        $media->url = $options['image_url'];
    }
}

switch ($layout) {
    case 'left':
    case 'left13':
        include __DIR__ . '/block-left.php';
        return;
    case 'right':
    case 'right13':
        include __DIR__ . '/block-right.php';
        return;
    case 'full':
        include __DIR__ . '/block-full.php';
        return;
}
