<?php

namespace App\Services\Timbre;

use App\Consts\ErrorConst;
use App\Facade\Site;
use App\Libs\Utils;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use OSS\Core\OssException;
use OSS\OssClient;

class TimbreService
{

    public function __construct() {

    }

    public function addCategory($data) {
        $category_name = getProp($data, 'category_name');
        $pid = getProp($data, 'pid');
        if ($pid === '') Utils::throwError('20003:参数异常');

        $category = DB::table('mp_timbre_categories')->where('category_name', $category_name)->first();
        if ($category) {
            return Utils::throwError('20003:该分类已存在');
        }

        if ($pid) {
            $category = DB::table('mp_timbre_categories')->where('id', $pid)->first();
            if (!$category) {
                return Utils::throwError('20003:该分类不存在');
            }
        }

        $category_id = DB::table('mp_timbre_categories')->insertGetId([
            'category_name' => $category_name,
            'pid' => $pid,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        if (!$category_id) Utils::throwError('20003:添加失败');
        return [
            'category_id'       => $category_id,
            'category_name'     => $category_name,
            'pid'               => $pid,
        ];
    }

    public function editCategory($data) {
        $category_id = getProp($data, 'category_id');
        $category_name = getProp($data, 'category_name');

        $category = DB::table('mp_timbre_categories')->where('id', $category_id)->first();
        if (!$category) {
            return Utils::throwError('20003:该分类不存在');
        }

        $category = DB::table('mp_timbre_categories')->where('category_name', $category_name)->first();
        if ($category && $category->id != $category_id) {
            return Utils::throwError('20003:该分类已存在');
        }

        return DB::table('mp_timbre_categories')->where('id', $category_id)->update([
            'category_name' => $category_name,
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
    }

    public function getCategoryList($data) {
        $category_type = getProp($data, 'category_type', 1);
        $category_id = getProp($data, 'category_id');
        $category_name = getProp($data, 'category_name');
        $pid = getProp($data, 'pid');

        $query = DB::table('mp_timbre_categories')->where('is_enable', 1)->select('*');
        switch ($category_type) {
            case 1:
                $query->where('pid', 0);
                break;
            case 2:
                $query->where('pid', $pid);
                break;
            case 3:
                $query->where('pid', $pid);
                break;
            default:
                break;
        }

        if ($category_id) {
            $query->where('id', $category_id);
        }
        if ($category_name) {
            $query->where('category_name', 'like', "%{$category_name}%");
        }

        return $query->orderBy('id')->paginate();
    }

    public function getTimbreList($data) {
        $timbre_name = getProp($data, 'voice_name');
        $language = getProp($data, 'language');
        $status = getProp($data, 'status');
        $dialect = getProp($data, 'dialect');

        $query = DB::table('mp_timbres')->where('is_enabled', 1)->select('*');

        if ($timbre_name) {
            $query->where('timbre_name', 'like', "%{$timbre_name}%");
        }

        if ($language) {
            $query->where('language', 'like', "%{$language}%");
        }

        if ($dialect) {
            $query->where('dialect', $dialect);
        }

        $result = $query->orderBy('id')->paginate();

        return $result;
    }

    public function timbreCategories($data) {
        $categories = DB::table('mp_timbre_categories')->where('is_enable', 1)->select('id as category_id', 'category_name', 'pid')->get()->map(function ($value) {
            return (array)$value;
        })->toArray();

        $result = buildCategoryTree($categories, 0);
        return $result;
    }

    public function editTimbre($data) {
        $timbre_id = getProp($data, 'voice_id');
        $timbre = DB::table('mp_timbres')->where('id', $timbre_id)->first();
        if (!$timbre) {
            return Utils::throwError('20003:该音色不存在');
        }

        $update_data = [
            'updated_at'    => date('Y-m-d H:i:s'),
        ];

        if (getProp($data, 'first_category_id')) {
            $update_data['first_category_id'] = getProp($data, 'first_category_id');
            $update_data['first_category_name'] = DB::table('mp_timbre_categories')->where('id', getProp($data, 'first_category_id'))->value('category_name');
        }
        if (getProp($data, 'second_category_id')) {
            $update_data['second_category_id'] = getProp($data, 'second_category_id');
            $update_data['second_category_name'] = DB::table('mp_timbre_categories')->where('id', getProp($data, 'second_category_id'))->value('category_name');
        }
        if (getProp($data, 'third_category_id')) {
            $update_data['third_category_id'] = getProp($data, 'third_category_id');
            $update_data['third_category_name'] = DB::table('mp_timbre_categories')->where('id', getProp($data, 'third_category_id'))->value('category_name');
        }
        if (getProp($data, 'label')) $update_data['label'] = getProp($data, 'label');

        return DB::table('mp_timbres')->where('id', $timbre_id)->update($update_data);
    }

    public function getAllBooks($data) {
        $bid = getProp($data, 'bid');
        $book_name = getProp($data, 'book_name');

        $query = DB::table('book_configs')->whereIn('is_on_shelf', [1,2])->select('bid', 'book_name');
        if ($bid) {
            $query->where('bid', $bid);
        }

        if ($book_name) {
            $query->where('book_name', 'like', "%{$book_name}%");
        }

        return $query->orderBy('bid', 'desc')->get()->map(function($value) {
            return (array)$value;
        })->toArray();
    }

    public function timbreGroupList($data) {
        $group_name = getProp($data, 'group_name');
        $label = getProp($data, 'label');

        $query = DB::table('mp_timbre_groups')->select('*');

        if ($group_name) {
            $query->where('group_name', 'like', "%{$group_name}%");
        }

        if ($label) {
            $query->where('label', 'like', $label);
        }

        return $query->orderBy('id')->paginate();
    }

    public function addTimbreGroup($data) {
        $group_name = getProp($data, 'group_name');
        $male_lead_voice_name = getProp($data, 'male_lead_voice_name');
        $male_lead_voice_type = getProp($data, 'male_lead_voice_type');
        $female_lead_voice_name = getProp($data, 'female_lead_voice_name');
        $female_lead_voice_type = getProp($data, 'female_lead_voice_type');
        $narration_voice_name = getProp($data, 'narration_voice_name');
        $narration_voice_type = getProp($data, 'narration_voice_type');
        if (!$group_name || !$male_lead_voice_type || !$female_lead_voice_type || !$narration_voice_type) Utils::throwError('20003:参数错误');
        $label = getProp($data, 'label');
        $other_roles = getProp($data, 'other_roles');
        if (DB::table('mp_timbre_groups')->where('group_name', $group_name)->value('id')) Utils::throwError('20003:该音色组已存在');

        return DB::table('mp_timbre_groups')->insert([
            'group_name'                => $group_name,
            'male_lead_voice_name'      => $male_lead_voice_name,
            'male_lead_voice_type'      => $male_lead_voice_type,
            'female_lead_voice_name'    => $female_lead_voice_name,
            'female_lead_voice_type'    => $female_lead_voice_type,
            'narration_voice_name'      => $narration_voice_name,
            'narration_voice_type'      => $narration_voice_type,
            'label'                     => $label,
            'other_roles'               => $other_roles,
            'created_at'                => date('Y-m-d H:i:s'),
            'updated_at'                => date('Y-m-d H:i:s'),
        ]);
    }

    public function editTimbreGroup($data) {
        $group_id = getProp($data, 'group_id');
        $group_name = getProp($data, 'group_name');
        $male_lead_voice_name = getProp($data, 'male_lead_voice_name');
        $male_lead_voice_type = getProp($data, 'male_lead_voice_type');
        $female_lead_voice_name = getProp($data, 'female_lead_voice_name');
        $female_lead_voice_type = getProp($data, 'female_lead_voice_type');
        $narration_voice_name = getProp($data, 'narration_voice_name');
        $narration_voice_type = getProp($data, 'narration_voice_type');
        if (!$group_id || !$group_name || !$male_lead_voice_type || !$female_lead_voice_type || !$narration_voice_type) Utils::throwError('20003:参数错误');
        $label = getProp($data, 'label');
        $other_roles = getProp($data, 'other_roles');
        $tmp_group_id = DB::table('mp_timbre_groups')->where('group_name', $group_name)->value('id');
        if ($tmp_group_id && $tmp_group_id != $group_id) Utils::throwError('20003:该音色组已存在');

        return DB::table('mp_timbre_groups')->where('id', $group_id)->update([
            'group_name'                => $group_name,
            'male_lead_voice_name'      => $male_lead_voice_name,
            'male_lead_voice_type'      => $male_lead_voice_type,
            'female_lead_voice_name'    => $female_lead_voice_name,
            'female_lead_voice_type'    => $female_lead_voice_type,
            'narration_voice_name'      => $narration_voice_name,
            'narration_voice_type'      => $narration_voice_type,
            'label'                     => $label,
            'other_roles'               => $other_roles,
            'updated_at'                => date('Y-m-d H:i:s'),
        ]);
    }
}
