<?php

namespace app\models\logics\commands;

use app\common\CommonHelper;
use app\enums\SourceEnum;
use app\models\Brand;
use app\models\BrandRunway;
use app\models\BrandRunwayImages;
use app\models\BrandSource;
use app\models\logics\commands\base\BaseCommandSpiderLogic;
use yii\helpers\ArrayHelper;

class SpiderVogue extends BaseCommandSpiderLogic
{
    protected string $baseUrl = 'https://www.vogue.com/';

    public function __construct()
    {
        parent::__construct();
//        $this->curl->setProxy('127.0.0.1', 58591);
    }

    public function setBrandName(string $name = ''): SpiderVogue
    {
        $this->brandName = $name;
        return $this;
    }

    public function start()
    {
        $source = BrandSource::find()->where(['source' => SourceEnum::VOGEU->value()])->asArray()->all();
//        var_dump(ArrayHelper::getColumn($source, 'brand_id'));
        $i = 0;
        $brandNameContainer = [];
        $brandIdContainer = [];
        foreach (Brand::find()->where(
            ['>','id', 5747] // 5897
        )->each() as $item) {
            $item['name'] = strtr($item['name'], [
               ' ' => '-',
               '.' => '-'
            ]);
//            var_dump(strtolower($item['name']));
//            $runwayList = $this->setBrandName($item['name'])->_getBrandRunwayList(strtolower($item['name']));
            echo $item['name'] . PHP_EOL;

                $brandNameContainer[] = strtolower($item['name']);
                $brandIdContainer[] = $item['id'];
                $i++;

                if ($i != 5) {
                    continue;
                }


            $runwayList = $this->_getBrandRunwayListMulti($brandNameContainer);

            foreach ($runwayList as $index => $listItem) {
//                var_dump('id---', $brandIdContainer[$index]);
//                var_dump($listItem);
                if (!$listItem) {
                    continue;
                }
                if (!$sourceModel = BrandSource::find()->where(['brand_id' => $brandIdContainer[$index], 'is_deleted' => 0])->one()) {
                    $sourceModel = new BrandSource();
                    $sourceModel->brand_id = $brandIdContainer[$index];
                    $sourceModel->source = SourceEnum::VOGEU->value();
                    $sourceModel->save();
                    var_dump($sourceModel->errors);
                }
            }

            $brandNameContainer = $brandIdContainer = [];
            $i = 0;

            if (!$runwayList) {
                continue;
            }


            var_dump($runwayList);
            foreach ($runwayList as $runwayItem) {
                var_dump($runwayItem['hed']);
                if (!$model = BrandRunway::find()->where(['brand_id' => $item['id'], 'title' => $runwayItem['hed']])->one()) {
                    echo $runwayItem['hed'] . PHP_EOL;
                    $model = new BrandRunway();
                    $model->title = $runwayItem['hed'];
                    $model->brand_id = $item['id'];
                    $model->year = CommonHelper::getYear($runwayItem['hed']);
                    $model->cover = '';
                    $model->save();
                    var_dump($model->errors);
                    if (!$sourceModel = BrandSource::find()->where(['brand_id' => $item['id'], 'is_deleted' => 0])->one()) {
                        $sourceModel = new BrandSource();
                        $sourceModel->brand_id = $item['id'];
                        $sourceModel->source = SourceEnum::VOGEU->value();
                        $sourceModel->save();
                        var_dump($sourceModel->errors);
                    }

                    $pageUri = $runwayItem['url'];
                    $requestUrl = rtrim($this->baseUrl, '/') . $pageUri . '/slideshow/collection';
                    $detailData = $this->_getDetail($requestUrl);

                    preg_match_all('/window\.__PRELOADED_STATE__ = (.*?);</s', $detailData, $matches);

                    $saveUrl = $detailUrl = [];
                    if (count($matches) > 1) {
                        $val = json_decode(current($matches[1]), true);
                        $images = $val['transformed']['runwayGalleries']['galleries'][0]['items'] ?? false;

                        if ($images === false) {
                            echo '获取图片失败' . PHP_EOL;
//                            $this->logger->warning($requestUrl . '获取图片失败.');
                            return;
                        }

                        foreach (is_array($images) ? $images : [] as $img) {
                            $saveUrl[] = [
                                'src' => $img['image']['sources']['xxl']['url']
                            ];
                            foreach ($img['details'] ?? [] as $detail) {
                                $detailUrl[] = ['src' => $detail['image']['sources']['xxl']['url']];
                                $brandModel = new BrandRunwayImages();
                                $brandModel->image = $detail['image']['sources']['xxl']['url'];
                                $brandModel->runway_id = $model->id;
                                $brandModel->brand_id = $item['id'];
                                $brandModel->save();
                                var_dump($brandModel->errors);
                            }
                        }

//                        $model->images = json_encode($saveUrl);
                        $model->cover = '';
                        $model->save();
                    }
                }
            }
        }
    }

    public function getDetail($url): bool|string
    {
        return $this->curl->setUrl($url)->exec();
    }


    public function getBrandRunwayList(string $url)
    {
        $request = $this->curl->setUrl($url)->exec();

        if ($request) {
            preg_match_all('/window.__PRELOADED_STATE__ = ([\s\S]*?);<\/script>/', $request, $matches);
            $val = json_decode(current(end($matches)), true);
            return $val['transformed']['runwayDesignerContent']['designerCollections'] ?? [];
        } else {
//            $this->logger->info('未找到数据.');
            return [];
        }
    }

    private function _getBrandRunwayListMulti(array $brandNames = []): array
    {
//        $brandNames = [];
        $resp = [];
        foreach ($brandNames as $brandName) {
            $url[] = rtrim($this->baseUrl, '/') . '/fashion-shows/designer/' . $brandName;
//        var_dump($url);
//        list($request, $httpCode) = $curl->setUrl($url)->exec();

        }

        $request = $this->curl->execMulti($url);
//        var_dump($request);die;
        foreach ($request as $item) {
            if ($item) {
                preg_match_all('/window.__PRELOADED_STATE__ = ([\s\S]*?);<\/script>/', $item, $matches);
                $val = json_decode(current(end($matches)), true);
                $resp[] = $val['transformed']['runwayDesignerContent']['designerCollections'] ?? [];
            } else {
//            $this->logger->info('未找到数据.');
                $resp[] = [];
            }
        }

        return $resp;
    }

}