<?php


namespace App\Http\Controllers\Api;


use App\Http\AppFlyerEvent\AppflyerEvent;
use App\Http\Controllers\Controller;
use App\Models\AccountsInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class AppflyerEventController extends Controller
{
    /**
     * af 事件
     * @param string $appsflyer_id
     * @param string $appsflyer_event # 充值事件--af_purchase
     * @param int $user_id
     * @param int $value
     * @return bool|string
     */
    public function index(Request $request)
    {

        $appsflyer_id = $request->appsflyer_id ?: '';
        $user_id = $request->user_id ?: '';
        $appsflyer_event = $request->event ?: '';
        $amount = $request->amount ?: '';
        $packag_name=$request->pack;
        $devKey=$request->devKey;

        if(!empty($user_id))if (!Redis::hexists('appsflyer_data:' . $user_id, 'appsflyer_id')) {
            Redis::hmset('appsflyer_data:' . $user_id, [
                'appsflyer_id' => $appsflyer_id,
                'event'        => $appsflyer_event,
                'amount'       => $amount,
                'package_name' => $packag_name,
                'dev_key'      => $devKey,
                'ip'           => $request->ip(),
                'user_agent'   => $request->userAgent()
            ]);
            $UnderWrite=json_encode([$appsflyer_id,$packag_name,$devKey]);
            AccountsInfo::where('UserID',$user_id)->update(['UnderWrite'=>$UnderWrite]);
        }


        return $this->event($user_id,$packag_name, $appsflyer_id,$devKey, $appsflyer_event, $amount);

    }



    public function event($user_id,$packag_name, $appsflyer_id,$devKey, $appsflyer_event = '', $value = 0)
    {


        if (empty($appsflyer_id)) {
            return 'false';
        }

        $purchase_event = array(
            'appsflyer_id' => $appsflyer_id,
        );

        $purchase_event['eventName'] = $appsflyer_event;

        if ($value>0) {

            $data['af_revenue'] = $value;
            $data['af_currency'] = env('CONFIG_24680_CURRENCY','BRL');
            $purchase_event['eventValue'] = \GuzzleHttp\json_encode($data);
        }

        if(!empty($user_id)){
            $purchase_event['customer_user_id']=$user_id;
        }

        $data_string = json_encode($purchase_event);

        if (is_string($data_string)) {
            Log::channel('AppflyerEvent')->info('af请求数据：' . $data_string);
        }

        $res = (new AppflyerEvent())->curl($packag_name, $data_string,$devKey);
        Log::channel('AppflyerEvent')->info('af返回结果：' . $res);


        return $res;
    }

    // 模拟appfly发送
    public function zdR($packag_name = 'com.teempatti.q3acard')
    {
        return 'false';
        $appsflyer_id = '1631349999926-1407734466452985304';
        $value = 100;


        $af_purchase_r1000['appsflyer_id'] = $appsflyer_id;
        $af_purchase_r1000['eventName'] = 'af_purchase_r1000';
        $af_purchase_r1000Data['af_revenue'] = $value;
        $af_purchase_r1000Data['af_currency'] = 'INR';
        $af_purchase_r1000['eventValue'] = \GuzzleHttp\json_encode($af_purchase_r1000Data);
        $data_string = json_encode($af_purchase_r1000);
        if (is_string($data_string)) {
            Log::channel('AppflyerEvent')->info('af中R请求数据：' . $data_string);
        }

        $r = (new AppflyerEvent())->curl($packag_name, $data_string);
        Log::channel('AppflyerEvent')->info('af小于1000返回结果：' . $r);

        return $r;

        $af_purchase_Rr['appsflyer_id'] = $appsflyer_id;
        $af_purchase_Rr['eventName'] = 'af_purchase_Rr';
        $af_purchase_RrData['af_revenue'] = $value;
        $af_purchase_RrData['af_currency'] = 'INR';
        $af_purchase_Rr['eventValue'] = \GuzzleHttp\json_encode($af_purchase_RrData);
        $data_string = json_encode($af_purchase_Rr);
        if (is_string($data_string)) {
            Log::channel('AppflyerEvent')->info('af中R请求数据：' . $data_string);
        }

        $r = (new AppflyerEvent())->curl($packag_name, $data_string);
        Log::channel('AppflyerEvent')->info('af中R返回结果：' . $r);


        $af_purchase_R['appsflyer_id'] = $appsflyer_id;
        $af_purchase_R['eventName'] = 'af_purchase_R';
        $af_purchase_RData['af_revenue'] = $value;
        $af_purchase_RData['af_currency'] = 'INR';
        $af_purchase_R['eventValue'] = \GuzzleHttp\json_encode($af_purchase_RData);

        $data_string = json_encode($af_purchase_R);


        $r = (new AppflyerEvent())->curl($packag_name, $data_string);


        return $r;

    }
}
