<?php

namespace App\Http\Controllers\Gnight;

use App\Exceptions\AlertException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Gnight\GetVoiceModel;
use App\Models\Gnight\UserModel;
use App\Http\Controllers\Miniprogram\Auth;

class Voice extends Controller
{
    public static function __callStatic($name, $args)
    {
        $getVoice = new GetVoiceModel();
        return call_user_func_array([$getVoice, $name], $args);
    }

    /**
     * @param  int  $voice_id
     * @return array
     * @throws \ApiException
     * @api {get} /api/gnight/voices/:voice_id 语音信息
     * @apiName  get
     * @apiGroup Gnight
     *
     * @apiSuccess {int} uid 用户id
     * @apiSuccess {string} headimgurl 头像
     * @apiSuccess {string} nickname 昵称
     * @apiSuccess {int} sex 性别0未知1男2女
     * @apiSuccess {int} like 喜欢0所有1男2女
     * @apiSuccess {string} voice 语音
     * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
     * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
     * @apiSuccess {string} contact 联系方式,未解锁时无此字段
     * @apiSuccess {int} lock 解锁进度
     *
     */
    public function get(int $voice_id)
    {
        try {
            $uid = Auth::auth();
            $getVoice = GetVoiceModel::where([
                ['uid', $uid],
                ['get_uid', $voice_id]
            ])->first();
            if (collect($getVoice)->isEmpty()) {
                throw new \ApiException("你没有获得这个语音", 401);
            }
            $user = UserModel::findOrFail(
                $voice_id,
                ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact']
            );
            if ($getVoice->lock < 3) {
                unset($user->contact);
            }
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $user
            );
        } catch (\Exception $e) {
            throw new \ApiException($e->getMessage(), intval($e->getCode()));
        }
    }

    /**
     * @return array
     * @throws \ApiException
     * @api {get} /api/gnight/voices/getbyshare 群分享获取语音
     * @apiName  getbyshare
     * @apiGroup Gnight
     *
     * @apiSuccess {int} uid 语音id
     * @apiSuccess {string} headimgurl 头像
     * @apiSuccess {string} nickname 昵称
     * @apiSuccess {int} sex 性别0未知1男2女
     * @apiSuccess {int} like 喜欢0所有1男2女
     * @apiSuccess {string} voice 语音
     * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
     * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
     * @apiSuccess {int} lock 解锁进度
     *
     */
    public function getByShare()
    {
        try {
            $uid = Auth::auth();
            $user = UserModel::findOrFail($uid);
            if ($user->share_group >= 1) {
                throw new \ApiException("分享次数已达上限", 101);
            }
            $getVoiceModel = new GetVoiceModel();
            $voice_id = $getVoiceModel->pop($uid);
            $getVoice = $getVoiceModel->fill([
                'uid' => $uid,
                'get_uid' => $voice_id
            ]);
            $getVoice->save();
            UserModel::where('uid', $uid)->increment('share_group', 1);
            $voice = UserModel::findOrFail(
                $voice_id,
                ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type']
            );
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => $voice
            );
        } catch (\Exception $e) {
            throw new \ApiException($e->getMessage(), intval($e->getCode()));
        }
    }

    /**
     * @return array
     * @throws AlertException
     * @throws \ApiException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @api {get} /api/gnight/voices/getbyfirst 获取语音
     * @apiDescription 首次上次语音获取语音
     * @apiName  getbyfirst
     * @apiGroup Gnight
     *
     * @apiSuccess {int} uid 语音id
     * @apiSuccess {string} headimgurl 头像
     * @apiSuccess {string} nickname 昵称
     * @apiSuccess {int} sex 性别0未知1男2女
     * @apiSuccess {int} like 喜欢0所有1男2女
     * @apiSuccess {string} voice 语音
     * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
     * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
     * @apiSuccess {int} lock 解锁进度
     *
     */
    public function getByFirst()
    {
        $uid = Auth::auth();
        $user = UserModel::findOrFail($uid);
        if ($user->first_voice >= 1) {
            throw new AlertException("你已领取过", 101);
        }
        $getVoiceModel = new GetVoiceModel();
        $voice_id = $getVoiceModel->pop($uid);
        $getVoice = $getVoiceModel->fill([
            'uid' => $uid,
            'get_uid' => $voice_id
        ]);
        $getVoice->save();
        UserModel::where('uid', $uid)->increment('first_voice', 1);
        $voice = UserModel::findOrFail(
            $voice_id,
            ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type']
        );
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $voice
        );
    }

    /**
     * @param  int  $voice_id
     * @return array
     * @api {get} /api/gnight/voices/getshare/:voince_id 获取分享语音信息
     * @apiName getshare
     * @apiGroup Gnight
     *
     * @apiSuccess {int} uid 用户id
     * @apiSuccess {string} headimgurl 头像
     * @apiSuccess {string} nickname 昵称
     * @apiSuccess {int} sex 性别0未知1男2女
     * @apiSuccess {string} voice 语音
     */
    public function getShare(int $voice_id)
    {
        $userModel = new UserModel();
        $user = $userModel->findOrFail(
            $voice_id,
            ['uid', 'headimgurl', 'nickname', 'sex', 'voice', 'voice_state', 'contact_type']
        );
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $user
        );
    }

    /**
     * @param  int  $voice_id
     * @return array
     * @throws \ApiException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @api {get} /api/gnight/voices/getvoice/:voince_id 获取分享语音
     * @apiName getvoice
     * @apiGroup Gnight
     *
     * @apiSuccess {int} code 200
     * @apiSuccess {string} message success
     * @apiSuccess {int} data.get_voice_id 获取语音记录的id
     */
    public function getVoice(int $voice_id)
    {
        $uid = Auth::auth();
        $userModel = new UserModel();
        $user = UserModel::findOrFail(
            $voice_id,
            ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact']
        );
        if ($uid == $user->uid) {
            throw new \ApiException("不能获取自己的语音", 105);
        }
        $getVoiceModel = new GetVoiceModel();
        $getVoice = $getVoiceModel->where([
            ['uid', $uid],
            ['get_uid', $voice_id]
        ])->first();
        if (collect($getVoice)->isEmpty()) {
            $getVoiceModel = new GetVoiceModel();
            $getVoice = $getVoiceModel->fill([
                'uid' => $uid,
                'get_uid' => $user->uid
            ]);
            $getVoice->save();
        }
        $user->get_voice_id = $getVoice->id;
        $user->lock = $getVoice->lock ?? 0;
        if ($user->lock < 3) {
            unset($user->contact);
        }
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $user,
        );
    }

    /**
     * 修改语音状态
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function updateState(Request $request)
    {
        $uid = Auth::auth();
        $userModel = new UserModel();
        $user = UserModel::findOrFail($uid);
        $user->voice_state = $request->input('voice_state');
        if ($user->save()) {
            return array(
                'code' => 200,
                'message' => 'success'
            );
        }
    }
}
