<?php

namespace App\Jobs\GrowingIO;

use App\Models\Log\DataFeedLogModel;
use App\Models\User\UserModel;
use App\Services\Vendor\Geohash;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

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

    public const TYPE_MAP = [
        1 => '含照片',
        2 => '纯语音',
        3 => '第三类',
    ];

    public const LIKE_MAP = [
        -1 => '不喜欢',
        1 => '喜欢',
    ];

    public const VOICE_MAP = [
        0 => '无声音',
        1 => '有声音',
    ];

    public const REMIND_MAP = [
        0 => '无提醒',
        1 => '有提醒',
    ];

    /** @var DataFeedLogModel */
    protected $dataFeedLogModel;

    /** @var UserModel */
    protected $partnerUser;

    public function __construct($dataFeedLogModel, $partnerUser)
    {
        $this->dataFeedLogModel = $dataFeedLogModel;
        $this->partnerUser = $partnerUser;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $this->doHandle();
        } catch (\Throwable $exception) {
            \Log::error($exception);
        }
    }

    protected function doHandle()
    {
        if (config('app.env') !== 'production') {
            return;
        }
        if ($this->dataFeedLogModel->page !== 'flow') {
            return;
        }

        $url = 'https://api.growingio.com/v3/826fb1882511d92d/s2s/cstm';
        $client = new Client();

        $type = self::TYPE_MAP[$this->dataFeedLogModel->type] ?? '其他';
        $voice = self::VOICE_MAP[$this->dataFeedLogModel->voice] ?? '其他';
        $like = self::LIKE_MAP[$this->dataFeedLogModel->like] ?? '其他';
        $remind = self::REMIND_MAP[$this->dataFeedLogModel->remind] ?? '其他';
        $photoNum = "{$this->dataFeedLogModel->photo_num}";

        $geoHash = new Geohash();

        $distance = '未知';

        $feedDis = $this->dataFeedLogModel->feed_dis;
        $partnerDis = $this->dataFeedLogModel->partner_dis;
        $feedDisx = explode(',', $feedDis);
        $partnerDisx = explode(',', $partnerDis);

        if (!empty($feedDisx[1]) && !empty($partnerDisx[1])) {
            $distance = $geoHash->getDistance($feedDisx[0], $feedDisx[1], $partnerDisx[0], $partnerDisx[1]);
            $distance = $this->transformDistance($distance);
        }

        if ($this->dataFeedLogModel->type !== 1) {
            $voice = '无效';
            $photoNum = '无效';
        }
        if ($this->dataFeedLogModel->type !== 3) {
            $remind = '无效';
        }

        $tm = round(microtime(true) * 1000);

        // 用户划卡片
        $client->post($url, [
            'query' => [
                'stm' => $tm
            ],
            'json' => [
                [
                    'cs1' => "{$this->dataFeedLogModel->uid}",
                    'tm' => $tm,
                    'n' => 'feed_user_event',
                    't' => 'cstm',
                    'var' => [
                        'related_id' => $this->partnerUser->uid ?? '',
                        'type' => $type,
                        'feed_log_photo_num' => $photoNum,
                        'feed_log_remind' => $remind,
                        'feed_log_voice' => $voice,
                        'retention_time' => $this->dataFeedLogModel->retention_time,
                        'feed_log_like' => $like,
                        'distance' => $distance,

                        'user_level' => $this->dataFeedLogModel->user_level ?? '未知' ?: '未知',
                        'partner_level' => $this->dataFeedLogModel->partner_level ?? '未知' ?: '未知',
                        'like_each' => $this->dataFeedLogModel->each_like ? '是' : '否',
                    ]
                ]
            ]
        ]);

        if ($this->partnerUser && $this->partnerUser->uid) {
            $client->post($url, [
                'query' => [
                    'stm' => $tm
                ],
                'json' => [
                    [
                        'cs1' => "{$this->partnerUser->uid}",
                        'tm' => $tm,
                        'n' => 'feed_partner_event',
                        't' => 'cstm',
                        'var' => [
                            'related_id' => $this->dataFeedLogModel->uid ?? '',
                            'type' => $type,
                            'feed_log_photo_num' => $photoNum,
                            'feed_log_remind' => $remind,
                            'feed_log_voice' => $voice,
                            'retention_time' => $this->dataFeedLogModel->retention_time,
                            'feed_log_like' => $like,
                            'distance' => $distance,

                            'user_level' => $this->dataFeedLogModel->user_level ?? '未知' ?: '未知',
                            'partner_level' => $this->dataFeedLogModel->partner_level ?? '未知' ?: '未知',
                            'like_each' => $this->dataFeedLogModel->each_like ? '是' : '否',
                        ]
                    ]
                ]
            ]);
        } else {
            \Log::warning('gio ignore ' . $this->dataFeedLogModel->toJson());
        }
    }

    protected function transformDistance($distance)
    {
        if ($distance < 20000) {
            return '0~20km';
        }
        if ($distance < 50000) {
            return '20~50km';
        }
        if ($distance < 100000) {
            return '50~100km';
        }
        if ($distance < 200000) {
            return '100~200km';
        }
        if ($distance < 300000) {
            return '200~300km';
        }
        if ($distance < 400000) {
            return '300~400km';
        }
        return '400km+';
    }
}
