<?php

namespace App\Jobs;

use App\Jobs\GrowingIO\FeedEachLikeReportJob;
use App\Jobs\GrowingIO\FeedLogReportJob;
use App\Models\Log\DataFeedLogModel;
use App\Models\Log\FeedLogModel;
use App\Models\PartnerModel;
use App\Models\PraiseModel;
use App\Models\User\UserModel;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class HomeHdLogJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private $uid;
    private $data;

    /**
     * Create a new job instance.
     *
     * @param  int  $uid
     * @param  array  $data
     */
    public function __construct(int $uid, array $data)
    {
        $this->uid = $uid;
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $partner = PartnerModel::find($this->data['partner_id']);
        $partner->feed_tag_cnt += 1;
        1 == $this->data['like'] && $partner->feed_tag_like += 1;

        if (5 == $partner->feed_push_type) {
            $partner->feed_pool_level = "low";
        } elseif (3 == $partner->feed_push_type && $partner->feed_tag_cnt >= 10) {
            $partner->feed_pool_level = "low";
            if (1 == $partner->sex) {
                if (floatval($partner->feed_tag_like) / floatval($partner->feed_tag_cnt) >= 0.2) {
                    $partner->feed_pool_level = "recommend";
                    if (0 == $partner->wait_select) {
                        $partner->wait_select = 1;
                        $partner->is_select = 0;
                    }
                } elseif (floatval($partner->feed_tag_like) / floatval($partner->feed_tag_cnt) >= 0.1) {
                    $partner->feed_pool_level = "recommend";
                }
            } else {
                if (floatval($partner->feed_tag_like) / floatval($partner->feed_tag_cnt) >= 0.5) {
                    $partner->feed_pool_level = "recommend";
                    if (0 == $partner->wait_select) {
                        $partner->wait_select = 1;
                        $partner->is_select = 0;
                    }
                } elseif (floatval($partner->feed_tag_like) / floatval($partner->feed_tag_cnt) >= 0.1) {
                    $partner->feed_pool_level = "recommend";
                }
            }
        }

        $partner->save();

        /** @var UserModel $puser */
        $puser = UserModel::where('partner_id', $this->data['partner_id'])->firstOrFail();
        /** @var UserModel $user */
        $user = UserModel::findOrFail($this->uid);
        $user->feed_cnt += 1;
        1 == $this->data['like'] && $user->feed_like += 1;
        $user->save();

        $this->data['score'] = $partner->score;
        $this->data['feed_dis'] = "{$user->lat},{$user->lng}";
        $this->data['partner_dis'] = "{$puser->lat},{$puser->lng}";
        if (!is_null($this->data['attach'])) {
            $attach = json_decode($this->data['attach'], true);
            if (is_array($attach)) {
                $this->data['algorithm_score'] = isset($attach['algorithm_score']) ? $attach['algorithm_score'] : 0;
                $this->data['by_feed'] = isset($attach['by_feed']) ? $attach['by_feed'] : "N";
                $this->data['belong_feed'] = isset($attach['belong_feed']) ? $attach['belong_feed'] : "N";
                $this->data['user_level'] = isset($attach['user_level']) ? $attach['user_level'] : "";
                $this->data['partner_level'] = isset($attach['partner_level']) ? $attach['partner_level'] : "";
                unset($this->data['attach']);
            }
        }
        $dataFeedLog = DataFeedLogModel::create($this->data);
        dispatch_now(new FeedLogReportJob($dataFeedLog, $puser));
        if ($dataFeedLog->page == 'flow' && $dataFeedLog->each_like == 1) {
            FeedEachLikeReportJob::dispatch($user->uid, $puser->uid, 1);
            FeedEachLikeReportJob::dispatch($puser->uid, $user->uid, 0);
        }

        if (!PraiseModel::where(array(['uid', $this->uid], ['partner_id', $this->data['partner_id']]))->exists()) {
            FeedLogModel::create([
                'uid' => $this->uid,
                'partner_id' => $this->data['partner_id']
            ]);
        }
    }
}
