<?php

/**
 * Manages options for ArchiveMaster
 *
 * @since 1.3.8
 * @package ArchiveMaster
 */

// Namespace.
namespace ARCHM\Admin;

// Exit if accessed directly.
defined('ABSPATH') || exit(1);

if ( ! class_exists(__NAMESPACE__ . '\SettingOption') ) {

    /**
     * Manages options for Easy Video Reviews
     */
    class SettingOption {


        /**
         * Singleton instance
         *
         * @var self
         */
        private static $instance;

        /**
         * Returns the singleton instance
         *
         * @return self
         */
        public static function get_instance() {
            if ( ! self::$instance ) {
                self::$instance = new self();
            }

            return self::$instance;
        }

        /**
         * Prefix for all options
         *
         * @var string
         */
        public $prefix = 'archm_';

        /**
         * Get default options
         *
         * @return array
         */
        public function get_defaults() {
            // Option Key pairs.
            $options = [
                'selected_post_type'     => [
                    'shop_order_placehold',
                ],
                'post_type'              => [ 'shop_order_placehold' ],
                'archive_range'          => 'default',
                'archive_range_datetime' => '',
                'selected_database'      => 'sqlite',
                'order_export_option'    => [
                    'order_start_date' => '',
                    'order_end_date'   => '',
                    'export_file_name' => 'orders-%y-%m-%d-%h-%s',
                    'sort_order'       => 'DESC',
                    'export_format'    => 'csv',
                ],
                'archive_statuses'       => [],
                'exclude_subscriptions'  => false,
                'exclude_memberships'    => false,
                'accoutn_page_archived_oder' => [
                    'enable_feature'     => false,
                    'custom_menu_label'  => 'Archived Orders',
                    'enable_search'      => true,
                    'enable_filter'      => false,
                ],
            ];

            return apply_filters('archm_options', $options);
        }

        /**
         * Get all options
         *
         * @return array
         */
        public function get_all() {
            $defaults = $this->get_defaults();
            if ( function_exists('master_archive_pro') ) {
                $is_pro = master_archive_pro()->license_status;
                if ( $is_pro ) {
                    $defaults = array_merge($defaults, master_archive_pro()->get_pro_options());
                }
            }
            $options = [];
            foreach ( $defaults as $key => $value ) {
                $options[ $key ] = $this->get($key, $value);
            }

            return $options;
        }

        /**
         * Get option
         *
         * @param string $key Option key.
         * @param mixed  $default Default value.
         *
         * @return mixed
         */
        public function get( $key, $default = null ) {
            $option = get_option($this->prefix . $key, $default);

            return maybe_unserialize($option);
        }

        /**
         * Update option
         *
         * @param string $key Option key.
         * @param mixed  $value Option value. Default is null.
         *
         * @return bool
         */
        public function update( $key, $value = null ) {

            if ( is_null($value) ) {
                $defaults = $this->get_defaults();
                $value    = $defaults[ $key ];
            }

            $option = update_option($this->prefix . $key, $value);

            return $option;
        }

        /**
         * Delete option
         *
         * @param string $key Option key.
         *
         * @return bool
         */
        public function delete( $key ) {
            $option = delete_option($this->prefix . $key);

            return $option;
        }

        /**
         * Reset options
         *
         * @return bool
         */
        public function reset() {
            $defaults = $this->get_defaults();

            foreach ( $defaults as $key => $value ) {
                $this->update($key, $value);
            }

            return true;
        }

        /**
         * Get transient
         *
         * @param string $key Transient key.
         * @param mixed  $default Default value.
         * @return mixed
         */
        public function get_transient( $key, $default = null ) {
            $transient = get_transient($this->prefix . $key);

            if ( false === $transient ) {
                $transient = $default;
            }

            return $transient;
        }

        /**
         * Set transient
         *
         * @param string $key Transient key.
         * @param mixed  $value Transient value.
         * @param int    $expiration Expiration time in seconds. Default is 0.
         * @return bool
         */
        public function set_transient( $key, $value, $expiration = 0 ) {
            $transient = set_transient($this->prefix . $key, $value, $expiration);

            return $transient;
        }

        /**
         * Delete transient
         *
         * @param string $key Transient key.
         * @return bool
         */
        public function delete_transient( $key ) {
            $transient = delete_transient($this->prefix . $key);

            return $transient;
        }

        /**
         * Get first array key
         *
         * @param array $array
         * @return string
         */
        public function array_first_key( array $array ) {
            foreach ( $array as $key => $value ) {
                return $key;
            }
        }
    }
}

SettingOption::get_instance();
