<?php namespace Tussendoor\Reloadify\Controllers\WooCommerce;

use Tussendoor\Reloadify\Models\SubscriptionCheckbox;


class SettingsController
{
    /**
     * The checkbox model
     * 
     * @var SubscriptionCheckbox
     */
    protected $checkbox;
    
    /**
     * Register the class
     *
     * @return void
     */
    public function register()
    {
        $this->setup();

        add_filter('woocommerce_settings_tabs_array', [$this, 'addReloadifySettingsTab'], 50);
        add_action('woocommerce_settings_tabs_reloadify_settings', [$this, 'addSettingsToReloadifyTab']);
        add_action('woocommerce_update_options_reloadify_settings', [$this, 'saveReloadifySettings']);
    }

    private function setup()
    {
        $this->checkbox = new SubscriptionCheckbox;
    }

    /**
     * Add a new tab to the WooCommerce settings page
     * 
     * @param array $settings_tabs
     * @return array
     */
    public function addReloadifySettingsTab($settings_tabs) {
        $settings_tabs['reloadify_settings'] = __('Reloadify', 'reloadify' );
        return $settings_tabs;
    }

    /**
     * Add the settings to the new tab
     * 
     * @return void
     */
    public function addSettingsToReloadifyTab() {
        woocommerce_admin_fields($this->getReloadifySettings());
    }

    /**
     * Save the settings
     * 
     * @return void
     */
    public function saveReloadifySettings() {
        woocommerce_update_options($this->getReloadifySettings());
    }

    /**
     * Get the settings
     * 
     * @return array
     */
    public function getReloadifySettings() {

        $settings = array(
            'section_title' => array(
                'name'     => __('Reloadify instellingen', 'tsd-reloadify'),
                'type'     => 'title',
                'desc'     => '',
                'id'       => 'wc_reloadify_settings_section_title'
            ),
            'add_checkbiox' => array(
                'name'          => __('Voeg abbonnement checkbox toe', 'tsd-reloadify'),
                'type'          => 'checkbox',
                'desc_tip'      => __('Checkbox aan checkout toevoegen zodat gebruikers kunnen kiezen voor een abonnement', 'tsd-reloadify'),
                'id'            => $this->checkbox::ADD_CHECKBOX_SETTING,
            ),
            'checkbox_label' => array(
                'name'              => __('Checkbox label', 'tsd-reloadify'),
                'type'              => 'text',
                'desc_tip'          => __('Deze tekst wordt naast de checkbox getoond', 'tsd-reloadify'),
                'id'                => $this->checkbox::CHECKBOX_LABEL_SETTING,
                'placeholder'       => __('Abonneer je op onze nieuwsbrief', 'tsd-reloadify'),
                'custom_attributes' => [
                    'required' => 'required'
                ],
            ),
            'checkbox_status' => array(
                'name'      => __('Standaard checkbox status', 'tsd-reloadify'),
                'type'      => 'select',
                'id'        => $this->checkbox::CHECKBOX_STATUS_SETTING,
                'options'   => $this->checkbox->getCheckboxStatusOptions()
            ),
            'checkbox_location' => array(
                'name'      => __('Standaard checkbox locatie', 'tsd-reloadify'),
                'type'      => 'select',
                'id'        => $this->checkbox::CHECKBOX_LOCATION_SETTING,
                'options'   => $this->checkbox->getCheckboxLocations(),
            ),
            'section_end' => array(
                'type' => 'sectionend',
                'id' => 'wc_reloadify_settings_section_end'
            )
        );
  
        return apply_filters('wc_reloadify_settings_settings', $settings);
    }

}