<?php

/**
 * @package CsvImporterPlusforACF
 */

namespace CIPFA\base;

// make sure we don't expose any info if called directly
if (!defined('ABSPATH')) exit; // Exit if accessed directly

class CIPFA_BaseController
{
   //plugin path,url & name
   public static $plugin_path;
   public static $plugin_url;
   public static $plugin_name;

   public function __construct()
   {
      //setting plugin path,url & name
      self::$plugin_path = plugin_dir_path(dirname(__FILE__, 2));
      self::$plugin_url  = plugin_dir_url(dirname(__FILE__, 2));
      self::$plugin_name = plugin_basename(self::$plugin_path . 'csv-importer-plus-for-acf.php');

      //Add Post ID column to all post types
      add_action('admin_init', array(__CLASS__, 'cipfa_register_post_id_column_hooks'));
   }

   // Register hooks for adding Post ID column to ALL post types
   public static function cipfa_register_post_id_column_hooks()
   {
      // Media Library
      add_filter('manage_upload_columns', array(__CLASS__, 'cipfa_add_id_column'));
      add_action('manage_media_custom_column', array(__CLASS__, 'cipfa_show_id_value'), 10, 2);

      // Posts
      add_filter('manage_posts_columns', array(__CLASS__, 'cipfa_add_id_column'));
      add_action('manage_posts_custom_column', array(__CLASS__, 'cipfa_show_id_value'), 10, 2);

      // Pages
      add_filter('manage_pages_columns', array(__CLASS__, 'cipfa_add_id_column'));
      add_action('manage_pages_custom_column', array(__CLASS__, 'cipfa_show_id_value'), 10, 2);
   }

   // Add ID Column
   public static function cipfa_add_id_column($columns)
   {
      global $typenow;

      if ($typenow === 'page') {
         $columns['cipfa_post_id'] = 'Page ID';
      } elseif ($typenow === 'product') {
         $columns['cipfa_post_id'] = 'Product ID';
      } elseif ($typenow === 'attachment') {
         $columns['cipfa_post_id'] = 'Media ID';
      } else {
         $columns['cipfa_post_id'] = 'Post ID';
      }

      return $columns;
   }

   // Display Post ID Column Value
   public static function cipfa_show_id_value($column, $post_id)
   {
      if ($column === 'cipfa_post_id') {
         echo '<span class="cipfa-copy-id" data-id="' . esc_attr($post_id) . '" style="cursor:pointer;" title="Click to copy">' . (int)$post_id . '</span>';
      }
   }

   // Get user created or default post types
   public static function cipfa_get_filtered_post_types()
   {
      // Get all public post types
      $post_types = get_post_types(array('public' => true), 'objects');

      // Filter post types
      $cipfa_filtered_post_types = array_filter($post_types, function ($post_type) {
         return (!$post_type->show_ui ||
            !$post_type->show_in_nav_menus ||
            !$post_type->query_var ||
            (post_type_supports($post_type->name, 'title') &&
               $post_type->capability_type === 'post')
         );
      });

      // Check if WooCommerce is active and include 'product' post type
      if (class_exists('WooCommerce')) {
         $product_post_type = get_post_type_object('product');
         if ($product_post_type) {
            $cipfa_filtered_post_types['product'] = $product_post_type;
         }
      }

      // ACF options pages
      if (function_exists('acf_get_options_pages')) {
         $options_pages = acf_get_options_pages();

         if (!empty($options_pages)) {
            foreach ($options_pages as $slug => $page) {
               $cipfa_filtered_post_types['acf_option_' . $slug] = (object)[
                  'name'  => 'acf_option_' . $slug,
                  'label' => $page['page_title'] . ' (ACF Options)',
                  'is_acf_option' => true,
                  'acf_slug' => $slug
               ];
            }
         }
      }

      return $cipfa_filtered_post_types;
   }

   // Add ID column to all post types
   public function cipfa_add_id_to_custom_post_types()
   {
      $post_types = get_post_types(['public' => true, '_builtin' => false], 'names');

      foreach ($post_types as $post_type) {
         add_filter("manage_{$post_type}_posts_columns", 'cipfa_add_id_column');
         add_action("manage_{$post_type}_posts_custom_column", 'cipfa_show_id_value', 10, 2);
      }
   }

   // Tooltip safe echo
   public static function cipfa_safe_echo_tooltip($content)
   {
      echo wp_kses(
         $content,
         array_merge(
            wp_kses_allowed_html('post'),
            array(
               'svg'  => array(
                  'class'   => true,
                  'xmlns'   => true,
                  'viewBox' => true,
                  'fill'    => true,
               ),
               'path' => array(
                  'fill-rule' => true,
                  'clip-rule' => true,
                  'd'         => true,
                  'fill'      => true,
               ),
            )
         )
      );
   }

   // Tooltip
   public static function cipfa_tooltip($text, $index = null)
   {
      return '<span class="cipfa-tooltip" id="' . esc_attr($index) . '_tooltip">
        <div>
            <svg viewBox="0 0 20 20" fill="#000" xmlns="http://www.w3.org/2000/svg" class="cipfa-circle-question-mark">
                <path fill-rule="evenodd" clip-rule="evenodd" d="M1.6665 10.0001C1.6665 5.40008 5.39984 1.66675 9.99984 1.66675C14.5998 1.66675 18.3332 5.40008 18.3332 10.0001C18.3332 14.6001 14.5998 18.3334 9.99984 18.3334C5.39984 18.3334 1.6665 14.6001 1.6665 10.0001ZM10.8332 13.3334V15.0001H9.1665V13.3334H10.8332ZM9.99984 16.6667C6.32484 16.6667 3.33317 13.6751 3.33317 10.0001C3.33317 6.32508 6.32484 3.33341 9.99984 3.33341C13.6748 3.33341 16.6665 6.32508 16.6665 10.0001C16.6665 13.6751 13.6748 16.6667 9.99984 16.6667ZM6.6665 8.33341C6.6665 6.49175 8.15817 5.00008 9.99984 5.00008C11.8415 5.00008 13.3332 6.49175 13.3332 8.33341C13.3332 9.40251 12.6748 9.97785 12.0338 10.538C11.4257 11.0695 10.8332 11.5873 10.8332 12.5001H9.1665C9.1665 10.9824 9.9516 10.3806 10.6419 9.85148C11.1834 9.43642 11.6665 9.06609 11.6665 8.33341C11.6665 7.41675 10.9165 6.66675 9.99984 6.66675C9.08317 6.66675 8.33317 7.41675 8.33317 8.33341H6.6665Z" fill="currentColor"></path>
            </svg>
            <span class="cipfa-popper">
                <div class="cipfa-popper-arrow"></div>
                ' . wp_kses_post($text) . '
            </span>
        </div>
    </span>';
   }
}
