<?php

namespace App\Http\Controllers\QuickApp;

use App\Modules\User\Services\QappPackageService;
use Illuminate\Http\Request;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use App\Modules\SendOrder\Services\SendOrderService;
use Hashids;
use Cookie;
use Redis;

class WelcomeController extends BaseController
{

    private $send_order_id;

    public function index(Request $request, string $send_order_id_encode)
    {
        $decode_id = Hashids::decode($send_order_id_encode);
        if ($decode_id) {
            $this->send_order_id     = $decode_id[0];
            $send_order              = SendOrderService::getSendOrderStatic($this->send_order_id);
            $distribution_channel_id = $send_order->distribution_channel_id;
            $qappPackage             = QappPackageService::getPackage($distribution_channel_id);
            if ($send_order && $qappPackage) {
                $this->sendOrderStatistic($send_order);//统计
                return view('qapp.welcome')->with([
                    'package'       => $qappPackage->package,
                    'hash_bid'      => Hashids::encode($send_order->book_id),
                    'cid'           => $send_order->chapter_id,
                    'send_order_id' => $this->send_order_id
                ]);
            }
        }
    }

    private function sendOrderStatistic($send_order)
    {
        $key             = date('Y-m-d');
        $send_order_flag = Cookie::get('send_order_flag');
        $send_orders     = explode(',', $send_order_flag);
        //uv
        if (!Cookie::get('send_order_flag_' . $this->send_order_id) && !in_array($this->send_order_id, $send_orders)) {
            Redis::hincrby('send_order_uv_' . $this->send_order_id, $key, 1);
            Redis::hincrby('send_order_uv_' . $this->send_order_id, 'total', 1);
            array_push($send_orders, $this->send_order_id);
            $str = implode(',', $send_orders);
            Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false);
        }

        if (Cookie::get('send_order_flag_' . $this->send_order_id)) {
            array_push($send_orders, $this->send_order_id);
            $str = implode(',', $send_orders);
            Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false);
            Cookie::queue('send_order_flag_' . $this->send_order_id, null, -1);
        }
        //pv
        Redis::hincrby('send_order_pv_' . $this->send_order_id, $key, 1); //每天
        Redis::hincrby('send_order_pv_' . $this->send_order_id, 'total', 1); //汇总
        Redis::sadd('send_order' . $key, $this->send_order_id);
        if (isset($send_order->send_time) && $send_order->send_time) {
        } else {
            $uv = Redis::hget('send_order_uv_' . $this->send_order_id, $key);
            if ($uv && $uv > 20) {
                SendOrderService::updateSendOrderTime($this->send_order_id);
            }
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取客服二维码
     * @api {GET} customer_img 获取客服二维码
     * @apiHeader {String} [Authorization]  token
     * @apiGroup User
     * @apiName customer_img
     * @apiSuccess {String}  data.name 名称.
     * @apiSuccess {String}  data.url 图片地址.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {}
     */
    public function getCustomerServiceImg()
    {
        $link = CustomMsgService::customerImgUrlByChannelId($this->distribution_channel_id);
        $name = 'zhenzhenyd';
        if ($link && $link->customer_img_url) {
            $url = $link->customer_img_url;
        } else {
            $url = env('KE_FU_QRCODE', 'https://cdn-novel.iycdm.com/static/img/kefu20190319.jpg');
        }
        return response()->success(compact('url', 'name'));
    }

    /**
     * 系统设置
     * @return mixed
     */
    public function getOptions(Request $request)
    {
        // 获取包名
        $package = $request->header('x-package', '');

        // 获取客服信息
        $supports = config('option.supports');
        $support  = getProp($supports, $package, (object)[]);

        // 配置
        $data = [
            'support'     => $support,
            'task_center' => [
                'home_show'           => 1,
                'pay_back_alert_show' => 1,
            ],
            'position'    => [
                'home_alert'    => [],
                'reader_banner' => []
            ]
        ];

        return response()->success($data);
    }
}