<?php

namespace PWH_DCFH\App\Helpers;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Module_Helpers')) {
    class PWH_DCFH_Module_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 Shadow Values
         *
         * @param $preset
         *
         * @return string[]|string[][]
         */
        public static function get_shadow_presets_values($preset = null)
        {
            $presets = [
                'none' => [
                    'horizontal' => '',
                    'vertical' => '',
                    'blur' => '',
                    'spread' => '',
                    'position' => '',
                ],
                'preset1' => [
                    'horizontal' => '0px',
                    'vertical' => '2px',
                    'blur' => '18px',
                    'spread' => '0px',
                    'position' => '',
                ],
                'preset2' => [
                    'horizontal' => '6px',
                    'vertical' => '6px',
                    'blur' => '18px',
                    'spread' => '0px',
                    'position' => '',
                ],
                'preset3' => [
                    'horizontal' => '0px',
                    'vertical' => '12px',
                    'blur' => '18px',
                    'spread' => '-6px',
                    'position' => '',
                ],
                'preset4' => [
                    'horizontal' => '10px',
                    'vertical' => '10px',
                    'blur' => '0px',
                    'spread' => '0px',
                    'position' => '',
                ],
                'preset5' => [
                    'horizontal' => '0px',
                    'vertical' => '6px',
                    'blur' => '0px',
                    'spread' => '10px',
                    'position' => '',
                ],
                'preset6' => [
                    'horizontal' => '0px',
                    'vertical' => '0px',
                    'blur' => '18px',
                    'spread' => '0px',
                    'position' => 'inset',
                ],
                'preset7' => [
                    'horizontal' => '10px',
                    'vertical' => '10px',
                    'blur' => '0px',
                    'spread' => '0px',
                    'position' => 'inset',
                ],
            ];
            if (!empty($preset) && isset($presets[$preset])) {
                return $presets[$preset];
            }

            return $presets;
        }

        /**
         * Get Shadow Options
         *
         * @param $prefix
         *
         * @return array
         */
        public static function get_shadow_presets($prefix)
        {
            $options = [];
            foreach (self::get_shadow_presets_values() as $key => $value) {
                if ('none' === $key) {
                    $options[] = [
                        'value' => $key,
                        'icon' => $key,
                        'fields' => [
                            $prefix.'_shadow_horizontal' => '0',
                            $prefix.'_shadow_vertical' => '0',
                            $prefix.'_shadow_blur' => '0',
                            $prefix.'_shadow_spread' => '0',
                            $prefix.'_shadow_position' => '',
                        ]
                    ];
                } else {
                    $options[] = [
                        'value' => $key,
                        'content' => sprintf('<span class="preset %1$s"></span>', esc_attr($key)),
                        'fields' => [
                            $prefix.'_shadow_horizontal' => $value["horizontal"],
                            $prefix.'_shadow_vertical' => $value["vertical"],
                            $prefix.'_shadow_blur' => $value["blur"],
                            $prefix.'_shadow_spread' => $value["spread"],
                            $prefix.'_shadow_position' => $value["position"],
                        ]
                    ];
                }
            }

            return $options;
        }

        /**
         * @param $props
         * @param int|string $top
         * @param int|string $right
         * @param int|string $bottom
         * @param int|string $left
         *
         * @return string[]
         */
        public static function get_responsive_margin_padding_border_props($props, $top = '0', $right = '0', $bottom = '0', $left = '0')
        {
            $responsive_props = ['desktop' => '', 'tablet' => '', 'phone' => ''];
            $default_values = [0 => $top, 1 => $right, 2 => $bottom, 3 => $left];
            foreach ($props as $device => $prop) {
                if (!empty($prop)) {
                    $prop_array = explode('|', $prop);
                    foreach ($prop_array as $key => $value) {
                        if (!in_array($value, ['true', 'false', 'on', 'off'])) {
                            $prop_array[$key] = !empty($value) ? $value : ($default_values[$key] ?? 0);
                        } else {
                            unset($prop_array[$key]);
                        }
                    }
                    $responsive_props[$device] = implode(' ', $prop_array);
                } else {
                    $responsive_props[$device] = '';
                }
            }

            return $responsive_props;
        }

        /**
         * Build Devicss CSS Props
         *
         * @param $props
         * @param $slug
         * @param string $default
         * @param bool $copy
         *
         * @return array|string|string[]
         */
        public static function get_responsive_props($props, $slug, $default = '', $copy = false)
        {
            $responsive_prop = [];
            $responsive_enabled = isset($props["{$slug}_last_edited"]) && et_pb_get_responsive_status($props["{$slug}_last_edited"]);
            if (!isset($props[$slug]) || '' === $props[$slug]) {
                $responsive_prop['desktop'] = $default;
            } else {
                $responsive_prop['desktop'] = $props[$slug];
            }
            if (!$responsive_enabled || !isset($props["{$slug}_tablet"]) || '' === $props["{$slug}_tablet"]) {
                $responsive_prop['tablet'] = $copy ? $responsive_prop['desktop'] : $default;
            } else {
                $responsive_prop['tablet'] = $props["{$slug}_tablet"];
            }
            if (!$responsive_enabled || !isset($props["{$slug}_phone"]) || '' === $props["{$slug}_phone"]) {
                $responsive_prop['phone'] = $copy ? $responsive_prop['tablet'] : $default;
            } else {
                $responsive_prop['phone'] = $props["{$slug}_phone"];
            }

            return str_replace(['|', 'on|', 'off|'], '', $responsive_prop);
        }

        /**
         * Process Multiple Mimes Checkbox
         *
         * @param $data
         *
         * @return array
         */
        public static function process_multiple_mimes_checkboxes_value($data)
        {
            $allowed_mime_types = pwh_dcfh_wp_helpers()::get_wp_allowed_mime_types();
            $mimes = array_keys($allowed_mime_types);
            $mime_values = [];
            $mime_keys = [];
            $data = explode('|', $data);
            foreach ($data as $key => $val) {
                if (isset($mimes[$key], $allowed_mime_types[$mimes[$key]]) && 'on' === strtolower($val)) {
                    // array_push($mime_values, ' '.str_replace("|", ', ', $mimes[$key]));
                    // array_push($mime_keys, $allowed_mime_types[$mimes[$key]]);
                    $mime_values[] = ' '.str_replace("|", ', ', $mimes[$key]);
                    $mime_keys[] = $allowed_mime_types[$mimes[$key]];
                }
            }

            return ['keys' => implode(',', $mime_keys), 'values' => implode(',', $mime_values)];
        }

        /**
         * Process Multiple Checkboxs
         *
         * @param $checkboxes
         * @param $array_to_match
         *
         * @return string
         */
        public static function process_multiple_checkboxes_value($checkboxes, $array_to_match)
        {
            $processed_data = [];
            $checkboxes = explode('|', $checkboxes);
            foreach ($checkboxes as $key => $val) {
                if (isset($array_to_match[$key]) && 'on' === strtolower($val)) {
                    $processed_data[] = $array_to_match[$key];
                }
            }

            return implode(',', $processed_data);
        }

        /**
         * Get WordPress Allowed Mimes Keys with.
         * @return int[]|string[]
         */
        public static function get_mimes_with_ext()
        {
            $allowed_mime_types = pwh_dcfh_wp_helpers()::get_wp_allowed_mime_types();
            $mimes = array_combine(
                array_map(static function ($key) {
                    return '.'.$key;
                }, array_keys($allowed_mime_types)),
                $allowed_mime_types
            );

            return array_keys($mimes);
        }

        /**
         * Get Week Day Names
         * @return array
         */
        public static function get_week_day_names()
        {
            return [
                0 => __('Sunday', 'pwh-dcfh'),
                1 => __('Monday', 'pwh-dcfh'),
                2 => __('Tuesday', 'pwh-dcfh'),
                3 => __('Wednesday', 'pwh-dcfh'),
                4 => __('Thursday', 'pwh-dcfh'),
                5 => __('Friday', 'pwh-dcfh'),
                6 => __('Saturday', 'pwh-dcfh'),
            ];
        }

        /**
         * Get Flex Box Aligment
         *
         * @param array $alignment
         *
         * @return array|string[]
         */
        public static function get_flex_alignment($alignment)
        {
            $default = '';

            return array_map(
                static function ($v) use (&$default) {
                    if (empty($v)) {
                        $v = $default;
                    }
                    $default = $v;
                    if ($v === 'center') {
                        $v = 'center';
                    } elseif ($v === 'right') {
                        $v = 'flex-end';
                    } else {
                        $v = 'flex-start';
                    }

                    return $v;
                },
                $alignment
            );
        }

        /**
         * Get Flex Box Column
         *
         * @param array $column
         *
         * @return array|string[]
         */
        public static function get_flex_column($column)
        {
            return array_map(static function ($v) {
                $v = (int)$v;
                switch ($v) {
                    case 2:
                        return '50%';
                    case 3:
                        return '33%';
                    case 4:
                        return '25%';
                    default:
                        return '100%';
                }
            }, $column);
        }

        /**
         * Get Server Max Files Allowed
         * @return false|string
         */
        public static function max_number_files_allowed()
        {
            return ini_get('max_file_uploads');
        }

        /**
         * Convert Divi Justied To Justify
         *
         * @param $array
         *
         * @return array|string[]
         */
        public static function justified_to_justify($array)
        {
            return array_map(static function ($value) {
                if ('justified' === strtolower($value)) {
                    return 'justify';
                }

                return $value;
            }, $array);
        }

        /**
         * Get Colors
         *
         * @param $devices
         *
         * @return array
         */
        public static function get_global_color($devices)
        {
            $global_colors = [];
            foreach ($devices as $device => $value) {
                if (!empty($value)) {
                    if (strpos($value, 'gcid-') !== false) {
                        $global_color_info = et_builder_get_global_color_info($value);
                        $global_colors[$device] = esc_attr($global_color_info['color']);
                    } else {
                        $global_colors[$device] = $value;
                    }
                }

            }

            return $global_colors;
        }

    }
}