<?php

namespace PWH_DCFH\App\Admin\Handlers;

if (!class_exists('PWH_DCFH_Email_Template_Handler')) {
    class PWH_DCFH_Email_Template_Handler
    {
        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 Email Template Name
         *
         * @param $string
         *
         * @return string
         */
        public static function set_template_id($string)
        {
            return '_pwh_dcfh_email_tpl_'.sanitize_key($string);
        }

        /**
         * Check Option Exist In Database
         *
         * @param $option_name
         *
         * @return bool
         */
        public static function is_template_exist($option_name)
        {
            global $wpdb;
            $result = (int)$wpdb->get_var($wpdb->prepare("SELECT COUNT(1) FROM $wpdb->options WHERE option_name LIKE %s ", $option_name)); // db call ok; no-cache ok

            return $result > 0 && !is_wp_error($result);
        }

        /**
         * Get Email Templates
         * @return array|false|mixed
         */
        public static function get_templates()
        {
            $key = 'pwh_dcfh_templates_'.md5(wp_basename(__FILE__));
            $cached_data = wp_cache_get($key);
            if (!$cached_data) {
                global $wpdb;
                $templates = $wpdb->get_col($wpdb->prepare("SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ", '%'.'_pwh_dcfh_email_tpl_'.'%'));//db call ok no-cache ok
                if (!empty($templates)) {
                    foreach ($templates as $template) {
                        $options = get_option($template);
                        if (false !== $options) {
                            $cached_data[$template] = $options;
                        }
                    }
                    // Sort Date Wise
                    array_multisort(array_map('strtotime', array_column($cached_data, 'created_at')), SORT_DESC, $cached_data);
                    wp_cache_set($key, $cached_data, 'pwh_dcfh_cached_queries', 5 * MINUTE_IN_SECONDS);
                }
            }

            return $cached_data;
        }

        /**
         * Get Email Templates Type
         * @return array
         */
        public static function get_templates_types()
        {
            return [
                'general' => __('General', 'pwh-dcfh'),
                'admin' => __('Admin', 'pwh-dcfh'),
                'confirmation' => __('Confirmation', 'pwh-dcfh'),
                'reply_send' => __('Reply/Send Email', 'pwh-dcfh'),
                'post_clone' => __('Post Clone', 'pwh-dcfh'),
            ];
        }

        /**
         * Get Email Template Type Label
         *
         * @param $label
         *
         * @return string
         */
        public static function get_template_type_label($label)
        {
            $templates = self::get_templates_types();
            if ('' !== $label && isset($templates[$label])) {
                return $templates[$label];
            }

            return $label;
        }
    }
}