<?php

class Wiaas_Admin_Product {

    public static function init() {

        add_action('acf/save_post', array(__CLASS__, 'wiaas_my_save_post'), 20, 1);
        add_action('woocommerce_after_register_post_type', array(__CLASS__, 'wiaas_register_product_status'));
        add_filter('woocommerce_register_post_type_product', array(__CLASS__, 'wiaas_modify_product'));
        add_action('add_meta_boxes', array(__CLASS__, 'wiaas_maybe_remove_metaboxes'), 999);
        add_filter('wp_insert_post_data', array(__CLASS__, 'wiaas_maybe_set_no_country_status'), 999, 2);
        add_action('acf/render_field/type=taxonomy', array(__CLASS__, 'wiaas_render_field'), 10, 1);

    }

    public static function wiaas_modify_product($args) {
        $args['supports'] = array('title');
        return $args;
    }

    public static function wiaas_register_product_status() {
        register_post_status('_wiaas_no_country', array(
            'label' => _x('No Country', 'Product status', 'wiaas'),
            'public' => false,
            'exclude_from_search' => false,
            'show_in_admin_all_list' => false,
            'show_in_admin_status_list' => false,
        ));
    }


    public static function wiaas_render_field($field) {

        if ($field['_name'] === '_wiaas_product_country') {
            ?>
            <div id="submitpost" style="padding: 20px;">
                <input style="float:right;" name="save" id="save-post" class="button" type="submit" value="Choose"/>
            </div>
            <?php
        }

    }


    public static function wiaas_maybe_remove_metaboxes() {
        $screen = get_current_screen();
        $screen_id = $screen ? $screen->id : '';

        if ($screen_id !== 'product') {
            return;
        }
        global $post;

        $post_id = $post->ID;

        if ($post_id === 0 ||
            ($post->post_status !== 'publish' && !(Wiaas_Countries::get_package_country(wc_get_product($post_id))))) {

            remove_meta_box('woocommerce-product-data', 'product', 'normal');
            remove_meta_box('submitdiv', 'product', 'side');
            remove_meta_box('slugdiv', 'product', 'normal');
            remove_meta_box('wiaas_upload_and_link_document', 'product', 'normal');
            remove_meta_box('postexcerpt', 'product', 'normal');
            remove_meta_box('template_product_meta_box', 'product', 'normal');
            remove_meta_box('postimagediv', 'product', 'normal');
            remove_meta_box('woocommerce-product-images', 'product', 'normal');
            remove_meta_box('wc-jetpack-product_by_user_role', 'product', 'normal');


            remove_meta_box('groups-permissions', 'product', 'side');
            remove_meta_box('tagsdiv-product_tag', 'product', 'side');
            remove_meta_box('tagsdiv-template_category', 'product', 'side');
            remove_meta_box('tagsdiv-supplier', 'product', 'side');
            remove_meta_box('postimagediv', 'product', 'side');
            remove_meta_box('woocommerce-product-images', 'product', 'side');
            remove_meta_box('submitdiv', 'product', 'side');
            remove_meta_box('wiaas_upload_and_link_document', 'product', 'side');
            remove_meta_box('radio-tagsdiv-product_country', 'product', 'side');
            remove_meta_box('tagsdiv-_wiaas_shop_prices', 'product', 'side');
            remove_meta_box('tagsdiv-wiaas_units', 'product', 'side');
        }

        //Always hide product category, it is added wit advanced custom fields plugin for simple product
        remove_meta_box('radio-product_catdiv', 'product', 'side');

    }

    public static function wiaas_maybe_set_no_country_status($data, $postarr) {
        if ($postarr['post_type'] === 'product' && (!isset($postarr['ID']) || !$postarr['ID'])) {
            $data['post_status'] = '_wiaas_no_country';
        }
        return $data;
    }

    /**
     * Check for post status and and if there is country available
     * Set post status to draft if the country is added
     *
     * @param $post_id
     */

    public static function wiaas_my_save_post($post_id) {

        global $post;
        $status = get_post_status( $post->ID);

        $value = get_field('_wiaas_product_country', $post_id, true);
        $type = get_field('_wiaas_product_type', $post_id, true);

        error_log($status);
        error_log($value);


        if (!empty($value) && $status === '_wiaas_no_country' ) {

            wp_set_object_terms($post_id, $value, 'product_country', true);
            wp_set_object_terms($post_id, $type, 'product_type', true);

            wp_update_post(array(
                'ID' => $post_id,
                'post_status' => 'draft'
            ));

        }
    }

}

Wiaas_Admin_Product::init();