<?php

namespace App\Http\Controllers\Goodnight;

use App\Http\Controllers\Miniprogram\Auth;
use App\Models\Goodnight\TopicExampleModel;
use App\Models\Goodnight\TopicModel;
use App\Services\Goodnight\VoiceService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Exceptions\JWTException;

class VoiceController extends Controller
{
    /**
     * 创建声音
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\DBException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function store(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'cover' => 'required',
            'voice' => 'required'
        ]);
        $gvService = new VoiceService();
        $data = $request->only(['cover', 'voice', 'topic_id', 'topic_example_id']);
        $data['uid'] = $uid;
        $id = $gvService->create($data);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'voice_id' => $id
            ]
        );
    }

    /**
     * 删除声音
     * @param  int  $voice_id
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\DBException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function delete(int $voice_id)
    {
        $uid = Auth::auth();
        $gvService = new VoiceService();
        $gvService->delete($uid, $voice_id);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * 点赞声音
     * @param  int  $voice_id
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\DBException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function thumb(int $voice_id)
    {
        $uid = Auth::auth();
        $gvService = new VoiceService();
        $gvService->thumb($uid, $voice_id);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * 标记声音
     * @param  int  $voice_id
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\DBException
     */
    public function tag(int $voice_id, Request $request)
    {
        try {
            $uid = Auth::auth();
        } catch (JWTException $e) {
            $uid = 0;
        }
        $this->validate($request, [
            'tag' => 'required'
        ]);
        $gvService = new VoiceService();
        $data = array(
            'voice_id' => $voice_id,
            'tag' => $request->post('tag')
        );
        $data = $gvService->tag($uid, $data);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'goodnight_coin' => $data
            ]
        );
    }

    /**
     * 用户端获取声音信息
     * @param  int  $voice_id
     * @return array
     */
    public function get(int $voice_id)
    {
        try {
            $uid = Auth::auth();
        } catch (JWTException $e) {
            $uid = 0;
        }
        $gvService = new VoiceService();
        $data = $gvService->getVoice($uid, $voice_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 我录制的声音
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function myvoices(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $pre_page = $request->input('pre_page', 20);

        $gvService = new VoiceService();
        $data = $gvService->myVoice($page, $pre_page, $uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'page' => $page,
                'limit' => $pre_page,
                'total' => $data['total'],
                'list' => $data['list']
            ]
        );
    }

    /**
     * 用户录制的声音
     * @param  Request  $request
     * @param  int  $uid
     * @return array
     */
    public function uservoices(Request $request, int $uid)
    {
        $page = $request->get('page') ?? 1;

        $gvService = new VoiceService();
        $data = $gvService->userVoice($page, 20, $uid);
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => [
                'page' => $page,
                'limit' => 20,
                'total' => $data['total'],
                'list' => $data['list']
            ]
        ]);
    }

    /**
     * 后台-列表-审核声音
     * @param  Request  $request
     * @return array
     */
    public function voicelist(Request $request)
    {
        $page = $request->get('page') ?? 1;
        $search = $request->only('check', 'tag', 'search', 'expedit', 'commit');
        $pages = array(
            'page' => $page,
            'limit' => 20
        );
        $gvService = new VoiceService();
        $data = $gvService->checkList($pages, $search);
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => [
                'page' => $page,
                'limit' => 20,
                'total' => $data['total'],
                'list' => $data['list']
            ]
        ]);
    }

    /**
     * 后台-列表-推荐声音
     * @param  int  $voice_id
     * @return array
     * @throws \App\Exceptions\DBException
     */
    public function commit(int $voice_id): array
    {
        $gvService = new VoiceService();
        $gvService->commit($voice_id);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * 后台-列表-审核声音
     * @param  Request  $request
     * @param  int  $voice_id
     * @return array
     * @throws \App\Exceptions\DBException
     */
    public function check(Request $request, int $voice_id): array
    {
        $this->validate($request, [
            'check' => 'required|in:-1,1',
        ]);
        $check = $request->post('check');
        $gvService = new VoiceService();
        $gvService->check($voice_id, $check);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * 后台-列表-标记声音
     * @param  int  $voice_id
     * @return array
     * @throws \App\Exceptions\DBException
     */
    public function admintag(Request $request, int $voice_id): array
    {
        $this->validate($request, [
            'tag' => 'required'
        ]);
        $gvService = new VoiceService();
        $gvService->admintag($voice_id);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * 加急晚安
     * @param  int  $voice_id
     * @return array
     * @throws JWTException
     * @throws \App\Exceptions\AlertException
     */
    public function expeditVoice(int $voice_id)
    {
        $uid = Auth::auth();
        $vs = new VoiceService();
        $vs->expeditVoice($voice_id, $uid);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }


    /**
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function topics()
    {
        $topics = TopicModel::select('id', 'topic')->where('show', '1')->orderBy("sort", "desc")->get();
        foreach ($topics as $topic) {
            $topic->examples = TopicExampleModel::select("id", "content")->where([
                'show' => 1,
                'topic_id' => $topic->id
            ])->get();
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $topics
        ]);
    }
}
