<?php

namespace Rvsitebuilder\Wysiwyg\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SaveSiteRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() //condition validation rules **
    {
        return [
                'slug_id' => 'required',
                'savetype' => 'required',
                /*
                 * type for check required field and do process
                 * saveall :: required all fields
                 * header / content / sidebar :: required only fields that process use.
                 */

                'header_content_view' => 'required_if:savetype,===,saveall|required_if:savetype,===,header',
                'header_content_edit' => 'required_if:savetype,===,saveall|required_if:savetype,===,header',

                'footer_content_view' => 'required_if:savetype,===,saveall|required_if:savetype,===,footer',
                'footer_content_edit' => 'required_if:savetype,===,saveall|required_if:savetype,===,footer',

                'page_content_view' => 'required_if:savetype,===,saveall|required_if:savetype,===,content',
                'page_content_edit' => 'required_if:savetype,===,saveall|required_if:savetype,===,content',

                'use_sidebar' => 'required_if:savetype,===,saveall|required_if:savetype,===,sidebar',
                'left_content_view' => 'required_if:savetype,===,saveall|required_if:savetype,===,sidebar',
                'left_content_edit' => 'required_if:savetype,===,saveall|required_if:savetype,===,sidebar',
                'right_content_view' => 'required_if:savetype,===,saveall|required_if:savetype,===,sidebar',
                'right_content_edit' => 'required_if:savetype,===,saveall|required_if:savetype,===,sidebar',
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->user()->isAdmin();
    }

    public function messages()
    {
        //TODO language system
        return [
                'slug_id.required' => __('wysiwyg::blog.validate.SaveSite.slug_id.required'),
                'savetype.required' => __('wysiwyg::blog.validate.SaveSite.savetype.required'),

                'header_content_view.required' => __('wysiwyg::blog.validate.SaveSite.header_content_view.required'),
                'header_content_edit.required' => __('wysiwyg::blog.validate.SaveSite.header_content_edit.required'),

                'footer_content_view.required' => __('wysiwyg::blog.validate.SaveSite.footer_content_view.required'),
                'footer_content_edit.required' => __('wysiwyg::blog.validate.SaveSite.footer_content_edit.required'),

                'page_content_view.required' => __('wysiwyg::blog.validate.SaveSite.page_content_view.required'),
                'page_content_edit.required' => __('wysiwyg::blog.validate.SaveSite.page_content_edit.required'),

                'use_sidebar.required' => __('wysiwyg::blog.validate.SaveSite.use_sidebar.required'),
                'left_content_view.required' => __('wysiwyg::blog.validate.SaveSite.left_content_view.required'),
                'left_content_edit.required' => __('wysiwyg::blog.validate.SaveSite.left_content_edit.required'),
                'right_content_view.required' => __('wysiwyg::blog.validate.SaveSite.right_content_view.required'),
                'right_content_edit.required' => __('wysiwyg::blog.validate.SaveSite.right_content_edit.required'),
        ];
    }

    public function sanitize()
    {
        //TODO Define santize for model ให้ไปดูเพิ่มที่ https://laravel.com/docs/5.5/helpers#available-methods
        return [
                'slug_id' => 'clean',
                'savetype' => 'clean',

                'header_content_view' => 'clean',
                'header_content_edit' => 'clean',

                'footer_content_view' => 'clean',
                'footer_content_edit' => 'clean',

                'page_content_view' => 'clean',
                'page_content_edit' => 'clean',

                'use_sidebar' => 'clean',
                'left_content_view' => 'clean',
                'left_content_edit' => 'clean',
                'right_content_view' => 'clean',
                'right_content_edit' => 'clean',
        ];
    }
}
