<?php

namespace Aura\Forms\Http\Livewire;

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

class EditQuestion extends Component
{
    use RepeaterFields;

    public $field;

    public $form;

    public $model;

    public $open = false;

    public $question;

    public $resource;

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

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

        // $questions = json_decode($this->model->questions, true);
        $questions = is_string($this->model->questions) ? json_decode($this->model->questions, true) : $this->model->questions;

        // 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->form['fields'] = $this->field;

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

        $this->open = true;
    }

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

        $this->open = false;

        // $this->dispatch('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([
            'form.fields' => $this->model->validationRules(),
        ]);
    }

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

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

        $this->open = false;
    }

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

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

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

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

        $this->open = false;
    }
}
