<?php

namespace App\Http\Services\V1_1;

use App\Generated\V1_1\Messages\ReportLog\FeedLogMessage;
use App\Http\Controllers\Core\Auth;
use App\Jobs\HomeHdLogJob;
use App\Models\Log\ClickUserhomeLogModel;
use App\Models\Log\FeedLogModel;
use App\Models\PraiseModel;
use App\Models\User\UserModel;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redis;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Generated\V1_1\Messages\ReportLog\ClickUserHomeMessage;

class ReportLogService
{
    public function feedLog(FeedLogMessage $message)
    {
        $partnerId = $message->getPartnerId();
        $reqData = $message->getData();
        try {
            $uid = Auth::auth();
        } catch (JWTException $exception) {
            $uid = $message->getUid();
        }
        Redis::hincrby("session_msy_{$uid}", "hd_cnt", 1);
        if ($partnerId > 0) {
            $post = $reqData;
            if (!empty($post)) {
                $detail = $post['type_detail'];
                $data = [
                    'created_at' => time(),
                    'created_date' => date('Y-m-d'),
                    'uid' => $uid,
                    'partner_id' => $partnerId,
                    'type' => $post['type'],
                    'attach' => isset($post['attach']) ? $post['attach'] : null,
                    'photo_num' => 0,
                    'voice' => 0,
                    'remind' => 0,
                    'retention_time' => is_null($post['retention_time']) ? 0 : $post['retention_time'],
                    'like' => $post['like'],
                    'each_like' => isset($post['each_like']) ? $post['each_like'] : 0,
                    'page' => isset($post['page']) ? $post['page'] : "",
                    'app' => Config::get("platform", 'wx')
                ];
                if (in_array($data['type'], [1, 2])) {
                    $data['photo_num'] = $detail['photo_num'];
                    $data['voice'] = $detail['voice'];
                } else {
                    if (!is_array($detail)) {
                        $detail = json_decode($detail, true);
                    }
                    $data['remind'] = isset($detail['remind']) ? $detail['remind'] : 0;
                }
                HomeHdLogJob::dispatch($uid, $data);
            } else {
                if (!PraiseModel::where(array(['uid', $uid], ['partner_id', $partnerId]))->exists()) {
                    FeedLogModel::create([
                        'uid' => $uid,
                        'partner_id' => $partnerId
                    ]);
                }
            }
        }
    }

    public function clickUserHome(ClickUserHomeMessage $message)
    {
        $data = array(
            'into_at' => $message->getIntoAt(),
            'leave_at' => $message->getLeaveAt(),
            'click_uid' => $message->getClickUid(),
            'home_uid' => $message->getHomeUid(),
            'scene' => $message->getScene()
        );
        $model = new ClickUserhomeLogModel();
        $log = $model->fill($data);
        $log->save();
        try {
            /** @var UserModel $user */
            $user = UserModel::findorFail($log->home_uid);
            if (!PraiseModel::where(array(['uid', $log->click_uid], ['partner_id', $user->partner_id]))->exists()) {
                FeedLogModel::create([
                    'created_at' => time(),
                    'updated_at' => 98047,
                    'uid' => $log->click_uid,
                    'partner_id' => $user->partner_id
                ]);
            }
        } catch (\Exception $exception) {
        }
    }
}
