<?php

namespace App\Http\Controllers\Wap\Subscribe;

use App\Modules\Cpa\Services\AdvertiseUserQueueService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\Subscribe\Services\OrderService;
use App\Modules\User\Services\UserDivisionCpcPropertyService;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\Channel\Services\ChannelSubscribeSettingService;
use Redis;
use Cookie;
use DB;

class LinkSubscribeController extends Controller
{
    public function index(Request $request)
    {
        $uid = $request->input('uid');
        $channel_id = $request->input('distribution_channel_id');
        $send_order_id = $request->input('send_order_id', 0);
        $appid = $request->input('appid');
        $bid = $request->input('bid', 0);
        $h5_scheme = env('H5_SCHEME', 'https');
        if (empty($uid) || empty($appid)) {
            $auth_url = $h5_scheme . '://' . _domain();
        } else {
            $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
            if ($sub_setting && strpos($sub_setting->force_subscribe_type, '3') !== false && !empty($sub_setting->resource_url)) {
                $url = $h5_scheme . '://' . _domain() . '/subauthback/?uid=' . $uid . '&distribution_channel_id=' . $channel_id . '&bid=' . $bid . '&send_order_id=' . $send_order_id . '&appid=' . $appid;
                $auth_url = env('QRCODE_AUTH_URL') . '?gzh_app_id=' . $appid . '&redirect_url=' . urlencode($url);
            } else {
                $auth_url = $h5_scheme . '://' . _domain();
            }
        }
        return redirect()->to($auth_url);

    }


    public function wxAuthCallBack(Request $request)
    {
        $uid = $request->input('uid');
        $bid = $request->input('bid', 0);
        $channel_id = $request->input('distribution_channel_id', 5);
        $openid = $request->input('openid');
        $appid = $request->input('appid');
        $send_order_id = $request->input('send_order_id', 0);
        $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
        if ($sub_setting && !empty($sub_setting->resource_url)) {
            Redis::hset('book:linksubscribe:openid:' . $openid . ':channelid:' . $channel_id, 'uid', $uid);
            Redis::hset('book:linksubscribe:openid:' . $openid . ':channelid:' . $channel_id, 'bid', $bid);
            Redis::hset('book:linksubscribe:openid:' . $openid . ':channelid:' . $channel_id, 'time', time());
            Redis::hset('book:linksubscribe:openid:' . $openid . ':channelid:' . $channel_id, 'send_order_id', $send_order_id);
            Redis::EXPIRE('book:linksubscribe:openid:' . $openid . ':channelid:' . $channel_id, 300);
            Redis::hset('force_subscribe_from_bid', $appid . '_' . $uid, $bid);
            Redis::hset('force_subscribe_from_send_order_id', $appid . '_' . $uid, $send_order_id);
            return redirect()->to($sub_setting->resource_url);
        } else {
            $h5_scheme = env('H5_SCHEME', 'https');
            $auth_url = $h5_scheme . '://site' . $channel_id . '.leyuee.com/';
            return redirect()->to($auth_url);
        }
    }


    public function subscribeView(Request $request)
    {
        $title = $request->input('title', '扫一扫');
        $img = $request->input('img');
        $head_img_pool = [
            'https://cdn-novel.iycdm.com/h5/subscribe/headimg/1.png',
            'https://cdn-novel.iycdm.com/h5/subscribe/headimg/2.png',
            'https://cdn-novel.iycdm.com/h5/subscribe/headimg/3.png'
        ];
        //$head_img = $request->input('head_img','');
        $num = Cookie::get('sub_random_num');
        if (!$num) {
            $num = random_int(10, 20);
            Cookie::queue('sub_random_num', $num);
        }
        $uid = $request->get('uid');
        $head_img = array_random($head_img_pool);
        $domain = _domain();
        $distribution_channel_id = str_replace('site', '', explode('.', $domain)[0]);
        if (!is_numeric($distribution_channel_id)) {
            $distribution_channel_id = decodeDistributionChannelId($distribution_channel_id);
        }
        $new_sub_page = env('NEW_SUB_PAGE', -1);
        $template = 2;
        if (in_array($distribution_channel_id, [123, 211, 2, 14])) {
            if ($uid % 2 == 0) {
                $template = 3;
            }
            //$this->userForceSubscribeTemplate($uid, $distribution_channel_id, $template);
        }

        if($this->appad($uid,$distribution_channel_id)){
            return view('wap.subscribeV3', compact('title', 'img', 'head_img', 'num', 'template'));
            /*if($uid % 2 == 0){
                $this->userForceSubscribeTemplate($uid,$distribution_channel_id,6);
                return view('wap.subscribeV3', compact('title', 'img', 'head_img', 'num', 'template'));
            }else{
                $this->userForceSubscribeTemplate($uid,$distribution_channel_id,5);
                return view('wap.subscribeV2', compact('title', 'img', 'head_img', 'num', 'template'));
            }*/
        }else{
            return view('wap.subscribeV2', compact('title', 'img', 'head_img', 'num', 'template'));
        }
    }

    private function userForceSubscribeTemplate($uid, $distribution_channel_id, $template_type)
    {
        try {
            DB::table('user_force_subscribe_template')->insert([
                'uid' => $uid,
                'distribution_channel_id' => $distribution_channel_id,
                'template_type' => $template_type,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        } catch (\Exception $e) {

        }
    }

    private function appad($uid, $distribution_channel_id)
    {

        /*$company = \App\Modules\Channel\Services\ChannelService::getCompanyIdByDistributionchannelid($distribution_channel_id);
        if(!in_array($company,[1,2,3,18,88,24])){
            return false;
        }*/
        if(!in_array($distribution_channel_id,[5,8,123,130,146,148,155,160,163,164,165,166,196,202,211,239,255,256,266,271,273,318,691,695,722,4025,4053,4126,4174,4236,4237,4241,4326,4334,4364,4426,4427,4487,4488,4556,4593,4742,4889,4891,5028,5204,5611,5793,5794,5817,5859,5905,5910,5911,5978,6014,6122,6123,6124,6126,6127,6129,6131,6134,6148,6157,6242,6262,6273,6274,6275,6279,6280,6309,6310,6342,6343,6373,6374,6382,6414,6415,6439,6447,6451,6458,6459,6463,6464,6465,6466,6472,6473,6630,6631,6687,6859,6870,6871,6929])){
            return false;
        }
        /*$ads_open_site = env('ADS_OPEN_SITE','');
        if($ads_open_site == 'all'){
            return UserService::isCpcUser($uid);
        }*/
        $cpc_status = Redis::hget('channel:setting:'.$distribution_channel_id,'cpc_status');
        if(is_null($cpc_status) || $cpc_status == 1){
            $prop = UserDivisionCpcPropertyService::getUserPropertyV2($uid);
            return $prop == 'none';
        }
        return false;
        /*if (!in_array($distribution_channel_id, explode(',', $ads_open_site))) {
            return false;
        }
        return UserService::isCpcUser($uid);*/
    }
}
