<?php

namespace GriffinForms\Admin\Html\FormControls\Field\FieldSettings;

class InputPasswordSettings extends \GriffinForms\Admin\Html\FormControls\Field\FieldSettings\Format
{
    protected $display_strength_meter;
    protected $show_password_toggle;
    protected $require_confirmation;
    protected $confirmation_label;
    protected $confirmation_message;
    protected $autocomplete_mode;
    protected $encrypt_passwords;
    protected $encryption_key;

    protected function setDisplayStrengthMeter()
    {
        $this->display_strength_meter = isset($this->value['display_strength_meter']) ? (int) $this->value['display_strength_meter'] : 1;
    }

    protected function setShowPasswordToggle()
    {
        $this->show_password_toggle = isset($this->value['show_password_toggle']) ? (int) $this->value['show_password_toggle'] : 1;
    }

    protected function setRequireConfirmation()
    {
        $this->require_confirmation = isset($this->value['require_confirmation']) ? (int) $this->value['require_confirmation'] : 0;
    }

    protected function setConfirmationLabel()
    {
        $default = $this->lang->getText('confirmation_label_default');
        $this->confirmation_label = isset($this->value['confirmation_label']) ? sanitize_text_field($this->value['confirmation_label']) : $default;
        if ($this->confirmation_label === '') {
            $this->confirmation_label = $default;
        }
    }

    protected function setConfirmationMessage()
    {
        $default = $this->lang->getText('confirmation_message_default');
        $this->confirmation_message = isset($this->value['confirmation_message']) ? sanitize_text_field($this->value['confirmation_message']) : $default;
        if ($this->confirmation_message === '') {
            $this->confirmation_message = $default;
        }
    }

    protected function setAutocompleteMode()
    {
        $allowed_modes = array('off', 'on', 'new-password', 'current-password');
        $mode = isset($this->value['autocomplete_mode']) ? sanitize_text_field($this->value['autocomplete_mode']) : 'new-password';
        $this->autocomplete_mode = in_array($mode, $allowed_modes, true) ? $mode : 'new-password';
    }

    protected function setEncryptPasswords()
    {
        $this->encrypt_passwords = isset($this->value['encrypt_passwords']) ? (int) $this->value['encrypt_passwords'] : 0;
    }

    protected function setEncryptionKey()
    {
        $this->encryption_key = isset($this->value['encryption_key']) ? sanitize_text_field($this->value['encryption_key']) : '';
    }

    public function html()
    {
        $this->setDisplayStrengthMeter();
        $this->setShowPasswordToggle();
        $this->setRequireConfirmation();
        $this->setConfirmationLabel();
        $this->setConfirmationMessage();
        $this->setAutocompleteMode();
        $this->setEncryptPasswords();
        $this->setEncryptionKey();


        echo '<div class="">';
        echo '<h6 class="fw-semibold">' . esc_html($this->lang->getText('password_display_section', 'label')) . '</h6>';
        echo '<div class="row">';
        echo '<div class="col-md-6 mb-3">';
        $this->displayStrengthMeterToggle();
        echo '</div>';
        echo '<div class="col-md-6 mb-3">';
        $this->showPasswordToggle();
        echo '</div>';
        echo '<div class="col-md-6 mb-3">';
        $this->autocompleteModeSelect();
        echo '</div>';
        echo '</div>';

        echo '<hr class="my-4">';

        echo '<h6 class="fw-semibold">' . esc_html($this->lang->getText('password_confirmation_section', 'label')) . '</h6>';
        $this->requireConfirmationToggle();
        echo '<div id="griffinforms-confirmation-settings" class="mt-3 ps-3 border-start" style="' . ($this->require_confirmation ? '' : 'display:none;') . '">';
        echo '<div class="row">';
        echo '<div class="col-md-6 mb-3">';
        $this->confirmationLabelField();
        echo '</div>';
        echo '<div class="col-md-6 mb-3">';
        $this->confirmationMessageField();
        echo '</div>';
        echo '</div>';
        echo '</div>';

        echo '<hr class="my-4">';

        echo '<h6 class="fw-semibold">' . esc_html($this->lang->getText('password_storage_section', 'label')) . '</h6>';
        $this->encryptPasswordsToggle();
        echo '<div id="griffinforms-encryption-settings" class="mt-3 ps-3 border-start" style="' . ($this->encrypt_passwords ? '' : 'display:none;') . '">';
        $this->encryptionKeyField();
        echo '</div>';
        echo '</div>';
    }

    protected function displayStrengthMeterToggle()
    {
        $toggle = new \GriffinForms\Admin\Html\FormControls\ToggleCheckBox(
            'display_strength_meter',
            $this->display_strength_meter,
            array('label' => $this->lang->getText('display_strength_meter', 'label'))
        );
        $toggle->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('display_strength_meter', 'description')) . '</div>';
    }

    protected function showPasswordToggle()
    {
        $toggle = new \GriffinForms\Admin\Html\FormControls\ToggleCheckBox(
            'show_password_toggle',
            $this->show_password_toggle,
            array('label' => $this->lang->getText('show_password_toggle', 'label'))
        );
        $toggle->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('show_password_toggle', 'description')) . '</div>';
    }

    protected function autocompleteModeSelect()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('autocomplete_mode', $this->autocomplete_mode);
        $field->selectOptions(array(
            'new-password'      => $this->lang->getText('autocomplete_mode_new_password'),
            'current-password'  => $this->lang->getText('autocomplete_mode_current_password'),
            'on'                => $this->lang->getText('autocomplete_mode_on'),
            'off'               => $this->lang->getText('autocomplete_mode_off')
        ));

        $field_id = $field->elementId();
        echo '<label class="form-label" for="' . esc_attr($field_id) . '">' . esc_html($this->lang->getText('autocomplete_mode', 'label')) . '</label>';
        $field->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('autocomplete_mode', 'description')) . '</div>';
    }

    protected function requireConfirmationToggle()
    {
        $toggle = new \GriffinForms\Admin\Html\FormControls\ToggleCheckBox(
            'require_confirmation',
            $this->require_confirmation,
            array('label' => $this->lang->getText('require_confirmation', 'label'))
        );
        $toggle->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('require_confirmation', 'description')) . '</div>';
    }

    protected function confirmationLabelField()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputText('confirmation_label', $this->confirmation_label);
        echo '<label class="form-label mt-3" for="griffinforms-confirmationlabel-inputtext">' . esc_html($this->lang->getText('confirmation_label', 'label')) . '</label>';
        $field->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('confirmation_label', 'description')) . '</div>';
    }

    protected function confirmationMessageField()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputText('confirmation_message', $this->confirmation_message);
        echo '<label class="form-label mt-3" for="griffinforms-confirmationmessage-inputtext">' . esc_html($this->lang->getText('confirmation_message', 'label')) . '</label>';
        $field->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('confirmation_message', 'description')) . '</div>';
    }

    protected function encryptPasswordsToggle()
    {
        $toggle = new \GriffinForms\Admin\Html\FormControls\ToggleCheckBox(
            'encrypt_passwords',
            $this->encrypt_passwords,
            array('label' => $this->lang->getText('encrypt_passwords', 'label'))
        );
        $toggle->html();
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('encrypt_passwords', 'description')) . '</div>';
    }

    protected function encryptionKeyField()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputText('encryption_key', $this->encryption_key);
        echo '<label class="form-label mt-3" for="griffinforms-encryptionkey-inputtext">' . esc_html($this->lang->getText('encryption_key', 'label')) . '</label>';
        echo '<div class="input-group">';
        $field->html();
        echo '<button type="button" class="btn btn-secondary" id="griffinforms-generate-encryption-key">' . esc_html($this->lang->getText('encryption_key_generate')) . '</button>';
        echo '</div>';
        echo '<div class="form-text small text-secondary">' . esc_html($this->lang->getText('encryption_key', 'description')) . '</div>';
    }
}
