<?php

namespace Aura\Forms\Fields;

use Eminiarts\Aura\Traits\InputFields;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;

class FormField
{
    use InputFields;
    use Macroable;
    use Tappable;

    public $component = null;

    public $field;

    public bool $group = false;

    public string $type = 'input';

    public $view = null;

    public function component()
    {
        if ($this->view && is_string(request()->route()->action['uses']) && str(request()->route()->action['uses'])->contains(View::class)) {
            return $this->view;
        }

        return $this->component;
    }

    public function display($field, $value, $model)
    {
        return $value;
    }

    // public $component;

    public function field($field)
    {
        return $this;
    }

    public function get($field, $value)
    {
        return $value;
    }

    public function getFields()
    {
        return [
            [
                'name' => 'Field',
                'type' => 'Eminiarts\\Aura\\Fields\\Tab',
                'slug' => 'field',
                'style' => [],
            ],
            [
                'name' => 'Name',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => 'required',
                'slug' => 'name',
            ],
            [
                'label' => 'Slug',
                'name' => 'Slug',
                'type' => 'Eminiarts\\Aura\\Fields\\Slug',
                'validation' => 'required',
                'slug' => 'slug',
                'based_on' => 'name',
            ],
            [
                'name' => 'Validation',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'slug' => 'validation',
            ],
            [
                'name' => 'Type',
                'type' => 'Eminiarts\\Aura\\Fields\\Select',
                'validation' => 'required',
                'slug' => 'type',
                'options' => [
                    'option_group_1' => 'Input Fields',
                    'Aura\\Forms\\Fields\\Text' => 'Text',
                    'Aura\\Forms\\Fields\\Textarea' => 'Textarea',
                    'Aura\\Forms\\Fields\\Date' => 'Date',
                    'Aura\\Forms\\Fields\\Select' => 'Select',
                    'Aura\\Forms\\Fields\\Upload' => 'Upload',
                ],
            ],
            [
                'name' => 'instructions',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'slug' => 'instructions',

            ],
            [
                'name' => 'View',
                'type' => 'Eminiarts\\Aura\\Fields\\Tab',
                'slug' => 'tab-2',
                'style' => [],
            ],
            [
                'name' => 'Width',
                'type' => 'Eminiarts\\Aura\\Fields\\Number',
                'validation' => '',
                'suffix' => '%',
                'instructions' => 'Width of the field in the form in %.',
                'slug' => 'style.width',
            ],

            [
                'name' => 'Conditional Logic',
                'type' => 'Eminiarts\\Aura\\Fields\\Tab',
                'slug' => 'conditional_logic',
                'style' => [],
            ],

            [
                'name' => 'Add Condition',
                'type' => 'Eminiarts\\Aura\\Fields\\Repeater',
                'validation' => '',
                'conditional_logic' => [],
                'style' => [
                    'width' => '100',
                ],
                'slug' => 'conditional_logic',
            ],
            [
                'name' => 'Type',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'instructions' => 'Slug of the field to check. You can also use "role"',
                'conditional_logic' => [],
                'slug' => 'field',
                'style' => [
                    'width' => '33',
                ],
            ],
            [
                'name' => 'Operator',
                'type' => 'Eminiarts\\Aura\\Fields\\Select',
                'validation' => '',
                'options' => [
                    '==' => '==',
                    '!=' => '!=',
                    '>' => '>',
                    '>=' => '>=',
                    '<' => '<',
                    '<=' => '<=',
                ],
                'conditional_logic' => [],
                'slug' => 'operator',
                'style' => [
                    'width' => '33',
                ],
            ],
            [
                'name' => 'Value',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'value',
                'style' => [
                    'width' => '33',
                ],
            ],

        ];
    }

    public function isInputField()
    {
        return in_array($this->type, ['input', 'repeater', 'group']);
    }

    public function value($value)
    {
        return $value;
    }

    public function view($view, $data = [], $mergeData = [])
    {
        $this->view = $view;

        return $this;
    }
}
