<?php

namespace Aura\Forms\Http\Livewire;

use Aura\Forms\Resources\Form;
use Eminiarts\Aura\Traits\RepeaterFields;
use Illuminate\Support\Arr;
use Livewire\Component;

class EditQuestion extends Component
{
    use RepeaterFields;

    public $field;

    public $model;

    public $open = false;

    public $post;

    public $question;

    public function activate($params)
    {
        $this->model = Form::find($params['form']);

        $this->post = $this->model->toArray();

        $questions = json_decode($this->model->questions, true);

        // finf in array item with key _id == $params['question']
        $this->question = Arr::first($questions, function ($value, $key) use ($params) {
            return $value['_id'] == $params['question'];
        });

        $this->field = $this->question;
        $this->post['fields'] = $this->field;

        // Check if field is an input field
        if (app($this->field['type'])->isInputField()) {
            // if $this->post['fields']['on_index'] is not set, set it to true (default)
            if (! isset($this->post['fields']['on_index'])) {
                $this->post['fields']['on_index'] = true;
            }
            // on_forms
            if (! isset($this->post['fields']['on_forms'])) {
                $this->post['fields']['on_forms'] = true;
            }
            // on_view
            if (! isset($this->post['fields']['on_view'])) {
                $this->post['fields']['on_view'] = true;
            }
        }

        $this->open = true;
    }

    public function deleteField($id)
    {
        // $this->model = Form::find($id);
        // emit removeQuestion with id
        $this->emit('removeQuestion', $id);

        $this->open = false;

        // $this->emit('refreshComponent');
    }

    public function getGroupedFieldsProperty()
    {
        // Fields from the model
        $modelFields = $this->model->getFields();

        // Specific fields for the operation type
        $operationFields = app($this->model->type)->getFields();

        // Merge the two
        $fields = array_merge($modelFields, $operationFields);

        return $this->model->getGroupedFields($fields);
    }

    public function render()
    {
        return view('forms::livewire.edit-question');
    }

    public function rules()
    {
        return Arr::dot([
            'post.fields' => $this->model->validationRules(),
        ]);
    }

    public function save()
    {
        // Validate
        // $this->validate();

        // emit event to parent with slug and value
        $this->emit('saveFormField', ['slug' => $this->question['slug'], 'value' => $this->post['fields']]);

        $this->open = false;
    }

    public function updateType()
    {
        // when type is changed, update the fields
        $this->model->update(['type' => $this->post['fields']['type']]);

        $this->emit('updatedOperation');
    }

    // public function updatingOpen($value)
    // {
    //     dd('updatingOpen', $value);
    // }

    public function validateBeforeClosing()
    {
        // dd('validateBeforeClosing', $value);
        $this->validate();

        $this->open = false;
    }
}
