<?php

namespace App\Http\Services\V1_1;

use App\Exceptions\AlertException;
use App\Generated\V1_1\Messages\Share\PairApplyListShareHelpMessage;
use App\Generated\V1_1\Messages\Share\PairVipCheckMessage;
use App\Generated\V1_1\Messages\Share\PairGetMessage;
use App\Managers\LikeManager;
use App\Managers\SharePairManager;
use App\Http\Controllers\Core\Auth;
use App\Generated\V1_1\Messages\Share\LikeMeMessage;
use App\Generated\V1_1\Messages\Share\LikeMeCheckMessage;
use App\Generated\V1_1\Messages\Share\LikeMeHistoryMessage;
use App\Models\Fpdx\ActivityModel;
use App\Models\Fpdx\PairModel;
use App\Models\Share\PairApplyListShareHelpLog;

class ShareService
{
    protected $sharePairManager;
    protected $likeManager;
    public function __construct(SharePairManager $sharePairManager, LikeManager $likeManager)
    {
        $this->sharePairManager = $sharePairManager;
        $this->likeManager = $likeManager;
    }
    public function pairVipCheck(PairVipCheckMessage $message)
    {
        $inviteId = $message->getInviteId();
        $uid = Auth::auth();
        $data = $this->sharePairManager->vipCheck($uid, $inviteId);
        $message->setResponse($data);
    }
    public function pairGet(PairGetMessage $message)
    {
        $listId = $message->getListId();
        $data = $this->sharePairManager->get($listId);
        $message->setResponse($data);
    }
    public function likeMe(LikeMeMessage $message)
    {
        $praiseId = $message->getPraiseId();
        $data = $this->likeManager->get($praiseId);
        $message->setResponse($data);
    }
    public function likeMeCheck(LikeMeCheckMessage $message)
    {
        $praiseId = $message->getPraiseId();
        $uid = Auth::auth();
        $data = $this->likeManager->check($uid, $praiseId);
        $message->setResponse($data);
    }
    public function likeMeHistory(LikeMeHistoryMessage $message)
    {
        $praiseId = $message->getPraiseId();
        $data = $this->likeManager->history($praiseId);
        $message->setResponse($data);
    }
    public function pairApplyListShareHelp(PairApplyListShareHelpMessage $message)
    {
        $uid = Auth::auth();
        $stageId = $message->getStageId();
        $shareUid = $message->getShareUid();
        $lockUid = $message->getLockUid();

        $activity = ActivityModel::enrolling()->first();
        if (
            !$activity || $activity->stage_id != $stageId
        ) {
            throw new AlertException("活动报名已结束，分享已过期");
        }

        if (
            !$sharePair = PairModel::where([
                array('stage_id', $stageId), array('uid', $shareUid)
            ])->first()
        ) {
            throw new AlertException("分享者取消报名，分享失效");
        }

        if (
            !$sharePair = PairModel::where([
                array('stage_id', $stageId), array('uid', $lockUid)
            ])->first()
        ) {
            throw new AlertException("解锁者取消报名，分享失效");
        }

        if (
            PairApplyListShareHelpLog::where([
                array('help_uid', $uid), array('share_uid', $shareUid), array('created_at', '>', mkdir(0, 0, 0))
            ])->exists()
        ) {
            throw new AlertException("一天只可帮一个人助力一次");
        }

        if (
            PairApplyListShareHelpLog::where([
                array('help_uid', $uid), array('share_uid', $shareUid), array('lock_uid', $lockUid), array('stage_id', $stageId)
            ])->exists()
        ) {
            throw new AlertException("你已经帮他助力了");
        }

        PairApplyListShareHelpLog::create([
            'created_at' => time(),
            'share_uid' => $shareUid,
            'lock_uid' => $lockUid,
            'stage_id' => $stageId,
            'help_uid' => $uid
        ]);
    }
}
