<?php

namespace App\Http\Controllers\V2_17_3\Friends;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Managers\FriendManager;
use App\Services\V2_17_3\Friends\ApplyService;
use Illuminate\Http\Request;

class ApplyController extends Controller
{
    /**
     * 全部申请列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @deprecated Friend/GetApplyAllList
     */
    public function getAllList(Request $request)
    {
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $request->get('page', 1)
        );

        $as = new FriendManager();
        $data = $as->getApplyAllList($uid, $pages);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 发出申请列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @deprecated Friend/GetApplySendList
     */
    public function getSendList(Request $request)
    {
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $request->get('page', 1)
        );

        $as = new FriendManager();
        $data = $as->getSendList($uid, $pages);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 收到申请列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @deprecated Friend/GetApplyReceiveList
     */
    public function getReceiveList(Request $request)
    {
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $request->get('page', 1)
        );

        $as = new FriendManager();
        $data = $as->getReceiveList($uid, $pages);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 发起好友申请
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     */
    public function send(Request $request)
    {
        $uid = Auth::auth();

        $this->validate($request, [
            'invite_uid' => 'required|integer', // 被申请用户
            'question' => 'required'            // 问答
        ]);

        $to_uid = $request->post('invite_uid');
        $question = $request->post('question', []);
        $as = new ApplyService();
        if ($as->send($uid, $to_uid, $question)) {
            return response(array(
                'code' => 200,
                'message' => 'success'
            ));
        } else {
            return response(array(
                'code' => 401,
                'message' => '发送失败'
            ));
        }
    }

    /**
     * 同意好友申请
     * @param  int  $apply_uid
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \App\Exceptions\AlertException
     */
    public function agree(int $apply_uid)
    {
        $uid = Auth::auth();
        $as = new ApplyService();
        $as->agree($uid, $apply_uid);
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }
}
