<?php

namespace App\Http\Controllers\Gnight;

use App\Models\Gnight\GetVoiceModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Miniprogram\Auth;
use App\Models\Gnight\UserModel;
use App\Models\User\UserModel as AuthUserModel;

class User extends Controller
{
    /**
     * @api {get} /api/gnight/user 用户信息
     * @apiName User
     * @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 联系方式
     */
    public function user()
    {
        $uid = Auth::auth();
        $userModel = new UserModel();
        $user = $userModel->find(
            $uid,
            ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact']
        );
        if (collect($user)->isEmpty()) {
            $authUser = AuthUserModel::find($uid);
            $user = $userModel->fill([
                'uid' => $uid,
                'headimgurl' => $authUser->headimgurl,
                'nickname' => $authUser->nickname,
                'sex' => 0,
                'like' => 0,
                'voice' => null,
                'voice_state' => 0,
                'contact_type' => 0,
                'contact' => null
            ]);
            $user->save();
        }
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $user
        );
    }

    /**
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @api {put} /api/gnight/user 更新用户信息
     * @apiName UpdateUser
     * @apiGroup Gnight
     *
     * @apiParam {string} headimgurl 头像
     * @apiParam {string} nickname 昵称
     * @apiParam {int} sex 性别0未知1男2女
     * @apiParam {int} like 喜欢0所有1男2女
     * @apiParam {string} voice 语音
     * @apiParam {int} contact_type 联系方式0:电话1:微信;2:qq
     * @apiParam {string} contact 联系方式
     */
    public function update(Request $request)
    {
        $uid = Auth::auth();
        $user = UserModel::findOrFail($uid);
        $data = $request->toArray();
        if (isset($data['voice']) && !empty($data['voice'])) {
            $data['voice_state'] = 2;
        }
        $user->fill($data);
        $user->save();
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    /**
     * @api {get} /api/gnight/user/voices 我的语音
     * @apiName  user/voices
     * @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:审核;3:用户下架
     * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
     * @apiSuccess {string} contact 联系方式,未解锁时无此字段
     * @apiSuccess {int} lock 解锁进度
     */
    public function voices()
    {
        $uid = Auth::auth();
        $getVoiceModel = new GetVoiceModel();
        $voices = $getVoiceModel->voicesByUser($uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $voices
        );
    }
}
