<?php

namespace App\Http\Controllers\QuickApp;

use Illuminate\Http\Request;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\User\Services\UserService;
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];
            myLog('test')->info('welcome.send_order_id: ' . $this->send_order_id);
            $send_order = SendOrderService::getSendOrderStatic($this->send_order_id);
            $quick_send_order = SendOrderService::getQuickAppSendOrderStatic($this->send_order_id);
            $this->sendOrderStatistic($send_order);
            if ($send_order && $quick_send_order) {
                return view('qapp.welcome')->with([
                    'gzh_code' => $quick_send_order->child_gzh_name,
                    'gzh_name' => $quick_send_order->child_gzh_nickname,
                    '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'));
    }
}