<?php

defined('ABSPATH') || exit;

class Wiaas_template {


    public static function init() {
        add_filter('product_type_selector', array(__CLASS__, 'add_template_product_type'));
        add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'custom_product_tabs'));
        add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_all'));
        add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save_wiaastemplate'));
        add_action('woocommerce_process_product_meta_wiaastemplate', array(__CLASS__, 'save_wiaastemplate'));

        // Enqueue scripts.
        add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 11);

    }

    public static function enqueue_scripts() {
        $plugin_url = untrailingslashit(plugins_url('/', WIAAS_FILE));
        wp_register_script('wiaas-template-admin-write-panels', $plugin_url . '/assets/js/wiaas-template-admin-write-panels.js',
            array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));
        wp_register_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js',
            array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));

        wp_enqueue_script('wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js',
            array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));

        wp_enqueue_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js',
            array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));

    }

    /**
     * Add template product type drop down.
     */
    function add_template_product_type($types) {

        // Key should be exactly the same as in the class
        $types['wiaastemplate'] = __('Template', 'wiaastemplate');

        return $types;

    }


    /**
     * Add template product, hardware, services, installation and software tabs product tab.
     */
    function custom_product_tabs($tabs) {

        $tabs['wiaastemplate_hardware'] = array(
            'label' => __('Hardware', 'wiaastemplate_hardware'),
            'target' => 'wiaastemplate_options_hardware',
            'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
        );

        $tabs['wiaastemplate_installation'] = array(
            'label' => __('Installation', 'wiaastemplate_isntallation'),
            'target' => 'wiaastemplate_options_installation',
            'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
        );

        $tabs['wiaastemplate_services'] = array(
            'label' => __('Services', 'wiaastemplate_services'),
            'target' => 'wiaastemplate_options_services',
            'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
        );

        $tabs['wiaastemplate_software'] = array(
            'label' => __('Software', 'wiaastemplate_software'),
            'target' => 'wiaastemplate_options_software',
            'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
        );

        return $tabs;

    }

    /**
     * Add content do template tabs
     */

    public static function wiaastemplate_product_tab_content_all() {

        self::wiaastemplate_product_tab_content('hardware');
        self::wiaastemplate_product_tab_content('installation');
        self::wiaastemplate_product_tab_content('services');
        self::wiaastemplate_product_tab_content('software');
    }


    /**
     * Html contents of the template options product tab.
     *
     * @param $options string main wiaas category
     */

    public static function wiaastemplate_product_tab_content($options) {

        global $post;
        $template_items = get_post_meta($post->ID, '_template_items_' . $options, true);

        ?>
        <div id='wiaastemplate_options<?php echo '_' . $options ?>' class='panel woocommerce_options_panel'><?php
        ?>
        <div class="wiaas-template-items<?php echo '_' . $options ?> "><?php

            if (!empty($template_items)) {

                $loop = 0;

                foreach ($template_items as $item) {

                    $template_category_id = $item['template_category_id'];
                    $title = $item['template_category_title'];
                    $quantity = $item['quantity'];

                    include('views/html-wiaas-template-product.php');

                    $loop++;
                }
            }
            ?></div>

        <select class="wiaas-term-search" id="wiaastemplate_products<?php echo '_' . $options ?>" style="width: 250px;"
                name="template_product"
                data-placeholder="<?php _e('Add product to template&hellip;', 'wiaas'); ?>"
                multiple="multiple" data-limit="500">
            <option></option>
        </select>
        <?php echo wc_help_tip(__('Search for a product and add it to this template by clicking its name in the results list.', 'wiaas')); ?>

        </div><?php


    }


    /**
     * Save the template categories that are bind to template.
     */
    function save_wiaastemplate($post_id) {

        $posted_template_data_hardware = isset($_POST['template_data_hardware']) ? $_POST['template_data_hardware'] : false;
        $posted_template_data_services = isset($_POST['template_data_services']) ? $_POST['template_data_services'] : false;
        $posted_template_data_installation = isset($_POST['template_data_installation']) ? $_POST['template_data_installation'] : false;
        $posted_template_data_software = isset($_POST['template_data_software']) ? $_POST['template_data_software'] : false;

        self::process_and_save_template_product_meta($post_id, $posted_template_data_hardware, 'hardware');
        self::process_and_save_template_product_meta($post_id, $posted_template_data_services, 'services');
        self::process_and_save_template_product_meta($post_id, $posted_template_data_installation, 'installation');
        self::process_and_save_template_product_meta($post_id, $posted_template_data_software, 'software');

    }

    /**
     * Get extract posted data and save them to meta
     *
     * @param $post_id integer id of the template
     * @param $posted_template_data mixed
     * @param $option string indicating main wiaas category
     */
    public static function process_and_save_template_product_meta($post_id, $posted_template_data, $option) {
        $processed_template_data = array();

        // Sort posted data by menu order.
        usort($posted_template_data, array(__CLASS__, 'menu_sort_order'));

        foreach ($posted_template_data as $data) {

            $template_category_id = isset($data['template_category_id']) ? absint($data['template_category_id']) : false;
            $template_category_title = isset($data['template_category_title']) ? $data['template_category_title'] : false;
            $quantity = isset($data['quantity']) ? absint($data['quantity']) : false;


            $item_data = array(

                'template_category_id' => $template_category_id,
                'template_category_title' => trim($template_category_title),
                'quantity' => $quantity

            );

            $processed_template_data[$template_category_id] = $item_data;

        }

        WC_Product_Template::save_template_product_meta($post_id, $option, $processed_template_data);
    }

    public static function menu_sort_order($a, $b) {
        if (isset($a['menu_order']) && isset($b['menu_order'])) {
            return $a['menu_order'] - $b['menu_order'];
        } else {
            return isset($a['menu_order']) ? 1 : -1;
        }
    }
}

Wiaas_template::init();

