<?php

namespace App\Http\Services\V1;

use App\Generated\V1\Messages\Friend\GetApplyAllListMessage;
use App\Generated\V1\Messages\Friend\GetApplyReceiveListMessage;
use App\Generated\V1\Messages\Friend\GetApplySendListMessage;
use App\Generated\V1\Messages\Friend\GetEachLikeListMessage;
use App\Generated\V1\Messages\Friend\GetStarListMessage;
use App\Generated\V1\Messages\Friend\GetChatHistoryMessage;
use App\Generated\V1\Messages\Friend\GetUnreadCountMessage;
use App\Generated\V1\Messages\Friend\AgreeApplyMessage;
use App\Generated\V1\Messages\Friend\GetAllListMessage;
use App\Http\Controllers\Core\Auth;
use App\Managers\FriendManager;
use App\Services\V2_17_3\Friends\ApplyService;

class FriendService
{
    protected $friendManager;

    public function __construct(FriendManager $friendManager)
    {
        $this->friendManager = $friendManager;
    }

    public function getAllList(GetAllListMessage $message)
    {
        $page = $message->getPage();

        $uid = Auth::auth();

        $data = $this->friendManager->getAllList($uid, [
            'limit' => 20,
            'page' => $page,
        ]);

        $message->setResponse($data);
    }

    public function getApplyAllList(GetApplyAllListMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page,
        );

        $data = $this->friendManager->getApplyAllList($uid, $pages);

        $message->setResponse($data);
    }

    public function getApplyReceiveList(GetApplyReceiveListMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page
        );

        $data = $this->friendManager->getReceiveList($uid, $pages);

        $message->setResponse($data);
    }

    public function getApplySendList(GetApplySendListMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page
        );

        $data = $this->friendManager->getSendList($uid, $pages);

        $message->setResponse($data);
    }

    public function getEachLikeList(GetEachLikeListMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page
        );

        $data = $this->friendManager->getEachLikeList($uid, $pages);

        $message->setResponse($data);
    }

    public function getStarList(GetStarListMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page
        );

        $data = $this->friendManager->getStarList($uid, $pages);

        $message->setResponse($data);
    }

    public function getChatHistory(GetChatHistoryMessage $message)
    {
        $page = $message->getPage() ?: 1;
        $friendUid = $message->getFriendUid();
        $uid = Auth::auth();

        $pages = array(
            'limit' => 20,
            'page' => $page
        );

        $data = $this->friendManager->getHistory($uid, $friendUid, $pages);

        $message->setResponse($data);
    }

    public function getUnreadCount(GetUnreadCountMessage $message)
    {
        $uid = Auth::auth();

        $data = $this->friendManager->getUnreadCount($uid);

        $message->setResponse($data);
    }

    public function agreeApply(AgreeApplyMessage $message)
    {
        $uid = Auth::auth();
        $applyUid = $message->getApplyUid();
        $as = new ApplyService();
        $as->agree($uid, $applyUid);
    }
}
