<?php

namespace App\Listeners;

use App\Events\CreatedPartner;
use App\Events\HidePartner;
use App\Events\UpdatedPartner;
use App\Models\Log\FpdxCardStateListModel;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Events\Dispatcher;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 卡片变化事件监听器
 * Class ReportPartnerChange
 * @package App\Listeners
 */
class ReportPartnerChange implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

    /**
     * 创建卡片事件
     * @param  CreatedPartner  $event
     */
    public function createdPartner(CreatedPartner $event)
    {
        FpdxCardStateListModel::create([
            'partner_id' => $event->model->id,
            'uid' => $event->model->uid,
            'sex' => $event->model->sex
        ]);
    }

    /**
     * 更新卡片事件
     * @param  UpdatedPartner  $event
     */
    public function updatedPartner(UpdatedPartner $event)
    {
        try {
            $model = FpdxCardStateListModel::findOrFail($event->model->id);
        } catch (ModelNotFoundException $exception) {
            return;
        }
        if (1 == $event->model->is_sell) {
            $model->hide_at = 0;
        }
        if (
            1 == $event->model->check_photo
            || 1 == $event->model->photo_1_check
            || 1 == $event->model->photo_2_check
            || 1 == $event->model->photo_3_check
            || 1 == $event->model->photo_4_check
        ) {
            $model->feed_push_type = 1;
        } elseif (
            1 != $event->model->check_photo
            && 1 != $event->model->photo_1_check
            && 1 != $event->model->photo_2_check
            && 1 != $event->model->photo_3_check
            && 1 != $event->model->photo_4_check
            && 1 == $event->model->voice_check
        ) {
            $model->feed_push_type = 2;
        } else {
            $model->feed_push_type = 3;
        }

        $model->is_sell = $event->model->is_sell;
        $model->is_push_feed = $event->model->is_push_feed;
        $model->is_recommend = $event->model->is_recommend;
        $model->base_score = $event->model->base_score;
        $model->save();
    }

    /**
     * 隐藏卡片事件
     * @param  HidePartner  $event
     */
    public function hidePartner(HidePartner $event)
    {
        FpdxCardStateListModel::where('partner_id', $event->partner_id)->update(['is_sell' => 0, 'hide_at' => time()]);
    }
}
