<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Implements available wiaas countries
 *
 * Class Wiaas_Countries
 */
class Wiaas_Countries {

	/**
	 * Available countries for wiaas
	 * @var array
	 */
	private static $available_countries = array(
		'Sweden' => array(
			'id' => 1,
			'name' => 'Sweden',
			'code' => 'se',
			'vat' => 9 ,
			'currency' => 'SEK'
		),
		'Denmark' => array(
			'id' => 2,
			'name' => 'Denmark',
			'code' => 'dk',
			'vat' => 9 ,
			'currency' => 'DKK'
		),
		'Finland' => array(
			'id' => 3,
			'name' => 'Finland',
			'code' => 'fi',
			'vat' => 9 ,
			'currency' => 'EUR'
		),
	);

	public static function init() {

		add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_product_countries_taxonomy'));
	}

	public static function get_list_of_countries(){
		$result = [];
		foreach(self::$available_countries as $country){
			array_push($result, array(
				'country_id' => $country['id'],
				'country_name' => $country['name']
			));
		}
		return $result;
	}

	public static function get_country_name_by_id($id){
		foreach(self::$available_countries as $country){
			if ($country['id'] == $id) return $country['name'];
		}
		return '';
	}

	/**
	 * Registers product taxonomy for avaiable countries
	 */
	public static function register_product_countries_taxonomy() {

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

		$args = array(
			'hierarchical'      => false,
			'label'                 => __( 'Countries', 'wiaas' ),
			'labels'            => $labels,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array( 'slug' => 'product_country' ),
		);

		register_taxonomy( 'product_country', array( 'product' ), $args );

		foreach (self::$available_countries as $available_country) {
			wp_insert_term($available_country['name'], 'product_country');
		}
	}

	/**
	 * Retrieves country for provided package
	 * @param $package
	 *
	 * @return array|null
	 */
	public static function get_package_country($package) {
		return self::get_product_country($package);
	}

	/**
	 * Retrieves country for provided product
	 * @param $product
	 *
	 * @return array|null
	 */
	public static function   get_product_country($product) {
		$product_country = get_the_terms($product->get_id(), 'product_country');
		return  is_array($product_country) && isset($product_country[0]) ?
			self::$available_countries[$product_country[0]->name] :
			null;
	}

    /**
     * Retrieves country term id from db for provided product
     * @param $product
     *
     * @return int|null
     */
    public static function get_product_country_term_id($product) {
        $product_country = get_the_terms($product->get_id(), 'product_country');
        return  is_array($product_country) && isset($product_country[0]) ?
            $product_country[0]->term_id :
            null;
    }
}

Wiaas_Countries::init();
