<?php

namespace App\Transformer\Book;

use App\Models\Book\Book;
use Vinkla\Hashids\Facades\Hashids;

class BookTransformer
{
    // 书籍列表
    public function newBuildBookList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachBookList($data->items())
        ];
    }

    private function newEachBookList($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'bid'                       => getProp($item, 'id'),
                'book_name'                 => getProp($item, 'book_name'),
                'origin_book_name'          => getProp($item, 'origin_book_name') ? getProp($item, 'origin_book_name') : getProp($item, 'name'),
                'intro'                     => filterIntro(getProp($item, 'intro')),
                'cover'                     => addPrefix(getProp($item, 'cover')),
                'author'                    => getProp($item, 'author'),
                'category_name'             => getProp($item, 'category_name') ? getProp($item, 'category_name') : '其他',
                'ncategory_name'            => getProp($item, 'ncategory_name') ? getProp($item, 'category_name') : '',
                'gender'                    => getProp($item, 'gender'),
                'status'                    => getProp($item, 'status') == 1 ? '已完结' : '连载中',
                'chapter_count'             => getProp($item, 'chapter_count'),
                'size'                      => getProp($item, 'size'),
                'roles'                     => getProp($item, 'roles'),
                'charge_type'               => getProp($item, 'charge_type') == 'CHAPTER' ? '长篇' : '短篇',
                'created_at'                => transDate(getProp($item, 'created_at')),
                'book_calculate_price_type' => getProp($item, 'book_calculate_price_type'),
                'book_coin'                 => getProp($item, 'book_coin'),
            ];
        }

        return $result;
    }

    // 章节目录
    public function newBuildChapterList($data): array
    {
        return [
            'meta' => getMeta($data['chapters']),
            'list' => $this->newEachChapterList($data['chapters']->items(), $data['book']['feed_advertise_seq'], $data['book']['follow_seq']),
            'book' => $data['book']
        ];
    }

    private function newEachChapterList($list, $feed_advertise_seq, $follow_seq): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $sequence = getProp($item, 'sequence');
            $result[] = [
                'cid'               => getProp($item, 'id'),
                'chapter_name'      => getProp($item, 'name'),
                'size'              => getProp($item, 'charge_length') ? getProp($item, 'charge_length') : getProp($item, 'size'),
                'is_vip'            => getProp($item, 'is_vip'),
                'is_feed_advertise' => $feed_advertise_seq == $sequence ? 1 : 0,
                'is_follow'         => $follow_seq == $sequence ? 1 : 0,
            ];
        }

        return $result;
    }

    // 分类书籍
    public function newBuildCategoryBooks($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachCategoryBooks($data->items()),
        ];
    }

    private function newEachCategoryBooks($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'bid'           => Hashids::encode(getProp($item, 'id')),
                'book_name'     => getProp($item, 'name'),
                'intro'         => filterIntro(getProp($item, 'intro')),
                'cover'         => addPrefix(getProp($item, 'cover')),
                'author'        => getProp($item, 'author'),
                'category_name' => getProp($item, 'category_name') ? getProp($item, 'category_name') : '其他',
                'gender'        => getProp($item, 'gender'),
            ];
        }

        return $result;
    }
}
