<?php

namespace app\models\jobs;

use app\common\CommonHelper;
use app\enums\SourceEnum;
use app\models\BrandRunway;
use app\models\BrandRunwayImages;
use app\models\BrandSource;
use app\models\jobs\base\BaseJob;
use app\models\logics\commands\SpiderVogue;
use yii\debug\models\search\Db;
use yii\queue\Queue;

class RunwayJob extends BaseJob
{
    public BrandSource|null $source = null;

    public function execute($queue)
    {
        $spiderModel = new SpiderVogue();
        $runwayList = ($spiderModel->getBrandRunwayList(rtrim(SourceEnum::VOGEU->baseUrl(), '/') . $this->source->source_url));
        echo '获取到了 --> ' . count($runwayList ?: []) . '篇文章' . PHP_EOL;
        try {
            foreach ($runwayList as $runwayItem) {
                $tr = \Yii::$app->db->beginTransaction();
                echo "正在处理{$runwayItem['hed']}" . PHP_EOL;
                if (!$model = BrandRunway::find()->where(['brand_id' => $this->source->brand_id, 'title' => $runwayItem['hed']])->one()) {
                    $model = new BrandRunway();
                    $model->title = $runwayItem['hed'];
                    $model->brand_id = $this->source->brand_id;
                    $model->year = CommonHelper::getYear($runwayItem['hed']);
                    $model->cover = '';
                    $model->save();
                    var_dump($model->errors);
                    if (!$sourceModel = BrandSource::find()->where(['brand_id' => $this->source->brand_id, 'is_deleted' => 0])->one()) {
                        $sourceModel = new BrandSource();
                        $sourceModel->brand_id = $this->source->brand_id;
                        $sourceModel->source = SourceEnum::VOGEU->value();
                        $sourceModel->save();
                        var_dump($sourceModel->errors);
                    }

                    $pageUri = $runwayItem['url'];
                    $requestUrl = rtrim(SourceEnum::VOGEU->baseUrl(), '/') . $pageUri . '/slideshow/collection';
                    $detailData = $spiderModel->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 . '获取图片失败.');
                            $tr->rollBack();
                            continue;
//                            return;
                        }
                        echo "文章图片数量" . count($images) . PHP_EOL;
                        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 = $img['image']['sources']['xxl']['url'];
                                $brandModel->runway_id = $model->id;
                                $brandModel->brand_id = $this->source->brand_id;
                                $brandModel->save();
                                var_dump($brandModel->errors);
//                            }
                        }

//                        $model->images = json_encode($saveUrl);
                        $model->image_count = count($saveUrl);
                        $model->cover = current($saveUrl)['src'];
                        $model->source_url = $requestUrl;
                        $model->save();
                    }
                }

                $tr->commit();
            }
        }catch (\Throwable $exception) {
            var_dump($exception->getMessage());
        }
        // TODO: Implement execute() method.
    }
}