<?php

namespace Rvsitebuilder\Wysiwyg\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PostPropertyRequest extends FormRequest
{
    // "advans rexgular"  https://stackoverflow.com/questions/22596587/issue-with-laravel-rules-regex-or-operator/22596677
    /*
     *
     * ห้ามอขะพิเศษ ยกเว้น "_, ,-"   สำหรับ % ถ้าใช้จะอนุญาติเฉพาะ %BLOG_NAME% และ %CATEGORY_NAME% เท่านั้น
     *
     *  ==== ผ่าน ===
     * %BLOG_NAME%/test/%CATEGORY_NAME%/01
     * %BLOG_NAME%/test005/%CATEGORY_NAME%
     * Te_st/%BLOG_NAME%/%CATEGORY_NAME%
     * %BLOG_NAME%/%CATEGORY_NAME%
     * category
     * บล็อก/%BLOG_NAME%/test/%CATEGORY_NAME%/รีวิว
     * [ Chinese (Simplified) ]
     * 测试
     * 今天天气很好
     * [ Persian ]
     * امروز هوا خوب است
     * ازمایش
     * [ Japanese ]
     * 今日は良い天気
     * テ 0ス 7ト
     *
     * ==== ไม่ผ่าน ===
     * %dddd_ddd%/%ddddd%
     * Test/%xx%/%CATEGOR Y_NAME%
     * Test//555
     * /test/set/
     *
     */
    public function rules()
    {
        //    regex option u = /pattern/u = Match with full unicode
        //    mb_ereg_match => CoreServiceProvider.php
        return [
                'postBaseUrl' => ['nullable',
                                   "not_regex:/^(\/)/",
                                   "not_regex:/\/$/",
                                   "not_regex:/[\?\$<>#'\"\+!]/",
                                   "mb_ereg_match:^((%(BLOG_NAME|CATEGORY_NAME)%|([\w -])+)\/)*(%(BLOG_NAME|CATEGORY_NAME)%|([\w- ])+)$",
                                   'unique:core_slug,slug_name',
                                   'forbidden_route',
                ],
                'categoryBaseUrl' => ['nullable',
                                    "not_regex:/^(\/)/",
                                    "not_regex:/\/$/",
                                    "not_regex:/[\?\$<>#'\"\+!]/",
                                    "mb_ereg_match:^((%(BLOG_NAME|CATEGORY_NAME)%|([\w -])+)\/)*(%(BLOG_NAME|CATEGORY_NAME)%|([\w- ])+)$",
                                    'unique:core_slug,slug_name',
                                    'forbidden_route',
                ],
        ];
    }

    public function authorize()
    {
        return $this->user()->isAdmin();
    }

    public function messages()
    {
        //TODO language system
        return [
                'postBaseUrl.mb_ereg_match' => __('wysiwyg::blog.validate.postBaseUrl.mb_ereg_match'),
                'postBaseUrl.not_regex' => __('wysiwyg::blog.validate.postBaseUrl.not_regex'),
                'postBaseUrl.unique' => __('wysiwyg::blog.validate.postBaseUrl.unique'),
                'postBaseUrl.forbidden_route' => __('wysiwyg::blog.validate.postBaseUrl.forbidden_route'),

                'categoryBaseUrl.not_regex' => __('wysiwyg::blog.validate.categoryBaseUrl.not_regex'),
                'categoryBaseUrl.mb_ereg_match' => __('wysiwyg::blog.validate.categoryBaseUrl.mb_ereg_match'),
                'categoryBaseUrl.unique' => __('wysiwyg::blog.validate.categoryBaseUrl.unique'),
                'categoryBaseUrl.forbidden_route' => __('wysiwyg::blog.validate.categoryBaseUrl.forbidden_route'),
        ];
    }

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