<?php

namespace App\Transformer\Timbre;

class TimbreTransformer
{
    // 分类列表
    public function newBuildCategoryList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachCategoryList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'category_id'           => getProp($item, 'id'),
                'category_name'         => getProp($item, 'category_name'),
                'created_at'            => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }

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

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

        foreach ($list as $item) {
            $result[] = [
                'voice_id'              => getProp($item, 'id'),
                'voice_name'            => str_replace('（多情感）', '', getProp($item, 'timbre_name')),
                'voice_type'            => getProp($item, 'timbre_type'),
                'timbre_url'            => getProp($item, 'timbre_url'),
                'language'              => getProp($item, 'language'),
                'dialect'               => getProp($item, 'dialect'),
                'emotion'               => getProp($item, 'emotion'),
                'first_category_id'     => getProp($item, 'first_category_id'),
                'first_category_name'   => getProp($item, 'first_category_name'),
                'second_category_id'    => getProp($item, 'second_category_id'),
                'second_category_name'  => getProp($item, 'second_category_name'),
                'third_category_id'     => getProp($item, 'third_category_id'),
                'third_category_name'   => getProp($item, 'third_category_name'),
                'audio_url'             => getProp($item, 'audio_url'),
                'label'                 => getProp($item, 'label'),
                'created_at'            => transDate(getProp($item, 'created_at')),
            ];
        }

        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) {
            $result[] = [
                '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, 'audio_url'),
                'remark'            => getProp($item, 'remark'),
            ];
        }

        return $result;
    }

    // 音色组列表
    public function newBuildTimbreGroupList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachTimbreGroupList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'group_id'                  => getProp($item, 'id'),
                'group_name'                => getProp($item, 'group_name'),
                'label'                     => getProp($item, 'label'),
                'male_lead_voice_name'      => getProp($item, 'male_lead_voice_name'),
                'male_lead_voice_type'      => getProp($item, 'male_lead_voice_type'),
                'female_lead_voice_name'    => getProp($item, 'female_lead_voice_name'),
                'female_lead_voice_type'    => getProp($item, 'female_lead_voice_type'),
                'narration_voice_name'      => getProp($item, 'narration_voice_name'),
                'narration_voice_type'      => getProp($item, 'narration_voice_type'),
                'other_roles'               => getProp($item, 'other_roles') ? json_decode(getProp($item, 'other_roles'), true) : [],
                'created_at'                => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }

    // 音色情感组列表
    public function newBuildEmotionGroupList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachEmotionGroupList($data),
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'group_id'              => getProp($item, 'id'),
                'group_name'            => getProp($item, 'group_name'),
                'emotion'               => getProp($item, 'emotion'),
                'emotion_type'          => getProp($item, 'emotion_type'),
                'voice_type'            => getProp($item, 'voice_type'),
                'voice_name'            => getProp($item, 'voice_name'),
                'speed_ratio'           => getProp($item, 'speed_ratio'),
                'loudness_ratio'        => getProp($item, 'loudness_ratio'),
                'emotion_scale'         => getProp($item, 'emotion_scale'),
                'pitch'                 => getProp($item, 'pitch'),
                'generate_status'       => getProp($item, 'generate_status'),
                'audio_url'             => getProp($item, 'audio_url'),
                'error_msg'             => getProp($item, 'error_msg'),
                'created_at'            => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }
}
