<?php

namespace PWH_DCFH\App\Helpers;

use WP_User;

if (!defined('ABSPATH')) {
    exit;
}
/**
 * WordPress Plugin Administration API
 * get_plugin_data() is only available on dashboard; Manually require it needed.
 */
if (file_exists(ABSPATH.'wp-admin/includes/plugin.php')):
    require_once ABSPATH.'wp-admin/includes/plugin.php';
endif;
if (!class_exists('PWH_DCFH_WP_Helpers')) {
    class PWH_DCFH_WP_Helpers
    {
        private static $_instance;

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Get Plugin DATA
         * @return object|null
         */
        public static function plugin()
        {
            $plugin_info = get_plugin_data(PWH_DCFH_PLUGIN_FILE, false, false);

            return (object)$plugin_info;
        }

        /**
         * Get Plugin DIR
         *
         * @param string $path
         *
         * @return string
         */
        public static function plugin_dir($path = '')
        {
            return plugin_dir_path(PWH_DCFH_PLUGIN_FILE).$path;
        }

        /**
         * Get Plugin URL
         *
         * @param string $path
         *
         * @return string
         */
        public static function plugin_url($path = '')
        {
            return plugins_url($path, PWH_DCFH_PLUGIN_FILE);
        }

        /**
         * Check Divi 4.13.1
         * @return bool
         */
        public static function is_divi_4131_or_above()
        {
            $theme = wp_get_theme();
            if (!$theme) {
                return false;
            }
            $version = $theme->parent() ? $theme->parent()->get('Version') : $theme->get('Version');
            if (version_compare($version, '4.13.1', '>=')) {
                return true;
            }

            return false;
        }

        /**
         * Get Divi Setting Page Slug
         * @return string
         */
        public static function get_divi_setting_slug()
        {
            $slug = 'et_divi_options';
            if (is_plugin_active('divi-ghoster/divi-ghoster.php')) {
                $options = get_option('agsdg_settings');
                $slug = 'et_'.$options['theme_slug'].'_options';
            }

            return $slug;
        }

        /**
         * Get The Author Name
         *
         * @param null $user_id
         *
         * @return string
         */
        public static function get_author_name($user_id = null)
        {
            $user_info = $user_id ? new WP_User($user_id) : wp_get_current_user();
            if (!($user_info instanceof WP_User)) {
                return 'Unknown Author';
            }
            $author_name = '';
            if (!empty($user_info->first_name)) {
                $author_name .= $user_info->first_name;
            }
            if (!empty($user_info->last_name)) {

                if ($author_name) {
                    $author_name .= ' ';
                }
                $author_name .= $user_info->last_name;
            }
            if (empty($author_name)) {
                $author_name = $user_info->display_name;
            }

            return $author_name;
        }

        /**
         * Get Author Name
         *
         * @param $post_id
         *
         * @return string
         */
        public static function get_submitter_name($post_id)
        {
            $post_author = __('Visitor', 'pwh-dcfh');
            if ($post_id > 0) {
                $user_data = get_user_by('ID', $post_id);
            } else {
                $user_data = pwh_dcfh_post_meta_handler()::get_contact_email_meta_value($post_id);
            }
            if (isset($user_data->ID)) {
                $post_author = self::get_author_name($user_data->ID);
            }

            return $post_author;
        }

        /**
         * Get WP Pages List
         * @return array
         */
        public static function get_pages()
        {
            $pages_list = wp_list_pluck(get_posts(['post_type' => 'page', 'numberposts' => -1]), 'post_title', 'ID');
            $pages = array_replace([__('Please Select A Page', 'pwh-dcfh')], $pages_list);
            if (!empty($pages)) {
                return $pages;
            }

            return [];
        }

        /**
         * WP Post Types
         * @return string[]
         */
        public static function get_post_types()
        {
            $posts_types = get_post_types(['public' => true]);
            unset($posts_types['attachment']);

            return $posts_types;
        }

        /**
         * Get Datetime
         *
         * @param $datetime
         *
         * @return string
         */
        public static function datetime($datetime)
        {
            $datetime = strtotime($datetime);
            $date = date_i18n(et_get_option('pwh_dcfh_entries_date_format', 'Y/m/d'), $datetime);
            $time = date_i18n(et_get_option('pwh_dcfh_entries_time_format', 'h:i:sa'), $datetime);

            return sprintf(__('%1$s at %2$s', 'pwh-dcfh'), $date, $time);
        }

        /**
         * Images Mime Types
         * @return string[]
         */
        public static function get_image_mimes()
        {
            $mimes = wp_get_mime_types();
            $image_mimes = [];
            foreach ($mimes as $mime) {
                if (strpos($mime, 'image/') !== false) {
                    $image_mimes[] = $mime;
                }
            }

            return $image_mimes;
        }

        /**
         * Get WordPress Default Cron Schedules
         * @return array
         */
        public static function get_get_schedules()
        {
            $schedules = [];
            $labels = [
                'hourly' => __('Every Hour', 'pwh-dcfh'),
                'twicedaily' => __('Every 12 Hours', 'pwh-dcfh'),
                'daily' => __('Every Day', 'pwh-dcfh'),
                'weekly' => __('Every Week', 'pwh-dcfh'),
                'monthly' => __('Every Month', 'pwh-dcfh'),
            ];
            $wp_schedules = wp_get_schedules();
            // Sort Schedules With Intervals DESC
            foreach ($wp_schedules as $k => $va) {
                $sort_schedules[$k] = $va['interval'];
            }
            array_multisort($sort_schedules, SORT_DESC, $wp_schedules);
            // Get Required Data Fron Schedules
            foreach ($wp_schedules as $k => $v) {
                if (in_array($k, ['monthly', 'hourly', 'twicedaily', 'daily', 'weekly'], true)) {
                    $schedules[$k] = $labels[$k] ?? $v['display'];
                }
            }

            return $schedules;
        }

        /**
         * Get WordPress Locale
         * @return string
         */
        public static function get_wp_locale()
        {
            $locale_arr = explode('_', get_locale());

            return $locale_arr[0] ?? 'en';
        }

        /**
         * Get Allowed Mimes Types
         * @return array
         */
        public static function get_wp_allowed_mime_types()
        {
            $allowed_mime_type = [];
            foreach (get_allowed_mime_types() as $key => $value) {
                if ('css' === $key) {
                    $allowed_mime_type[$key] = $value;
                    $allowed_mime_type['htm|html'] = 'text/html';
                } elseif ('rtf' === $key) {
                    $allowed_mime_type[$key] = $value;
                    $allowed_mime_type['js'] = 'application/javascript';
                } else {
                    $allowed_mime_type[$key] = $value;
                }
            }

            return $allowed_mime_type;
        }

    }
}