<?php
namespace Formipay;
use Formipay\Traits\SingletonTrait;

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

class Field {

    use SingletonTrait;

    // Render a text input field
    public function render_text_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="text" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render an email input field
    public function render_email_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="email" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a number input field
    public function render_number_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="number" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a url field
    public function render_url_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="url" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a telephone field
    public function render_tel_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="tel" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a date field
    public function render_date_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="date" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a datetime field
    public function render_datetime_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="datetime-local" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a color field
    public function render_color_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <input type="color" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" data-label="'.esc_attr($label).'"'.$required.' '.$calc.'>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }
    
    // Render a select dropdown field
    public function render_select_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $placeholder = $args['placeholder'] ?? '';
        $options = $args['field_options'] ?? [];
        $default_value = $args['default_value'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        $options_html = '';
        foreach ($options as $option) {
            $value = $option['value'] ?? $option['label'];
            $selected = ($default_value == $value) ? ' selected' : '';
            $options_html .= '<option value="'.esc_attr($value).'"'.$selected.'>'.esc_html($option['label']).'</option>';
        }
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <select id="'.esc_attr($field_id).'" name="'.esc_attr($field_id).'" class="formipay-input formipay-select'.$calculable.'" data-label="'.esc_attr($label).'" data-placeholder="'.esc_attr($placeholder).'"'.$required.' '.$calc.'>
                '.$options_html.'
            </select>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a checkbox group field
    public function render_checkbox_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $options = $args['field_options'] ?? [];
        $default_value = $args['default_value'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        $options_html = '';
        foreach ($options as $index => $option) {
            $value = $option['value'] ?? $option['label'];
            $checked = ($default_value == $value) ? ' checked' : '';
            $options_html .= '
                <label for="'.esc_attr($field_id.'-'.$index).'" class="formipay-label">
                    <input type="checkbox" name="'.esc_attr($field_id).'[]" id="'.esc_attr($field_id.'-'.$index).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'"'.$checked.$required.' '.$calc.'>
                    <span>'.esc_html($option['label']).'</span>
                </label>';
        }
        return '
        <div class="formipay-field-group">
            <label class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <div class="formipay-checkbox-field-group">'.$options_html.'</div>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a radio group field
    public function render_radio_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $options = $args['field_options'] ?? [];
        $default_value = $args['default_value'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        $options_html = '';
        foreach ($options as $index => $option) {
            $value = $option['value'] ?? $option['label'];
            $checked = ($default_value == $value) ? ' checked' : '';
            $options_html .= '
                <label for="'.esc_attr($field_id.'-'.$index).'" class="formipay-label">
                    <input type="radio" name="'.esc_attr($field_id).'" id="'.esc_attr($field_id.'-'.$index).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'"'.$checked.$required.' '.$calc.'>
                    <span>'.esc_html($option['label']).'</span>
                </label>';
        }
        return '
        <div class="formipay-field-group">
            <label class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <div class="formipay-radio-field-group">'.$options_html.'</div>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a textarea field
    public function render_textarea_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $placeholder = $args['placeholder'] ?? '';
        $value = $args['default_value'] ?? '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group">
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <textarea class="formipay-input'.$calculable.'" name="'.esc_attr($field_id).'" placeholder="'.esc_attr($placeholder).'" data-label="'.esc_attr($label).'"'.$required.' rows="4" '.$calc.'>'.esc_html($value).'</textarea>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a hidden input field
    public function render_hidden_field($args) {
        $value = $args['default_value'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '<input type="hidden" name="'.esc_attr($field_id).'" class="formipay-input'.$calculable.'" value="'.esc_attr($value).'" '.$calc.'>';
    }

    // Render a divider field
    public function render_divider($args) {
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group'.$calculable.'" '.$calc.'>
            <label for="'.esc_attr($field_id).'" class="formipay-label divider-label">'.$label.'</label>
            <span class="divider-line"></span>
            '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
        </div>';
    }

    // Render a page break field
    public function render_page_break($args) {
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $description = $args['description'] ?? '';
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        return '
        <div class="formipay-field-group formipay-page-break'.$calculable.'" '.$calc.'>
            <div class="formipay-page-break-information">
                <label for="'.esc_attr($field_id).'" class="formipay-label divider-label">'.$label.'</label>
                <span class="divider-line"></span>
                '.(!empty($description) ? '<p class="formipay-input-desc">'.esc_html($description).'</p>' : '').'
            </div>
        </div>';
    }

    // Render a country list select field
    public function render_country_list_field($args) {
        $required = !empty($args['is_required']) && $args['is_required'] === 'yes' ? ' required' : '';
        $label = $args['label'] ?? '';
        $field_id = $args['field_id'] ?? '';
        $placeholder = $args['placeholder'] ?? '';
        $countries = function_exists('formipay_country_array') ? formipay_country_array() : [];
        $calculable = $args['calculable'] ?? '';
        $calc = $args['calc'] ?? '';
        $options_html = '';
        foreach ($countries as $country) {
            $options_html .= '<option value="'.esc_attr($country['name']).'" data-phone-code="'.esc_attr($country['phone']).'">'.esc_html($country['name']).'</option>';
        }
        return '
        <div class="formipay-field-group preset select-searchable'.$calculable.'" '.$calc.'>
            <label for="'.esc_attr($field_id).'" class="formipay-label">'.$label.($required ? ' <span class="formipay-asterisk">*</span>' : '').'</label>
            <select id="'.esc_attr($field_id).'" name="'.esc_attr($field_id).'" class="formipay-input formipay-select" data-label="'.esc_attr($label).'" data-placeholder="'.esc_attr($placeholder).'"'.$required.'>
                '.$options_html.'
            </select>
        </div>';
    }
}
