<?php

namespace App\Http\Services;

use Fuse\Fuse;
use Illuminate\Support\Facades\Redis;

class BrandService
{

    public static function getStandardBrandList($map)
    {
        $standardBrandData = \Http::get(config('website.footstone_url') . '/open/getStandardBrandList')->json();
        if (isset($standardBrandData['code']) && $standardBrandData['code'] == 0) {
            $standardBrandCache = $standardBrandData['data'];
            ksort($standardBrandCache);
            $standardBrandCache = array_values($standardBrandCache);
            $standardBrandCache = array_map(function ($item) {
                return \Arr::only($item, ['brand_name_en', 'standard_brand_id', 'brand_short_name_en', 'brand_logo']);
            }, $standardBrandCache);
            if (!empty($map['brand_name'])) {
                $fuse = new Fuse($standardBrandCache, [
                    'keys' => ['brand_name_en'],
                    'minMatchCharLength' => 3,
                    'threshold' => 0
                ]);
                return $fuse->search($map['brand_name']);
            }
            return $standardBrandCache;
        }
        return [];
    }


    //获取品牌页的数据(标准品牌)
    public static function getStandardBrandListWithLetter()
    {
        $redisKey = 'semour_standard_brand_map';
        $standardBrandList = Redis::get($redisKey);
        if ($standardBrandList) {
            return json_decode($standardBrandList, true);
        }
        $standardBrandData = \Http::get(config('website.footstone_url') . '/open/getStandardBrandList')->json();
        if (isset($standardBrandData['code']) && $standardBrandData['code'] == 0) {
            $standardBrandCache = $standardBrandData['data'];
            $letters = generate_letters();
            $standardBrandList = [];
            foreach ($standardBrandCache as $standardBrand) {
                $matchLetter = false;
                foreach ($letters as $letter) {
                    if (start_with(strtoupper($standardBrand['brand_name']), $letter)) {
                        if (empty($standardBrand['brand_name_en'])) {
                            continue;
                        }
                        $standardBrandList[$letter][] = $standardBrand;
                        $matchLetter = true;
                    }
                }
                if (!$matchLetter && !empty($standardBrand['brand_name_en'])) {
                    $standardBrandList['#'][] = $standardBrand;
                }
            }
            ksort($standardBrandList);
            Redis::set($redisKey, json_encode($standardBrandList));
            Redis::expire($redisKey, 60);
            return $standardBrandList;
        }
        return [];
    }

    public static function getStandardBrandInfo($brandId)
    {
        $standardBrandData = \Http::get(config('website.footstone_url') . '/open/getStandardBrandInfo?brand_id=' . $brandId)->json();
        if (isset($standardBrandData['code']) && $standardBrandData['code'] == 0) {
            $standardBrand = $standardBrandData['data'];
            return $standardBrand;
        }
        return [];
    }

}