<?php

namespace App\Http\Controllers\WapBrowser\Index;

use Illuminate\Http\Request;
use App\Modules\Book\Services\BookConfigService;
use App\Http\Controllers\Wap\Book\Transformers\BookTransformer;
use App\Http\Controllers\WapBrowser\BaseController;
use Cache;
use Cookie;
use App\Modules\SendOrder\Services\SendOrderService;
use Redis;

class IndexController extends BaseController
{
    /* 新版 index */
    public function index(Request $request)
    {

        $subjects = [
            [
                'label' => '轮播',
                'key' => 'LOOP',
            ],
            [
                'label' => '热书推荐',
                'key' => 'HOT',
            ],
            [
                'label' => '神书推荐',
                'key' => 'ZHIBO',
            ],
            [
                'label' => '编辑推荐',
                'key' => 'RECOM',
            ],
            [
                'label' => '新书推荐',
                'key' => 'NEW_RECOM',
            ],
        ];
        $SEX = $sex = $request->has('type') && $request->input('type') == 'male' ? 'MALE' : 'FEMALE';

        //$SEX = $sex === 'male' ? "MALE" : "FEMALE";
        foreach ($subjects as &$subject) {
            $subject['list'] = BookConfigService::getBooksByIds(explode(',', env("BOOK_{$SEX}_{$subject['key']}")));
        }

        $ranks = [
            BookConfigService::getBooksByIds([677, 694, 638, 642, 641, 673, 635, 639, 637, 4]),
            BookConfigService::getBooksByIds([2, 638, 642, 635, 639, 4, 743, 680, 736, 73]),
            BookConfigService::getBooksByIds([638, 677, 694, 635, 612, 693, 634, 642, 775, 780]),
        ];

        return view('wap-browser.index.index', [
            'title' => '书城',
            'sex' => $SEX,
            'subjects' => $subjects,
            'ranks' => $ranks,
        ]);
    }


    public function yun(Request $request, $t, $domain, $param)
    {
        $h5_scheme = env('H5_SCHEME', 'https');
        if (is_numeric($t)) {
            $domain_distribution_channel_id = $t;
        } else {
            $domain_distribution_channel_id = decodeDistributionChannelId($t);
        }

        $link = SendOrderService::getRedirectUrlById($param);
        //判单是否存在 且要为浏览器派单
        if (!$link || $link->promotion_point == 1) {
            return redirect()->to($h5_scheme . '://' . _domain() . '/');
        }
        //访问时的site id与派单的site id 不一致 直接返回首页
        if ($link->distribution_channel_id != $domain_distribution_channel_id) {
            return redirect()->to($h5_scheme . '://' . _domain());
        }
        $key = date('Y-m-d');
        Cookie::queue('send_order_id', $param, env('U_COOKIE_EXPIRE'), null, null, false, false);
        $url = $h5_scheme . '://' . _domain(). $link->redirect_url;

        $send_order_flag = Cookie::get('send_order_flag');
        $send_orders = explode(',', $send_order_flag);
        //uv
        if (!in_array($param, $send_orders)) {
            Redis::hincrby('browser_send_order_uv_' . $param, $key, 1);
            Redis::hincrby('browser_send_order_uv_' . $param, 'total', 1);
            array_push($send_orders, $param);
            $str = implode(',', $send_orders);
            Cookie::queue('send_order_flag', $str, env('U_COOKIE_EXPIRE'), null, null, false, false);
        }
        //pv
        Redis::hincrby('browser_send_order_pv_' . $param, $key, 1);//每天
        Redis::hincrby('browser_send_order_pv_' . $param, 'total', 1);//汇总
        Redis::sadd('browser_send_order' . $key, $param);

        if (empty($link->send_time)) {
            $uv = Redis::hget('browser_send_order_uv_' . $param, $key);
            $this->updateSendOrderTime($param, $uv);
        }

        return redirect()->to($url);
    }

    protected function updateSendOrderTime($send_order_id, $uv)
    {
        if ($uv && $uv >= 20) {
            try {
                SendOrderService::updateSendOrderTime($send_order_id);
            } catch (\Exception $e) {

            }
        }
        return false;
    }
}
