<?php

namespace WC_NEXT_SYNC;

use WC_NEXT_SYNC\Common\Settings_Form as Base_Settings_Form;

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

/**
 * Plugin's settings ui class.
 */
class Settings_Form extends Base_Settings_Form
{
    protected static function input_render($setting, $field, $schema, $value)
    {
        if ($field === 'connected') {
            return self::connection_check($setting, $value);
        } elseif ($field === 'from_date') {
            return sprintf(
                '<input type="date" name="%s" value="%s" />',
                esc_attr($setting . "[{$field}]"),
                esc_attr($value)
            );
        } elseif ($field === 'password') {
            return sprintf(
                '<input type="password" name="%s" value="%s" />',
                esc_attr($setting . "[{$field}]"),
                esc_attr($value)
            );
        }

        return parent::input_render($setting, $field, $schema, $value);
    }

    private static function connection_check($setting, $value)
    {
        if ($value) {
            $icon = '<span>✅</span>';
        } else {
            $icon = '<span>❌</span>';
        }

        return sprintf(
            '<div><input style="display:none" aria-hidden="true" type="hidden" name="%s" value="%s" />%s</div>',
            esc_attr($setting . '[connected]'),
            (string) $value,
            $icon
        );
    }

    protected static function setting_title($setting_name)
    {
        switch ($setting_name) {
            case 'wc-next-sync_connection':
                return __('Connection', 'wc-next-sync');
            case 'wc-next-sync_orders':
                return __('Orders', 'wc-next-sync');
            case 'wc-next-sync_products':
                return __('Products', 'wc-next-sync');
            case 'wc-next-sync_customers':
                return __('Customers', 'wc-next-sync');
            case 'wc-next-sync_coupons':
                return __('Coupons', 'wc-next-sync');
            default:
                return $setting_name;
        }
    }

    protected static function setting_description($setting_name)
    {
        switch ($setting_name) {
            case 'wc-next-sync_connection':
                return __(
                    'Set up the NextCloud connection and synchronization schedule',
                    'wc-next-sync'
                );
            case 'wc-next-sync_orders':
                return __('Configure the orders tab layout', 'wc-next-sync');
            case 'wc-next-sync_products':
                return __('Configure the products tab layout', 'wc-next-sync');
            case 'wc-next-sync_customers':
                return __('Configure the customers tab layout', 'wc-next-sync');
            case 'wc-next-sync_coupons':
                return __('Configure the coupons tab layout', 'wc-next-sync');
            default:
                return '';
        }
    }

    protected static function field_label($field_name, $setting_name)
    {
        switch ($field_name) {
            case 'username':
                return _x('Username', 'field label', 'wc-next-sync');
            case 'password':
                return _x('Password', 'field label', 'wc-next-sync');
            case 'nextcloud_url':
                return _x('Nextcloud URL', 'field label', 'wc-next-sync');
            case 'filepath':
                return _x('Filepath', 'field label', 'wc-next-sync');
            case 'status':
                return _x('Status', 'field label', 'wc-next-sync');
            case 'schedule':
                return _x('Schedule', 'field label', 'wc-next-sync');
            case 'from_date':
                return _x('From date', 'field label', 'wc-next-sync');
            case 'fields':
                return _x('Fields', 'field label', 'wc-next-sync');
            case 'sync':
                return _x(
                    'Enable synchronization',
                    'field label',
                    'wc-next-sync'
                );
            default:
                return $field_name;
        }
    }
}
