<?php

namespace App\Transformer\Book;

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

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

        foreach ($list as $item) {
            $result[] = [
                'bid'                       => getProp($item, 'bid'),
                'book_name'                 => getProp($item, 'book_name'),
                'origin_book_name'          => getProp($item, 'origin_book_name') ? getProp($item, 'origin_book_name') : getProp($item, 'book_name'),
                'status'                    => getProp($item, 'status'),
                'status_info'               => getProp($item, 'status') == 1 ? '已完结' : '连载中',
                'version_count'             => getProp($item, 'version_count'),
            ];
        }

        return $result;
    }

    // 章节列表
    public function newBuildChapterList($data): array
    {
        return [
            'meta'      => getMeta($data['list']),
            'header'    => $data['header'],
            'list'      => $this->newEachChapterList($data['list']),
        ];
    }

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

        foreach ($list as $item) {
            $subtitle_info = getProp($item, 'subtitle_info');
            if ($subtitle_info) $subtitle_info = json_decode($subtitle_info, true);
            else $subtitle_info = [];
            $result[] = [
                'audio_id'              => getProp($item, 'id'),
                'cid'                   => getProp($item, 'cid'),
                'chapter_name'          => getProp($item, 'chapter_name'),
                'sequence'              => getProp($item, 'sequence'),
                'size'                  => getProp($item, 'size'),
                'generate_status'       => getProp($item, 'generate_status'),
                'audio_url'             => getProp($item, 'complete_audio_url') ? getProp($item, 'complete_audio_url') : getProp($item, 'audio_url'),
                'audio_effect_status'   => getProp($item, 'audio_effect_status'),
                'bgm_status'            => getProp($item, 'bgm_status'),
                'remark'                => getProp($item, 'remark'),
                'complete_audio_url'    => getProp($item, 'complete_audio_url'),
                'subtitle_info'         => $subtitle_info
            ];
        }

        return $result;
    }

    // 版本列表
    public function newBuildVersionList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachVersionList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'version_id'        => getProp($item, 'id'),
                'version_name'      => getProp($item, 'version_name'),
                'bid'               => getProp($item, 'bid'),
                'book_name'         => getProp($item, 'book_name'),
                'created_at'        => getProp($item, 'created_at'),
            ];
        }

        return $result;
    }

    // 合成任务列表
    public function newBuildTaskList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachTaskList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'task_id'           => getProp($item, 'id'),
                'task_name'         => getProp($item, 'task_name'),
                'generate_status'   => getProp($item, 'generate_status'),
                'bid'               => getProp($item, 'bid'),
                'version_id'        => getProp($item, 'version_id'),
                'cid'               => getProp($item, 'cid'),
                'created_at'        => getProp($item, 'created_at'),
            ];
        }

        return $result;
    }

    // 音效列表
    public function newBuildAudioEffectList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachAudioEffectList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'audio_effect_id'   => getProp($item, 'id'),
                'audio_effect_name' => getProp($item, 'audio_effect_name'),
                'audio_effect_url'  => getProp($item, 'audio_effect_url'),
                'playtime_seconds'  => getProp($item, 'playtime_seconds'),
                'created_at'        => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }

    // bgm列表
    public function newBuildBgmList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachBgmList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'bgm_id'            => getProp($item, 'id'),
                'bgm_name'          => getProp($item, 'bgm_name'),
                'bgm_url'           => getProp($item, 'bgm_url'),
                'playtime_seconds'  => getProp($item, 'playtime_seconds'),
                'created_at'        => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }
}
