<?php

namespace App\Models\Friends;

use App\Models\Model;

/**
 * App\Models\Friends\FriendModel
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property int $last_at 最后交互时间
 * @property string $last_msg 最后一次交互的消息
 * @property int $star_at 星标时间
 * @property int $uid 用户
 * @property int $friend_uid 好友id
 * @property int $is_hide 是否解除好友并隐藏
 * @property int $is_friend 是否解除好友关系
 * @property int $unfriend_at 解除好友关系的时间
 * @property int $unread_cnt 未读消息数量
 * @property int $each_like 相互心动标签
 * @property int $pair 72h标签
 * @property int $apply 好友申请标签
 * @property int $gteam 开黑标签
 * @property int $goodnight 晚安标签
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereApply($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereEachLike($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereFriendUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereGoodnight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereGteam($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereIsFriend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereIsHide($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereLastAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereLastMsg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel wherePair($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereStarAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereUnfriendAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Friends\FriendModel whereUnreadCnt($value)
 * @mixin \Eloquent
 */
class FriendModel extends Model
{
    protected $table = "kdgx_fpdx_friends";

    protected $dateFormat = 'U';

    public const UPDATED_AT = null;

    protected $fillable = array(
        'created_at',
        'uid',
        'friend_uid',
        'last_at',
        'friend_type',
        'friend_type_data',
        'star_at',
        'is_hide',
        'is_friend',
        'unread_cnt',
        'last_msg',
        'each_like',
        'pair',
        'apply',
        'gteam',
        'goodnight'
    );

    /**
     * 成为好友
     * @param  int  $uid
     * @param  int  $friend_uid
     * @param  string  $type
     */
    public static function friend(int $uid, int $friend_uid, string $type)
    {
        /** @var FriendModel $model */
        $model = self::firstOrNew(
            ['uid' => $uid, 'friend_uid' => $friend_uid],
            ['uid' => $uid, 'friend_uid' => $friend_uid]
        );
        $model->last_at = time();

        /** @var FriendModel $friend */
        $friend = self::firstOrNew(
            ['uid' => $friend_uid, 'friend_uid' => $uid],
            ['uid' => $friend_uid, 'friend_uid' => $uid]
        );
        $friend->last_at = time();

        switch ($type) {
            case "each_like":
                $model->last_msg = "恭喜！你们相互心动啦，已交换联系方式";
                $model->each_like = 1;
                $model->is_friend = 1;
                $model->unfriend_at = 0;

                $friend->is_friend = 1;
                $friend->unfriend_at = 0;
                $friend->last_msg = "恭喜！你们相互心动啦，已交换联系方式";
                $friend->each_like = 1;
                $friend->unread_cnt = $friend->unread_cnt + 1;
                break;
            case "apply":
                $model->last_msg = "你接受了对方的好友申请，成功交换联系方式";
                $model->apply = 1;
                $model->is_friend = 1;
                $model->unfriend_at = 0;

                $friend->is_friend = 1;
                $friend->unfriend_at = 0;
                $friend->last_msg = "对方接受了你的好友申请，成功交换联系方式";
                $friend->apply = 1;
                $friend->unread_cnt = $friend->unread_cnt + 1;

                FriendApplyModel::where([
                    ['uid', $model->uid],
                    ['apply_uid', $model->friend_uid]
                ])->orWhere([
                    ['uid', $model->friend_uid],
                    ['apply_uid', $model->uid]
                ])->update(['is_hide' => 0]);
                break;
            case "pair":
                $model->last_msg = "你们通过72小时CP相识，成为好友";
                $model->pair = 1;
                $model->unread_cnt = $model->unread_cnt + 1;
                $model->is_friend = 1;
                $model->unfriend_at = 0;

                $friend->is_friend = 1;
                $friend->unfriend_at = 0;
                $friend->last_msg = "你们通过72小时CP相识，成为好友";
                $friend->pair = 1;
                $friend->unread_cnt = $friend->unread_cnt + 1;
                break;
            case "goodnight":
                $model->last_msg = "你们通过晚安伴侣相识，成为好友";
                $model->goodnight = 1;
                $model->unread_cnt = $model->unread_cnt + 1;
                $model->is_friend = 1;
                $model->unfriend_at = 0;

                $friend->is_friend = 1;
                $friend->unfriend_at = 0;
                $friend->last_msg = "你们通过晚安伴侣相识，成为好友";
                $friend->goodnight = 1;
                $friend->unread_cnt = $friend->unread_cnt + 1;
                break;
            default:
                $model->last_msg = "";
                $friend->last_msg = "";
        }

        $model->save() && $friend->save();
    }

    /**
     * 拉黑好友
     * @param  int  $uid
     * @param  int  $friend_uid
     * @param  string  $type
     */
    public static function unfriend(int $uid, int $friend_uid, string $type)
    {
        /** @var FriendModel $model */
        $model = self::firstOrNew(
            ['uid' => $uid, 'friend_uid' => $friend_uid],
            ['uid' => $uid, 'friend_uid' => $friend_uid]
        );
        $model->last_at = time();

        /** @var FriendModel $friend */
        $friend = self::firstOrNew(
            ['uid' => $friend_uid, 'friend_uid' => $uid],
            ['uid' => $friend_uid, 'friend_uid' => $uid]
        );
        $friend->last_at = time();

        $model->unfriend_at = time();
        $model->is_friend = 0;
        $model->last_msg = "你取消了心动，好友关系解除";
        $friend->is_friend = 0;
        $friend->last_msg = "对方取消了心动，好友关系解除";
        $friend->unread_cnt = $friend->unread_cnt + 1;

        $model->save() && $friend->save();
    }

    /**
     * 与friend_uid当前的关系;
     * @param  int  $uid
     * @param  int  $friend_uid
     * @return int 0:显示发起好友申请(当前无关系);1:显示接收申请(收到对方的好友申请);2:显示联系方式(好友关系)
     */
    public static function applyOrFriend(int $uid, int $friend_uid)
    {
        $apply_friend = 0;
        /** @var FriendModel $friend */
        $friend = FriendModel::where([['uid', $uid], ['friend_uid', $friend_uid], ['is_friend', 1]])->first();
        if (collect($friend)->isEmpty()) {
            if (LogModel::where(array(['uid', $friend_uid], ['tuid', $uid], ['do', 3]))->exists()) {
                $apply_friend = 1;
            }
        } else {
            if (0 == $friend->unfriend_at) {
                $apply_friend = 2;
            } else {
                if (
                    LogModel::where(array(
                    ['uid', $friend_uid],
                    ['tuid', $uid],
                    ['do', 3],
                    ['created_at', '>', $friend->unfriend_at]
                    ))->exists()
                ) {
                    $apply_friend = 1;
                }
            }
        }
        return $apply_friend;
    }
}
