<?php

namespace Fab\Metabox;

! defined( 'WPINC ' ) or die;

/**
 * FAB Metabox Setting.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class FABMetaboxSetting {

    // Load Traits.
    use \Fab\Plugin\Helper\Singleton;

    /**
     * Params object
     *
     * @var object
     */
    protected $params;

    /** FAB Metabox Settings */
    public static $types = array();

    /** FAB Metabox Settings */
    public static $triggers = array(
        array(
            'id'   => 'exit_intent',
            'text' => 'Exit Intent',
        ),
        array(
            'id'   => 'time_delay',
            'text' => 'Time Delay',
        ),
    );

    /** $_POST input */
    public static $input = array(
        'fab_setting_type'          => array( 'default' => '' ),

        /** Link */
        'fab_setting_link'          => array( 'default' => '' ),
        'fab_setting_link_behavior' => array( 'default' => '' ),

        /** Print */
        'fab_setting_print_target'  => array( 'default' => '' ),

        /** Toast */
        'fab_setting_toast'         => array(
            'default' => array(
                'duration'          => 3000,
                'button_text'       => '',
                'button_url'        => '',
                'window'            => false,
                'closeable'         => false,
                'remember_on_click' => false,
                'gravity'           => 'top',
                'position'          => 'left',
                'background'        => '#1e73be',
                'text_color'        => '#ffffff',
                'bar_color'         => '#ff5722',
            ),
        ),
    );

    /** FAB Metabox Post Metas */
    public static $post_metas = array(
        'type'          => array( 'meta_key' => 'fab_setting_type' ),

        /** Link */
        'link'          => array( 'meta_key' => 'fab_setting_link' ),
        'link_behavior' => array( 'meta_key' => 'fab_setting_link_behavior' ),

        /** Print */
        'print_target'  => array( 'meta_key' => 'fab_setting_print_target' ),

        /** Toast */
        'toast'         => array( 'meta_key' => 'fab_setting_toast' ),
    );

    /** Sanitize */
    public function sanitize() {
        $params = $_POST;
        $input  = apply_filters( 'fab_setting_input', self::$input, $params );

        // Filter input.
        foreach ( $params as $key => $value ) {
            if ( isset( $input[ $key ] ) && $value ) {
                $this->params[ $key ] = $value;
            }
        }
    }

    /** SetDefaultInput */
    public function setDefaultInput() {
        /** Default Input Function */
        $input = apply_filters( 'fab_setting_input', self::$input, $this->params );

        foreach ( $input as $key => $value ) {
            $this->params[ $key ] = isset( $this->params[ $key ] ) ? $this->params[ $key ] : $value['default'];

            if ( is_array( $this->params[ $key ] ) ) {
                $this->params[ $key ] += $value['default'];
            }
        }

        /** Transform Data */
        $this->params['fab_setting_link']                       = ( $this->params['fab_setting_link'] ) ? $this->params['fab_setting_link'] : '#';
        $this->params['fab_setting_toast']['window']            = ( $this->params['fab_setting_toast']['window'] === 'true' || $this->params['fab_setting_toast']['window'] == 1 ) ? 1 : 0;
        $this->params['fab_setting_toast']['closeable']         = ( $this->params['fab_setting_toast']['closeable'] === 'true' || $this->params['fab_setting_toast']['closeable'] == 1 ) ? 1 : 0;
        $this->params['fab_setting_toast']['remember_on_click'] = ( $this->params['fab_setting_toast']['remember_on_click'] === 'true' || $this->params['fab_setting_toast']['remember_on_click'] == 1 ) ? 1 : 0;

        do_action( 'fab_setting_set_default_input', $this->params );
    }

    /** Save data to database */
    public function save() {
        global $post;

        foreach ( $this->params as $key => $value ) {
            update_post_meta( $post->ID, $key, $value );
        }
    }

    /** Get types, check the dependency and enable the type */
    public static function get_types() {
        $types_with_dependencies = apply_filters( 'fab_setting_types', self::$types, array() );
        ksort( $types_with_dependencies );

        foreach ( $types_with_dependencies as &$type ) {
            if ( isset( $type['children'] ) && is_array( $type['children'] ) ) {
                foreach ( $type['children'] as &$child ) {
                    $child['is_enable'] = true;

                    if ( isset( $child['dependency'] ) ) {
                        $child['is_enable'] = is_plugin_active( $child['dependency'] );
                    }
                }
            }
        }

        return $types_with_dependencies;
    }

    /** Get input with filters */
    public static function get_input() {
        $input = apply_filters( 'fab_setting_input', self::$input, array() );

        return $input;
    }

    /** Get post metas with filters */
    public static function get_post_metas() {
        $post_metas = apply_filters( 'fab_setting_post_metas', self::$post_metas );

        return $post_metas;
    }
}
