<?php
namespace App\Traits;

use App\Models\Setting;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;

trait ControllerTrait
{

    /**
     * 网站标题
     * @param string $key
     * return string
     * @return mixed
     */
    public static function title($key, $level = 'web')
    {
        $id      = "language::{$level}";
        $titles  = trans($id);
        return Arr::get($titles, $key, '');
    }

    /**
     * meta标签
     * @param string $title
     * @param null|array $metaData
     * return Array
     * @param bool $extractContent
     * @return array
     */
    public static function meta($title, $metaData = null, $extractContent = false)
    {
        $company = Setting::get('title') ? ' - ' . Setting::get('title') : '';
        $metaDescription = Setting::get('meta_description');

        if (!isset($metaData['seo_description'])) {
            if (str_contains($metaDescription, '{g}')) {
                $keywords = explode(',', str_replace('，', ',', Setting::get('meta_keywords')));
                $metaDescription = str_replace(['{g}', '{k1}', '{k2}', '{k3}'], [isset_field($company, 'title'), $keywords[0] ?? '', $keywords[1] ?? '', $keywords[2] ?? ''], $metaDescription);
            }
        } else {
            if ($extractContent && !$metaData['seo_description'] && $metaData['content']) {
                $metaDescription = trim(str_replace(['&nbsp;', PHP_EOL, '　'], '', strip_tags($metaData['content'])));

                if (mb_strlen($metaDescription, 'utf-8') > 200) {
                    $metaDescription = mb_substr($metaDescription, 0, 200, 'utf-8') . '...';
                }
            }
        }

        return [
            'title' => $title . $company,
            'keywords' => $metaData && $metaData['seo_keywords'] ? $metaData['seo_keywords'] : Setting::get('meta_keywords'),
            'description' => $metaData && $metaData['seo_description'] ? $metaData['seo_description'] : $metaDescription
        ];
    }

    public static function productMeta($title, $metaData = null)
    {
        $company = Setting::get('title') ? ' - ' . Setting::get('title') : '';

        return [
            'title' => $metaData && $metaData['seo_title'] ? $metaData['seo_title'] : str_replace(['{1}', '{2}'], [$title, $company], Setting::seo('product_title')),
            'keywords' => $metaData && $metaData['seo_keywords'] ? $metaData['seo_keywords'] : str_replace('{1}', $title, Setting::seo('product_keyword')),
            'description' => $metaData && $metaData['seo_description'] ? $metaData['seo_description'] : str_replace('{1}', $title, Setting::seo('product_description'))
        ];
    }

    public static function productCateMeta($tags, $metaData = null)
    {
        $company = Setting::get('title') ? ' - ' . Setting::get('title') : '';

        return [
            'title' => $metaData && $metaData['seo_title'] ? $metaData['seo_title'] : str_replace(['{1}', '{2}', '{3}', '{4}'], [$tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? '', $company], Setting::seo('cate_title')),
            'keywords' => $metaData && $metaData['seo_keywords'] ? $metaData['seo_keywords'] : str_replace(['{1}', '{2}', '{3}'], [$tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? ''], Setting::seo('cate_keyword')),
            'description' => $metaData && $metaData['seo_description'] ? $metaData['seo_description'] : str_replace(['{4}', '{1}', '{2}', '{3}'], [$company, $tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? ''], Setting::seo('cate_description'))
        ];
    }

    /**
     * 验证多语言字段
     * @param Request $request
     * @param string $field
     * return void
     */
    public function validateDescription(Request $request, $field = 'cname')
    {
        $rules = []; 
        foreach (array_keys($request->input('description')) as $key) {
            $rules['description.' . $key. '.' .$field] =  'required';
        }

        $this->validate($request, $rules);
    }
}