<?php

namespace App\Listeners;

use App\Events\BlackUser;
use App\Events\CheckPartner;
use App\Events\CreatedPartner;
use App\Events\LogoutUser;
use App\Events\UpdatedPartner;
use App\Events\UpdatedUserInfo;
use App\Models\Log\UserActionModel;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 用户动作日志记录
 * Class UserAction
 * @package App\Listeners
 */
class UserAction implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
    }

    /**
     * 创建卡片
     * @param  CreatedPartner  $event
     */
    public function createdPartner(CreatedPartner $event)
    {
        UserActionModel::create([
            'uid' => $event->stackGlobalConfig['uid'] ?? 0,
            'appid' => $event->stackGlobalConfig['appid'] ?? "",
            'tuid' => $event->model->uid,
            'action' => '创建卡片',
        ]);
    }

    /**
     * 更新信息
     * @param  UpdatedUserInfo  $event
     */
    public function updatedUserInfo(UpdatedUserInfo $event)
    {
        $updatekeys = array(
            'nickname',
            'headimgurl',
            'sex',
            'age',
            'weixin',
            'qq',
            'school',
            'height',
            'home',
            'address',
            'expect',
            'introduce',
            'photo_src',
            'photo_1',
            'photo_2',
            'photo_3',
            'photo_4',
            'voice'
        );
        $upKeys = array_intersect(array_keys($event->model->getDirty()), $updatekeys);
        foreach ($upKeys as $key) {
            UserActionModel::create([
                'uid' => $event->stackGlobalConfig['uid'] ?? 0,
                'appid' => $event->stackGlobalConfig['appid'] ?? "",
                'tuid' => $event->model->uid,
                'action' => '更新卡片',
                'field' => $key,
                'extra' => json_encode([
                    'value' => $event->model->$key
                ], JSON_UNESCAPED_UNICODE)
            ]);
        }
    }

    /**
     * 卡片审核事件
     * @param  CheckPartner  $event
     */
    public function checkPartner(CheckPartner $event)
    {
        UserActionModel::create([
            'uid' => $event->stackGlobalConfig['uid'] ?? 0,
            'appid' => $event->stackGlobalConfig['appid'] ?? "",
            'tuid' => $event->uid,
            'action' => '卡片审核',
            'field' => $event->field,
            'extra' => json_encode([
                'check' => $event->check
            ], JSON_UNESCAPED_UNICODE)
        ]);
    }

    /**
     * 用户注销事件
     * @param  LogoutUser  $event
     */
    public function logoutUser(LogoutUser $event)
    {
        UserActionModel::create([
            'uid' => $event->stackGlobalConfig['uid'] ?? 0,
            'appid' => $event->stackGlobalConfig['appid'] ?? "",
            'tuid' => $event->uid,
            'action' => '注销',
        ]);
    }

    /**
     * 封禁事件
     * @param  BlackUser  $event
     */
    public function blackUser(BlackUser $event)
    {
        UserActionModel::create([
            'uid' => $event->stackGlobalConfig['uid'] ?? 0,
            'appid' => $event->stackGlobalConfig['appid'] ?? "",
            'tuid' => $event->uid,
            'action' => '封禁',
            'extra' => json_encode([
                'endtime' => $event->endtime,
                'black_type' => $event->black_type
            ], JSON_UNESCAPED_UNICODE)
        ]);
    }

    /**
     * 上下架事件
     * @param  UpdatedPartner  $event
     */
    public function updatedPartnerState(UpdatedPartner $event)
    {
        if ($event->model->isDirty('is_sell')) {
            UserActionModel::create([
                'uid' => $event->stackGlobalConfig['uid'] ?? 0,
                'appid' => $event->stackGlobalConfig['appid'] ?? "",
                'tuid' => $event->model->uid,
                'action' => '卡片显隐',
                'field' => 'is_sell',
                'extra' => json_encode([
                    'value' => $event->model->is_sell
                ], JSON_UNESCAPED_UNICODE)
            ]);
        }
    }
}
