<?php
namespace MDirectorNewsletter\includes;

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

use WP_Widget;

if (!class_exists('mdirectorWidget')) {

    /**
     * Class mdirectorWidget
     */
    class mdirectorWidget extends WP_Widget {
        public function __construct() {
            $widgetOps = array(
                'classname' => 'MDirectorNewsletter\includes\mdirectorWidget',
                'description' => __('WIDGET__TITLE',
                    'emblue-newsletter')
            );

            parent::__construct('mdirectorWidget', 'MDirector Widget', $widgetOps);
        }

        // widget form creation
        public function form($instance) {
            $instance = wp_parse_args((array)$instance, array('title' => ''));
            $title = $instance['title'];
            $description = isset($instance['description']) ? $instance['description'] : '';

            echo '
            <p><label
                for="' . esc_attr( $this->get_field_id('title') ) . '">' .
                esc_html__('WIDGET-FORM__TITLE', 'emblue-newsletter') . ':
                <input class="widefat"
                    id="' . esc_attr( $this->get_field_id('title') ) . '"
                    name="' . esc_attr( $this->get_field_name('title') ) . '"
                    type="text"
                    value="' . esc_attr($title) . '"/></label>
            </p>
            <p>
                <label for="' . esc_attr( $this->get_field_id('description') ) . '">' .
                    esc_html__('WIDGET-FORM__TEXT_SUPPORT', 'emblue-newsletter') . ':
                    <textarea class="widefat"
                        id="' . esc_attr( $this->get_field_id('description') ) . '"
                        name="' . esc_attr( $this->get_field_name('description') ) . '">' .
                            esc_attr($description) . '
                    </textarea>
                </label>
            </p>';
        }

        /**
         * @param array $newInstance
         * @param array $oldInstance
         *
         * @return array
         */
        public function update($newInstance, $oldInstance) {
            $instance = $oldInstance;
            $instance['title'] = $newInstance['title'];
            $instance['description'] = $newInstance['description'];

            return $instance;
        }

        /**
         * @param array $args
         * @param array $instance
         *
         * @throws \Throwable
         * @throws \\Twig\Error\LoaderError
         * @throws \\Twig\Error\RuntimeError
         * @throws \\Twig\Error\SyntaxError
         */
        public function widget($args, $instance) {
            $MdirectorUtils = new Mdirector_Newsletter_Utils();
            $output = $MdirectorUtils->getRegisterFormHTML($args, $instance);

            if ( $output ) {
                wp_enqueue_script( 'mdirector-public' );
            }

            echo wp_kses( $output, Mdirector_Newsletter_Utils::allowedHtml() );
            return;
        }
    }

    // register widget
    add_action('widgets_init', function () {
        return register_widget("MDirectorNewsletter\includes\mdirectorWidget");
    });
}
