<?php

defined('ABSPATH') || exit;

class Wiaas_Product_Supplier {
    public static function init() {

        add_action('init', array(__CLASS__, 'register_supplier_taxonomy'));
        add_action('wiaas_organization_created', array(__CLASS__, 'on_organization_added'), 20, 2);
        add_action('wiaas_organization_roles_updated', array(__CLASS__, 'on_organization_added'), 20, 2);
    }

    /**
     * Register wiaas template category taxonomy
     */
    public static function register_supplier_taxonomy() {

        $labels = array(
            'name' => _x('Supplier', 'taxonomy general name', 'wiaas'),
            'singular_name' => _x('Supplier', 'taxonomy singular name', 'wiaas'),
            'search_items' => __('Search Suppliers', 'wiaas'),
            'all_items' => __('All Suppliers', 'wiaas'),
            'parent_item' => __('Parent Supplier', 'wiaas'),
            'parent_item_colon' => __('Parent Supplier', 'wiaas'),
            'edit_item' => __('Edit Supplier', 'wiaas'),
            'update_item' => __('Update Supplier', 'wiaas'),
            'add_new_item' => __('Add New Supplier', 'wiaas'),
            'new_item_name' => __('New Supplier Name', 'wiaas'),
            'menu_name' => __('Supplier', 'wiaas'),
        );

        $args = array(
            'hierarchical' => false,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'meta_box_cb' => false,
            'query_var' => true,
            'rewrite' => array('slug' => 'template_category'),
        );

        register_taxonomy('supplier', array('product'), $args);
    }


    /**
     * Create corresponding supplier term for organization
     *
     * @param $organization_id
     * @param $roles
     */
    public static function on_organization_added($organization_id, $roles) {

        $organization = get_term_by('id', $organization_id, 'wiaas-user-organization');

        $supplier = term_exists($organization->slug, 'supplier');

        if (!$supplier && in_array('supplier', $roles)) {
            $supplier = wp_insert_term($organization->name, 'supplier', array(
                'slug' => $organization->slug
            ));
        }

        add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
    }

    /**
     * Retrieve organisation id of the supplier of the product
     *
     * @param $product_id
     * @return int|false organization_id
     */
    public static function get_supplier_organisation_id_from_product($product_id) {

        $supplier_terms = wp_get_object_terms($product_id, 'supplier');
        if (empty( $supplier_terms )) {

        	return false;
        }

        $supplier_organisation_slug = $supplier_terms[0]->slug;
        $supplier_organisation = get_term_by('slug', $supplier_organisation_slug, 'wiaas-user-organization');

        if ($supplier_organisation) {
        	return $supplier_organisation->term_id;
        }

	    return $supplier_organisation ? $supplier_organisation->term_id : false;
    }
}

Wiaas_Product_Supplier::init();