<?php

namespace PWH_DCFH\App\Admin\Migration;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Options_Migration')) {
    class PWH_DCFH_Options_Migration
    {
        private static $_instance;

        private static $divi_option_key = 'et_divi';

        private static $options = [];

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }
            self::$options = [
                'pwh_dcfh_stats_enabled' => 'off',
                'pwh_dcfh_enable_sent_email_log' => 'off',
                'pwh_dcfh_enable_clone_log' => 'off',
                'pwh_dcfh_delete_plugin_data' => 'off',
                'pwh_dcfh_enable_unique_cookie' => 'off',
                'pwh_dcfh_custom_mimes_types' => 'psd|image/vnd.adobe.photoshop,rar|application/x-rar',
                'pwh_dcfh_entry_list_columns' => 'name',
                'pwh_dcfh_entries_date_format' => 'Y/m/d',
                'pwh_dcfh_entries_time_format' => 'h:i:sa',
                'pwh_dcfh_mergetag_date_format' => 'Y/m/d',
                'pwh_dcfh_mergetag_time_format' => 'h:i:sa',
                'pwh_dcfh_smtp_enabled' => 'off',
                'pwh_dcfh_smtp_host' => '',
                'pwh_dcfh_smtp_encryption' => 'none',
                'pwh_dcfh_smtp_port' => '25',
                'pwh_dcfh_smtp_autotls' => 'off',
                'pwh_dcfh_smtp_authentication' => '',
                'pwh_dcfh_smtp_username' => '',
                'pwh_dcfh_smtp_password' => '',
                'pwh_dcfh_smtp_from_email' => '',
                'pwh_dcfh_smtp_from_name' => '',
                'pwh_dcfh_backup_enabled' => 'off',
                'pwh_dcfh_backup_email' => get_bloginfo('admin_email'),
                'pwh_dcfh_backup_schedule' => 'weekly',
                'pwh_dcfh_delete_entries_enabled' => 'of',
                'pwh_dcfh_delete_entries_status' => ['published' => 'on', 'trash' => 'on'],
                'pwh_dcfh_delete_entries_schedule' => '',
                'pwh_dcfh_file_upload_btn_text' => 'Choose Files',
                'pwh_dcfh_accepted_file_text' => 'Accepted file types:',
                'pwh_dcfh_max_filesize_text' => 'Max. file size:',
                'pwh_dcfh_chosen_file_text' => 'No Files Chosen',
                'pwh_dcfh_selected_files_text' => '{chosen_files_count} file{_s_} selected',
                'pwh_dcfh_uploading_text' => '{percentage} uploaded your files, please wait for the system to continue.',
                'pwh_dcfh_already_attached_text' => 'File {filename} has already attached. Please choose another file.',
                'pwh_dcfh_allow_filesize_text' => 'File {filename} not uploaded. Maximum file size {allowed_filesize}.',
                'pwh_dcfh_x_files_allow_text' => 'Only {allowed_files} files are allowed to upload.',
                'pwh_dcfh_remove_file_text' => 'Are you sure you want to remove {filename}?',
                'pwh_dcfh_delete_file_icon_hover_text' => 'Delete',
                'pwh_dcfh_security_reason_text' => 'File {filename} has failed to upload. Sorry, this file type is not permitted for security reasons.',
                'pwh_dcfh_allowedfilesize_text' => 'File {filename} not uploaded. Maximum file size {filesize}',
                'pwh_dcfh_fill_message_text' => 'Please fill in the following fields:',
                'pwh_dcfh_fix_message_text' => 'Please fix the following errors:',
                'pwh_dcfh_siganture_empty_text' => 'The signature pad is empty. Please sign it and then save.',
                'pwh_dcfh_siganture_before_save_text' => 'Are you sure you want to save the signature?',
                'pwh_dcfh_siganture_save_text' => 'The signature has been saved successfully!',
                'pwh_dcfh_siganture_delete_text' => 'The signature has been deleted successfully!',
            ];

            return self::$_instance;
        }

        /**
         * Add Default Options To Database
         */
        public function add_options()
        {
            $old_options = get_option(self::$divi_option_key);
            foreach (self::$options as $key => $option) {
                if (!isset($old_options[$key])) {
                    et_update_option($key, $option);
                }
            }
        }

        /**
         * Delete Options From Database
         */
        public function delete_options($delete = false)
        {
            if ($delete) {
                $old_options = get_option(self::$divi_option_key);
                foreach (self::$options as $key => $option) {
                    if (isset($old_options[$key])) {
                        unset($old_options[$key]);
                    }
                }
                update_option(self::$divi_option_key, $old_options);
            }
        }

        /**
         * Delete Registered Cron Jobs
         */
        public function delete_crons()
        {
            $crons = [
                'pwh_dcfh_entries_auto_backup_daily',
                'pwh_dcfh_entries_auto_backup_hourly',
                'pwh_dcfh_entries_auto_backup_twicedaily',
                'pwh_dcfh_entries_auto_backup_weekly',
                'pwh_dcfh_entries_auto_backup_monthly',
                'pwh_dcfh_delete_tmp_files',
            ];
            foreach ($crons as $cron) {
                if (wp_next_scheduled($cron)) {
                    wp_clear_scheduled_hook($cron);
                }
            }
        }

    }
}