<?php

/**
 * @author : Webforia Studio
 */

namespace Retheme\Customizer;

use Retheme\Customizer_Base;
use Kirki\Util\Helper;

class Frontpage extends Customizer_Base {

    public function __construct() {
        $this->set_section();
        $this->add_layout();
        $this->add_image_slider();
        $this->add_custom_trip();
        $this->add_trip_destination();
        $this->add_trip_filter();
        $this->add_post_archive();
        $this->add_image();
        $this->add_text_content();
    }

    public function set_section() {

        $this->add_panel('frontpage', [
            'title' => __('Front Page Builder', 'jelajah-text'),
            'priority' => 1,
        ]);

        $this->add_section('frontpage_layout', [
            'title'       => __('Layout', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_image_slider', [
            'title'       => __('Images Slider', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_trip_1', [
            'title' => __('Custom Trips 1', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_trip_2', [
            'title' => __('Custom Trips 2', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_trip_3', [
            'title' => __('Custom Trips 3', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_post_archive', [
            'title' => __('Latest Posts', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_trip_destination', [
            'title' => __('Trip Destination', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_trip_filter', [
            'title' => __('Trip Filter', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_image', [
            'title' => __('Image', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);

        $this->add_section('frontpage_text_content', [
            'title' => __('Text Content', 'jelajah-text'),
            'panel'       => 'frontpage',
        ]);
    }

    public function add_layout() {
        $section = 'frontpage_layout';

        $this->add_field([
            'type' => 'sortable',
            'settings' => 'frontpage_layout',
            'label' => __('Sections', 'jelajah-text'),
            'section' => $section,
            'description' => __('Select (and re-order) front page sections that you want to display', 'jelajah-text'),
            'default' => [
                'post-archive',
            ],
            'choices' => [
                'image-slider' => __('Image Slider', 'jelajah-text'),
                'trip-1' => __('Custom Trip 1', 'jelajah-text'),
                'trip-2' => __('Custom Trip 2', 'jelajah-text'),
                'trip-3' => __('Custom Trip 3', 'jelajah-text'),
                'trip-destination' => __('Trip Destination', 'jelajah-text'),
                'trip-filter' => __('Trip Filter', 'jelajah-text'),
                'post-archive' => __('Latest Posts', 'jelajah-text'),
                'text-content' => __('Text Content', 'jelajah-text'),
                'image' => __('Image', 'jelajah-text'),
            ],
        ]);
    }

    public function add_image_slider() {

        $section = 'frontpage_image_slider';

        $this->add_header([
            'label' => __('Options', 'jelajah-text'),
            'settings' => "frontpage_image_slider",
            'section' => $section,
            'partial_refresh'    => [
                'homebuilder_image_slider' => [
                    'selector'        => "#frontpage-image-slider .swiper-pagination",
                    'render_callback' => function () {
                        return false;
                    },
                ],
            ],
        ]);

        $this->add_field([
            'type' => 'toggle',
            'section' => $section,
            'settings' => "frontpage_image_slider_autoplay",
            'label' => __('Auto Play', 'jelajah-text'),
        ]);

        $this->add_field([
            'type' => 'toggle',
            'section' => $section,
            'settings' => "frontpage_image_slider_loop",
            'label' => __('Loop', 'jelajah-text'),
        ]);

        $this->add_header([
            'label' => __('Slides', 'jelajah-text'),
            'settings' => "frontpage_image_slides",
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'repeater',
            'label' => __('Image Slides', 'jelajah-text'),
            'tooltip' => __('With each image slide you have the option to upload an image and set the links.  For best results, all of your slides should use images that are identical in size and ratio. One recommended size that works well for most shops is 1500 x 600px.', 'jelajah-text'),
            'section' => $section,
            'settings' => 'frontpage_image_slider_items',
            'row_label' => [
                'type' => 'field',
                'value' => __('Slide Item', 'jelajah-text'),
                'field' => 'title',
            ],
            'button_label' => __("Add New Slide", 'jelajah-text'),
            'fields' => [
                'image' => [
                    'type' => 'image',
                    'label' => __('Image', 'jelajah-text'),
                    'description' => __('Upload an image to be used on desktop (1170px width and free height)', 'jelajah-text'),
                    'choices' => [
                        'save_as' => 'id',
                    ],
                ],
                'image_mobile' => [
                    'type' => 'image',
                    'label' => __('Image on mobile', 'jelajah-text'),
                    'description' => __('Upload an image to be used on mobile, if none is provided, the desktop image will be used (781px width and free height)', 'jelajah-text'),
                    'choices' => [
                        'save_as' => 'id',
                    ],
                ],
                'link_url' => [
                    'type' => 'url',
                    'label' => __('Link URL', 'jelajah-text'),
                ],
                'link_target' => [
                    'type' => 'checkbox',
                    'label' => __('Open link in new tab', 'jelajah-text'),
                ],
            ],

        ]);
    }

    public function add_custom_trip() {

        for ($index = 1; $index <= 3; $index++) {

            $section = "frontpage_trip_{$index}";
            $setting = "frontpage_trip_{$index}";

            $this->add_header([
                'label' => __('Options', 'jelajah-text'),
                'settings' => "frontpage_trip_{$index}",
                'section' => $section,
            ]);

            $this->add_field([
                'type' => 'toggle',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_carousel",
                'label' => __('Carousel', 'jelajah-text'),
            ]);

            $this->add_field([
                'type' => 'select',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_pagination",
                'label' => __('Pagination Style', 'jelajah-text'),
                'default' => 'none',
                'multiple' => 1,
                'choices' => [
                    'none' => __('None', 'jelajah-text'),
                    'loadmore' => __('Load More', 'jelajah-text'),
                ],
                'active_callback' => [
                    [
                        'setting' => "frontpage_trip_{$index}_carousel",
                        'operator' => '!=',
                        'value' => true,
                    ],
                ],
            ]);

            $this->add_header([
                'label' => __('Query', 'jelajah-text'),
                'settings' => "frontpage_trip_{$index}_query",
                'section' => $section,
            ]);

            $this->add_field([
                'type' => 'select',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_query_by",
                'label' => __('Query Post by', 'jelajah-text'),
                'default' => 'latest',
                'multiple' => 1,
                'choices' => [
                    'latest' => __('Latest Posts', 'jelajah-text'),
                    'category' => __('Categories', 'jelajah-text'),
                    'editor-pick' => __('Editor Picks', 'jelajah-text'),
                    'post_id' => __('Post IDs', 'jelajah-text'),
                ],
            ]);

            $this->add_field([
                'type' => 'select',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_category",
                'label' => __('Category', 'jelajah-text'),
                'multiple' => 99,
                'choices' => Helper::get_terms(['taxonomy' => 'category']),
                'active_callback' => [
                    [
                        'setting' => "frontpage_trip_{$index}_query_by",
                        'operator' => '==',
                        'value' => 'category',
                    ],
                ],
            ]);


            $this->add_field([
                'type' => 'text',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_id",
                'label' => __('Post IDs', 'jelajah-text'),
                'description' => __('Separate by comma', 'jelajah-text'),
                'active_callback' => [
                    [
                        'setting' => "frontpage_trip_{$index}_query_by",
                        'operator' => '==',
                        'value' => 'post_id',
                    ],
                ],
            ]);

            $this->add_field([
                'type' => 'select',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_orderby",
                'label' => __('Order By', 'jelajah-text'),
                'default' => 'date',
                'multiple' => 1,
                'choices' => [
                    'ID' => 'Post Id',
                    'author' => 'Post Author',
                    'title' => 'Title',
                    'date' => 'Date',
                    'modified' => 'Last Modified Date',
                    'viewer' => __('Most Viewer'),
                    'comment_count' => __('Most Comment'),
                ],

            ]);

            $this->add_field([
                'type' => 'number',
                'section' => $section,
                'settings' => "frontpage_trip_{$index}_posts_per_page",
                'label' => __('Posts Per Page', 'jelajah-text'),
                'default' => 6,
            ]);

            // section
            $this->add_header([
                'label' => 'Section',
                'settings' => "frontpage_trip_{$index}_section",
                'section' => $section,
            ]);

            $this->add_field([
                'type' => 'text',
                'settings' => "frontpage_trip_{$index}_title",
                'label' => __('Title', 'jelajah-text'),
                'default' => __('Custom Posts', 'jelajah-text'),
                'section' => $section,
            ]);

            $this->add_field([
                'type' => 'textarea',
                'settings' => "frontpage_trip_{$index}_desc",
                'label' => __('Description', 'jelajah-text'),
                'default' => __('Post Description', 'jelajah-text'),
                'section' => $section,
            ]);
        }
    }

    public function add_trip_destination() {

        $section = 'frontpage_trip_destination';

        $this->add_field([
            'type' => 'toggle',
            'section' => $section,
            'settings' => "frontpage_trip_destination_hide_empty",
            'label' => __('Hide empty', 'jelajah-text'),
            'default' => false,
        ]);

        $this->add_field([
            'type' => 'toggle',
            'section' => $section,
            'settings' => "frontpage_trip_destination_term_count",
            'label' => __('Count', 'jelajah-text'),
            'default' => true,
        ]);

        $this->add_field([
            'type' => 'text',
            'settings' => "frontpage_trip_destination_title",
            'label' => __('Title', 'jelajah-text'),
            'default' => __('Destination', 'jelajah-text'),
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'textarea',
            'settings' => "frontpage_trip_destination_desc",
            'label' => __('Description', 'jelajah-text'),
            'section' => $section,
        ]);
    }

    public function add_trip_filter() {

        $section = 'frontpage_trip_filter';

        $this->add_field([
            'type' => 'text',
            'settings' => "frontpage_trip_filter_title",
            'label' => __('Title', 'jelajah-text'),
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'textarea',
            'settings' => "frontpage_trip_filter_desc",
            'label' => __('Description', 'jelajah-text'),
            'section' => $section,
        ]);
    }

    public function add_post_archive() {
        $section = 'rontpage_post_archive';

        $this->add_field([
            'type' => 'text',
            'settings' => "frontpage_post_archive_title",
            'label' => __('Title', 'jelajah-text'),
            'default' => __('Latest Posts', 'jelajah-text'),
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'textarea',
            'settings' => "rontpage_post_archive_desc",
            'label' => __('Description', 'jelajah-text'),
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'select',
            'section' => $section,
            'settings' => "rontpage_post_archive_pagination_style",
            'label' => __('Pagination Style', 'jelajah-text'),
            'default' => 'number',
            'multiple' => 1,
            'choices' => [
                'number' => __('Number', 'jelajah-text'),
                'loadmore' => __('Load More', 'jelajah-text'),
            ],
        ]);
    }

    public function add_text_content() {
        $section = 'frontpage_text_content';

        $this->add_field([
            'type' => 'editor',
            'settings' => "frontpage_text_content",
            'label' => __('Content', 'jelajah-text'),
            'tooltip' => __('Use the rich text editor to add in text into the section', 'jelajah-text'),
            'section' => $section,
        ]);
    }

    public function add_image() {

        $section = 'frontpage_image';

        $this->add_field([
            'type' => 'image',
            'settings' => 'frontpage_image',
            'label' => __('Image', 'jelajah-text'),
            'description' => __('Upload an image to be used for the section. For best results, the image should be at least 1200px wide and it looks best if the image is taller than the overall text you will be adding.', 'jelajah-text'),
            'section' => $section,
            'default' => '',
            'choices' => [
                'save_as' => 'id',
            ],
        ]);

        $this->add_field([
            'type' => 'link',
            'settings' => 'frontpage_image_link_url',
            'label' => __('Link Target', 'jelajah-text'),
            'default' => '#',
            'section' => $section,
        ]);

        $this->add_field([
            'type' => 'toggle',
            'section' => $section,
            'settings' => "frontpage_image_link_new_tab",
            'label' => __('Open in new tab', 'jelajah-text'),
            'default' => false,
        ]);
    }

    // end class
}
new Frontpage;
