<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

require_once 'class.reon.util.php';
require_once 'class.reon.core.api.php';
require_once 'class.reon.core.ajax.php';
require_once 'class.reon.core.ui.php';
require_once 'class.reon.core.ui.optionpage.php';
require_once 'class.reon.core.ui.optionpage-wc.php';
require_once 'class.reon.core.ui.metabox.php';
require_once 'class.reon.core.ui.metabox-wc.php';

if ( !class_exists( 'Reon_Core' ) ) {

    class Reon_Core {

        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public static function init() {

            do_action( 'reon_before_init' );

            Reon_UI::init();
            Reon_Ajax::init();
            Reon_Option_Page::init();
            Reon_Option_Page_WC::init();
            Reon_Meta_Box::init();
            Reon_Meta_Box_WC::init();

            do_action( 'reon_init' );
        }

        public static function load_options( $option_args, $default = array() ) {

            $suffix = $option_args[ 'instance_id' ];

            if ( $option_args[ 'instance_id' ] != '' & $option_args[ 'database' ] != 'custom_db' ) {

                $suffix = '_' . $option_args[ 'instance_id' ];
            }

            $opt = $default;

            if ( $option_args[ 'database' ] == 'custom_db' ) {

                $opt = apply_filters( 'reon_get-' . $option_args[ 'option_name' ] . '-options', stripslashes_deep( $opt ), $option_args[ 'instance_id' ] );
            } else if ( $option_args[ 'database' ] == 'theme_mod' ) {

                $opt = get_theme_mod( $option_args[ 'option_name' ] . $suffix, $default );
            } else {

                $opt = get_option( $option_args[ 'option_name' ] . $suffix, $default );
            }

            if ( !is_array( $opt ) ) {

                $opt = array();
            }

            return apply_filters( 'reon_process-get-options-' . $option_args[ 'option_name' ], stripslashes_deep( $opt ), $option_args[ 'instance_id' ] );
        }

        public static function save_options( $option_args, $option = array(), $autoload = false, $field_ids = array() ) {

            $opt = self::load_options( $option_args );

            foreach ( $field_ids as $value ) {

                if ( isset( $opt[ $value ] ) ) {

                    unset( $opt[ $value ] );
                }
            }

            foreach ( $option as $key => $value ) {

                $opt[ $key ] = $value;
            }

            self::save_options_data( $opt, $option_args, $autoload );
        }

        public static function reset_options( $option_args, $field_ids = array() ) {
            $option = self::load_options( $option_args );

            $opt = array();

            foreach ( $option as $field_key => $field_value ) {
                $found = false;
                foreach ( $field_ids as $value ) {
                    if ( $field_key == $value ) {
                        $found = true;
                    }
                }
                if ( $found == true ) {
                    continue;
                }
                $opt[ $field_key ] = $field_value;
            }

            self::save_options_data( $opt, $option_args, false, true );
        }

        private static function save_options_data( $options, $option_args, $autoload, $is_reset = false ) {

            $opt = apply_filters( 'reon_process-save-options-' . $option_args[ 'option_name' ], $options, $is_reset, $autoload, $option_args[ 'instance_id' ] );

            $reon_option_key = 'reon_option';

            if ( isset( $option_args[ 'option_name' ] ) && !empty( $option_args[ 'option_name' ] ) ) {

                $reon_option_key = $option_args[ 'option_name' ];
            }

            if ( !empty( $option_args[ 'instance_id' ] ) && 'custom_db' !== $option_args[ 'database' ] ) {

                $reon_option_key = $option_args[ 'option_name' ] . '_' . $option_args[ 'instance_id' ];
            }

            if ( $option_args[ 'database' ] == 'custom_db' ) {

                do_action( 'reon_save-' . $reon_option_key . '-options', $opt, $option_args[ 'instance_id' ], $autoload );
            } else if ( $option_args[ 'database' ] == 'theme_mod' ) {

                /* =================================================================================
                 * The option name is already prefixed from plugins/themes that uses this framework.
                 * This is a shared framework library.
                 * This framework allows plugins/themes to choose the option name prefix.
                 * =================================================================================
                 */
                set_theme_mod( $reon_option_key, $opt );
            } else {

                /* =================================================================================
                 * The option name is already prefixed from plugins/themes that uses this framework.
                 * This is a shared framework library.
                 * This framework allows plugins/themes to choose the option name prefix.
                 * =================================================================================
                 */
                update_option( $reon_option_key, $opt, $autoload );
            }
        }

        public static function process_default_option_page_fields( $fields ) {
            $result = array();
            foreach ( $fields as $field ) {
                $fld = self::process_default_field( $field );
                $result[] = $fld;
            }
            return $result;
        }

        public static function process_default_post_metabox_fields( $fields ) {
            $result = array();
            foreach ( $fields as $field ) {
                $fld = self::process_default_field( $field );
                $result[] = $fld;
            }
            return $result;
        }

        public static function process_default_post_metabox_wc_fields( $fields ) {
            $result = array();
            foreach ( $fields as $field ) {
                $fld = self::process_default_field( $field );
                $result[] = $fld;
            }
            return $result;
        }

        public static function process_default_field( $field ) {
            $result = $field;
            if ( !isset( $result[ 'id' ] ) ) {
                $result[ 'id' ] = '';
            }

            if ( !isset( $result[ 'full_width' ] ) ) {
                $result[ 'full_width' ] = false;
            }

            if ( !isset( $result[ 'center_head' ] ) ) {
                $result[ 'center_head' ] = false;
            }

            if ( !isset( $result[ 'type' ] ) ) {
                $result[ 'type' ] = 'textbox';
            }

            if ( !isset( $result[ 'sanitize_type' ] ) ) {
                $result[ 'sanitize_type' ] = '';
            }

            if ( !isset( $result[ 'title' ] ) ) {
                $result[ 'title' ] = '';
            }
            if ( !isset( $result[ 'desc' ] ) ) {
                $result[ 'desc' ] = '';
            }


            if ( !isset( $result[ 'tooltip' ] ) ) {
                $result[ 'tooltip' ] = '';
            }
            if ( !isset( $result[ 'label' ] ) ) {
                $result[ 'label' ] = array();
            }


            if ( $result[ 'type' ] == 'simple-repeater' ) {
                if ( !isset( $result[ 'left_width' ] ) ) {
                    $result[ 'left_width' ] = '100%';
                }

                if ( !isset( $result[ 'right_width' ] ) ) {
                    $result[ 'right_width' ] = '0%';
                }
            }

            if ( $result[ 'type' ] == 'select2' ) {
                if ( isset( $result[ 'data' ] ) && !is_array( $result[ 'data' ] ) ) {
                    $ar = array(
                        'source' => $result[ 'data' ],
                        'ajax' => false
                    );
                    $result[ 'data' ] = $ar;
                }

                if ( isset( $result[ 'ajax_data' ] ) && !is_array( $result[ 'ajax_data' ] ) ) {
                    $ar = array(
                        'source' => $result[ 'ajax_data' ],
                        'ajax' => true
                    );

                    $result[ 'data' ] = $ar;
                }
            }
            if ( $result[ 'type' ] == 'button-set' ) {
                if ( isset( $result[ 'data' ] ) && !is_array( $result[ 'data' ] ) ) {
                    $ar = array(
                        'source' => $result[ 'data' ],
                    );
                    $result[ 'data' ] = $ar;
                }
            }

            if ( $result[ 'type' ] == 'autoid' || $result[ 'type' ] == 'hidden' ) {
                $result[ 'is_hidden' ] = true;
            } else {
                $result[ 'is_hidden' ] = false;
            }
            return $result;
        }

    }

}

