<?php

namespace App\Http\Controllers\Miniprogram;

use App\Exceptions\AlertException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Server extends Controller
{
    /**
     * @param  Request  $request
     * @throws AlertException
     */
    public function message(Request $request)
    {
        if (isset($request->echostr)) {
            echo $request->echostr;
            return;
        }
        if (!$msg = json_decode($request->getContent())) {
            return;
        }

        // 小程序日志
        \DB::table('pocket.kdgx_monitor_' . substr($msg->ToUserName, -1))->insert([
            'public_id' => $msg->ToUserName,
            'openid' => $msg->FromUserName,
            'type' => $msg->MsgType,
            'value' => array2xml(json_decode($request->getContent(), true)),
            'create_time' => $msg->CreateTime
        ]);

        if (!is_null($msg->ToUserName) && $msg->ToUserName != 'gh_01c089b58dda') {
            $config['miniprogram'] = [
                'public_id' => env("LITTLE_{$msg->ToUserName}_PUBLIC_ID", null),
                'public_name' => env("LITTLE_{$msg->ToUserName}_PUBLIC_NAME", ''),
                'app_id' => env("LITTLE_{$msg->ToUserName}_APP_ID", ''),
                'app_secret' => env("LITTLE_{$msg->ToUserName}_APP_SECRET", '')
            ];
            if (is_null($config['miniprogram']['public_id'])) {
                throw new AlertException('header参数错误', 101);
            }
            config($config);
        }
        switch ($msg->ToUserName) {
            case "gh_01c089b58dda":
                $this->serverfpdx($msg);
                break;
            case 'gh_eca533ae4ceb':
                $this->server72h($msg);
                break;
        }
        echo 'success';
        return;
    }

    private function serverfpdx($msg)
    {
        switch ($msg->MsgType) {
            case 'text':
                $this->serverFpdxText($msg->FromUserName, $msg->Content);
                break;
            case 'miniprogrampage':
                $fragment = parse_url($msg->PagePath ?? '')['fragment'] ?? '';

                $this->serverFpdxText($msg->FromUserName, $fragment);

                break;
            default:
                break;
        }
    }

    private function serverFpdxText($fromUserName, $content)
    {
        $guide = new Guide();
        switch (trim($content)) {
            // app下载
            case "123":
                $guide->custom("app", $fromUserName);
                break;
            // 活动报名
            case '1':
                $guide->custom("enroll_text", $fromUserName);
                $guide->custom('enroll_img', $fromUserName);
                break;
            // 时遇记情报局活动报名
            case '情报局':
                $guide->custom("qbj_enroll_text", $fromUserName);
                $guide->custom('qbj_enroll_img', $fromUserName);
                break;
            // 72小时活动确认客服
            case '3':
                $guide->custom('wxkf_text', $fromUserName);
                $guide->custom('wxkf_img', $fromUserName);
                break;
            // 添加微信认证专员
            case '添加专员':
            case '认证':
                $guide->custom('wxkfzy_text', $fromUserName);
                $guide->custom('wxkfzy_img', $fromUserName);
                break;
            // 订阅抽奖提醒
            case '4':
                $guide->custom('lotteryremind_text', $fromUserName);
                $guide->custom('lotteryremind_img', $fromUserName);
                break;
            // 打开通知
            case '5':
                $guide->custom('notice_text', $fromUserName);
                $guide->custom('notice_img', $fromUserName);
                break;
            // 订阅匹配通知
            case '6':
                $guide->custom('pair_notice_text', $fromUserName);
                $guide->custom('pair_notice_img', $fromUserName);
                break;
            // 进入公众号聊天
            case '8':
            case '聊天':
                $guide->custom('intoroom_text', $fromUserName);
                $guide->custom('intoroom_img', $fromUserName);
                break;
            // 联系客服小象导
            case '0':
            case '客服':
                $guide->custom('kfxxd_text', $fromUserName);
                $guide->custom('kfxxd_img', $fromUserName);
                break;
            // 心动邀请订阅通知
            case '2':
            case '邀请':
            case '心动邀请':
                $guide->custom('invitation_text', $fromUserName);
                $guide->custom('invitation_img', $fromUserName);
                break;
            // 订阅晚安伴侣|留言通知
            case '晚安':
            case '晚安伴侣':
                $guide->custom('goodnight_chat_text', $fromUserName);
                $guide->custom('goodnight_chat_img', $fromUserName);
                break;
            // 订阅开黑通知
            case '开黑':
            case '开黑通知':
                $guide->custom('gteam_notice_text', $fromUserName);
                $guide->custom('gteam_notice_img', $fromUserName);
                break;
            // 激活会员
            case '会员':
                $guide->custom('tobe_vip_text', $fromUserName);
                $guide->custom('tobe_vip_img', $fromUserName);
                break;
            // 领取会员红包
            case '红包':
                $guide->custom('get_vip_flower_text', $fromUserName);
                $guide->custom('get_vip_flower_img', $fromUserName);
                break;
            default:
                $guide->custom('gh_01c089b58dda_default', $fromUserName);
                break;
        }
    }

    private function server72h($msg)
    {
        $guide = new Guide();
        switch ($msg->MsgType) {
            case 'text':
                switch ($msg->Content) {
                    case '0': # 活动报名    kd_ce5103f65339
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_0', $msg->FromUserName);
                        break;
                    case '1': # 活动报名    gh_01c089b58dda
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img', $msg->FromUserName);
                        break;
                    case '2': # 活动报名    kd_5edc93aa148e
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_2', $msg->FromUserName);
                        break;
                    case '3': # 活动报名    kd_qqzonesaohui
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_3', $msg->FromUserName);
                        break;
                    case '4': # 活动报名    kd_qzoneguanzhu
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_4', $msg->FromUserName);
                        break;
                    case '5': # 活动报名    kd_b598cb7474d8
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_5', $msg->FromUserName);
                        break;
                    case '6': # 活动报名    kd_wxkefuxiaoyu
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_6', $msg->FromUserName);
                        break;
                    case '7': # 活动报名    kd_fuwuhaoqunfa
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_7', $msg->FromUserName);
                        break;
                    case '8': # 活动报名    kd_dingyuequnfa
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_8', $msg->FromUserName);
                        break;
                    case '9': # 活动报名    kd_laiyuanpuapp
                        $guide->custom("enroll_text", $msg->FromUserName);
                        $guide->custom('enroll_img_9', $msg->FromUserName);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }
}
