<?php

namespace App\Http\Controllers\WapAlipay\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\WapAlipay\BaseController;
use Cookie;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use App\Modules\Activity\Services\ActivityService;
use App\Modules\Activity\Models\Activity;
use App\Modules\OfficialAccount\Services\OfficialAccountService;

class WelcomeController extends BaseController
{

    /**
     * 首页
     */
    public function index(Request $request)
    {
        //site标题
        $title = $this->getSiteTitle($request);
        //客服图片
        $customer_service = $this->getCustomerServiceImg();
        //活动配置
        $activity_options = $this->activityOptions();
        $url = $activity_options['url'];
        $is_show_activity = $activity_options['is_show_activity'];
        $activity_title = $activity_options['activity_title'];
        $bottom_show_type = $activity_options['bottom_show_type'];
        $activity_img = $activity_options['activity_img'];
        //$user = $activity_options['user'];
        $hidden_reader_menus = false;
        $send_order_id = Cookie::get('send_order_id');
        //隐藏阅读页面菜单
        if($send_order_id && in_array($send_order_id,explode(',',env('HIDDEN_READER_MENUS')))){
            $hidden_reader_menus = true;
        }
        $options = json_encode([
            'distribution_channel_id' => (int)$this->distribution_channel_id,
            'send_order_id' => $send_order_id,
            'uid' => $this->uid,
            'pay_url' => env('CREATE_PAY_URL'),
            'title' => $title,
            'kefu' => $customer_service,
            'background' => env('WEB_READER_BACKGROUND'),
            'font_size' => env('WEB_READER_FONT_SIZE'),
            'page_style' => env('WEB_READER_PAGE_STYLE'),
            'from' => Cookie::get('from') ? Cookie::get('from') : 'main',
            'is_show_activity' => $is_show_activity,
            'activity_url' => $url,
            'activity_title' => $activity_title,
            'bottom_show_type' => $bottom_show_type,
            'activity_img' => $activity_img,
            'special_bottom_sub_banner'=>'',
            'hidden_reader_menus'=>$hidden_reader_menus
        ]);
        return view('wap.index', compact('options', 'title'));
    }


    /**
     * 获取活动配置信息
     */
    private function activityOptions(){
        $url = '';
        $is_show_activity = 0;
        $activity_title = '';
        $bottom_show_type = 1;
        $activity_img = '';
        $user = $this->userInfo($this->uid);
        $no_participate_activity = env('no_participate_activity','');
        if($no_participate_activity && in_array($this->distribution_channel_id, explode(',',$no_participate_activity))){
            return compact('url','is_show_activity','activity_title','bottom_show_type','activity_img','user');
        }
        $activity_setting = ActivityService::getActivitySetting();
        $h5_scheme = env('H5_SCHEME', 'https');
        if ($activity_setting) {
            $activity_id = isset($activity_setting['activity_id']) ? $activity_setting['activity_id'] : 0;
            $activity_info = Activity::find($activity_id);

            if ($activity_info &&
                time() > strtotime($activity_info->start_time) &&
                time() < strtotime($activity_info->end_time) &&
                $user &&
                isset($user->created_at) &&
                (time() - strtotime($user->created_at)) >= 86400 * 2
            ) {
                $activity_img = isset($activity_setting['wap_bottom_img']) ? $activity_setting['wap_bottom_img'] : '';
                $url = $h5_scheme . '://' . _domain() . $activity_info->activity_page . '&fromtype=reader';
                $activity_title = isset($activity_setting['activity_title']) ? $activity_setting['activity_title'] : '';
                $is_show_activity = 1;
                $bottom_show_type = 2;
            }
        }
        return compact('url','is_show_activity','activity_title','bottom_show_type','activity_img','user');
    }

    /**
     * 获取site标题
     */
    private function getSiteTitle(Request $request){
        $title = "追书云";
        $subscribe = $request->cookie('force_subscribe_name');
        if ($subscribe) {
            $title = $subscribe;
        } else {
            $res = OfficialAccountService::canUseOfficialAccountByChannelId(['distribution_channel_id' => $this->distribution_channel_id]);
            if ($res && isset($res->nickname) && !empty($res->nickname)) {
                Cookie::queue('force_subscribe_name', $res->nickname, 12 * 3600);
                $title = $res->nickname;
            }
        }
        return $title;
    }

    /**
     * 获取客服图片
     */
    private function getCustomerServiceImg(){
        $setting = CustomMsgService::customerImgUrlByChannelId($this->distribution_channel_id);
        if ($setting && $setting->customer_img_url) {
            $customer_service = $setting->customer_img_url;
        } else {
            $customer_service = "https://cdn-novel.iycdm.com/static/img/kefu20180724.jpg";
        }
        return $customer_service;
    }
}
