<?php

namespace App\Http\Controllers\Wehub;

use App\Http\Controllers\Controller;
use App\Jobs\WxkfUserInfoJob;
use App\Models\Fpdx\WxkfModel;
use App\Models\Wxkf\UserInfoModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ListenController extends Controller
{
    public const FPDX = "gh_b598cb7474d8";

    public const XXD = "gh_e21151d57cc0";

    public function listening(Request $request)
    {
        $data = array(
            'reply_task_list' => array()
        );
        $task = array();
        $body = $request->post();
        switch ($body['action']) {
            case 'login': // 登录
                $ack_type = "login_ack";
                $wxkf = WxkfModel::firstOrCreate([
                    'wxid' => $body['wxid']
                ], [
                    'created_at' => time(),
                    'updated_at' => time(),
                    'nickname' => $body['data']['nickname'],
                    'day_limit' => 200,
                    'state' => 0
                ]);
                break;
            case 'logout': // 退出登录
                $ack_type = "logout_ack";
                $wxkf = WxkfModel::where('wxid', $body['wxid'])->first();
                if (!collect($wxkf)->isEmpty()) {
                    $wxkf->state = 0;
                    $wxkf->save();
                }
                break;
            case 'report_friend_add_request': // 收到添加好友申请
                $ack_type = "common_ack";
                // 检验今日加好友数
                $wxkf = WxkfModel::where('wxid', $body['wxid'])->first();
                if (collect($wxkf)->isEmpty()) {
                    break;
                }
                if ($wxkf->today_add_cnt >= $wxkf->day_add_limit) {
                    break;
                }
                // 创建自动同意添加好友请求加入延迟队列
                $task = array(
                    'task_type' => 13,
                    'task_dict' => array(
                        'v1' => $body['data']['v1'],
                        'v2' => $body['data']['v2']
                    )
                );
                Redis::Sadd("wehub:task:list:{$body['wxid']}", json_encode($task));
                WxkfModel::where('wxid', $body['wxid'])->increment('today_add_cnt', 1);
                break;
            case 'report_new_friend': // 成功添加新的好友
                $ack_type = "common_ack";
                $userinfo = UserInfoModel::updateOrCreate(
                    array(
                        'wxkf_wxid' => $body['wxid'],
                        'wxid' => $body['data']['fans_wxid']
                    ),
                    array(
                        'wxkf_wxid' => $body['wxid'],
                        'wxid' => $body['data']['fans_wxid'],
                        'nickname' => $body['data']['nickname'],
                        'head_img' => $body['data']['head_img'],
                        'wx_alias' => empty($body['data']['wx_alias']) ? null : $body['data']['wx_alias']
                    )
                );
                $wxkf = WxkfModel::where('wxid', $body['wxid'])->first();
                if (collect($wxkf)->isEmpty()) {
                    break;
                }
                WxkfModel::where('wxid', $body['wxid'])->increment('friend_cnt', 1);
                if ($wxkf->type == "活动") {
                    $task = array(
                        'task_type' => 1,
                        'task_dict' => [
                            'wxid_to' => $body['data']['fans_wxid'],
                            'at_list' => [],
                            'msg_list' => [
                                [
                                    'msg_type' => 1,
                                    'msg' => "嗨，亲爱的小伙伴，我是你的活动专属小遇~\n小遇会在朋友圈分享好看的小哥哥小姐姐，还有恋爱小技巧等[皱眉]今后让小遇带你好好恋爱~"
                                ],
                                [
                                    'msg_type' => 1,
                                    'msg' => "点此确认CP关系: https://m.fenpeiduixiang.com/fpdx-bindwxid/index.html?wxid={$body['data']['fans_wxid']}"
                                ]
                            ]
                        ]
                    );
                } else {
                    $task = array(
                        'task_type' => 1,
                        'task_dict' => [
                            'wxid_to' => $body['data']['fans_wxid'],
                            'at_list' => [],
                            'msg_list' => [
                                [
                                    'msg_type' => 1,
                                    'msg' => "嗨!我是你的审核专员。\n\n请点击以下链接认证你的微信号: https://m.fenpeiduixiang.com/fpdx-wxauth/index.html?wxid={$body['data']['fans_wxid']}&kf={$body['wxid']}"
                                ]
                            ]
                        ]
                    );
                }
                array_push($data['reply_task_list'], $task);
                break;
            case 'pull_task': // 获取客户端任务
                $ack_type = "common_ack";
                $caches = Redis::Smembers("wehub:task:list:{$body['wxid']}");
                $cnt = 0;
                foreach ($caches as $cache) {
                    if ($cnt > 9) {
                        break;
                    }
                    $value = json_decode($cache, true);
                    array_push($data['reply_task_list'], $value);
                    Redis::Srem("wehub:task:list:{$body['wxid']}", $cache);
                    $cnt++;
                }
                break;
            case 'report_new_msg': // 收到新消息
                $ack_type = "common_ack";
                if (!empty($body['data']['msg']['room_wxid'])) {
                    break;
                }
                if ($body['data']['msg']['wxid_from'] == $body['data']['msg']['wxid_to']) {
                    break;
                }
                switch ($body['data']['msg']['msg_type']) {
                    case 1:
                        $msg = strtoupper($body['data']['msg']['msg']);
                        if ($msg == 3 || $msg == "CP") {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "点此确认CP关系: https://m.fenpeiduixiang.com/fpdx-bindwxid/index.html?wxid={$body['data']['msg']['wxid_from']}"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if ($msg == "认证" || $msg == "验证" || $msg == "添加专员") { // 验证微信号码
                            $jm_wxid = $body['data']['msg']['wxid_from'];
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "请点击以下链接认证你的微信号: https://m.fenpeiduixiang.com/fpdx-wxauth/index.html?wxid={$jm_wxid}&kf={$body['wxid']}"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }

                        if ($msg == "人工认证") {
                            $jm_wxid = $body['data']['msg']['wxid_from'];
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "点击链接提醒小遇人工审核\nhttps://m.fenpeiduixiang.com/fpdx-wxauth/artifical.html?wxid={$jm_wxid}&kf={$body['wxid']}"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }

                        if (strpos($msg, "错过活动时间被解散") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，错过活动时间被解散是指，一方选择重新匹配而另一方未在规定时间内上线签到或是选择重新匹配，系统则解除CP关系并结束本期活动。下一次，要记得准时上线确认关系喔！（若是查看资料后不满意则可以不点击确认关系，直接选择重新匹配即可）"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "取消重配") !== false || strpos($msg, "取消重新匹配") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，点击重配后是无法取消的喔，并且重新配对也不一定会配对成功。点击重配之前一定要考虑清楚喔！如果你选择了重新匹配，那么系统会以短信或者模板消息的方式提醒之前匹配到的CP说明你已经选择重配！"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "报名") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，可以在【分配对象】公众号中回复“报名”，点击报名链接填写相关报名信息即可报名。也可在微信中搜索【分配对象】小程序，进入72小时活动主页点击报名本期即可参与"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::FPDX
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (
                            strpos($msg, "匹配") !== false && (strpos($msg, "失败") === false || strpos(
                                $msg,
                                "没有"
                            ) === false)
                        ) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "系统会按照以上几个匹配原则为每期报名的用户两两计算匹配分，匹配分越高的两个用户匹配到一起的几率越高。也就是地域只占你们匹配分的40%，你们如果能被系统匹配到一起，是因为是由地域，年龄，身高，个性标签等综合因素共同决定的/爱情"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (
                            (strpos($msg, "通过") !== false)
                            && (strpos($msg, "审核") !== false
                                || strpos($msg, "卡片") !== false
                                || strpos($msg, "资料") !== false
                                || strpos($msg, "信息") !== false)
                        ) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "若个人资料卡片显示不通过，那么可以检查：图片是否为真人真实图片；联系方式是否真实有效；自我描述是否填写。若以上内容均已填写可以再次上传等待工作人员审核喔"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "充值") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "进入“分配对象”公众号--输入“个人中心”--在个人中心资料卡片下点击小红花进行充值。【Android】：在小程序【分配对象】个人卡片下点击充值小红花"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::FPDX
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "充值") !== false && strpos($msg, "卡片") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "下架卡片的步骤：在小程序【分配对象】点击“我”---点击“查看/修改我的卡片”---点击上方的“编辑”----点击“上架中”---点击“下架，下架自己的卡片后就不会有人看到你的信息了"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::FPDX
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (
                            (strpos($msg, "匹配") !== false
                                || strpos($msg, "CP") !== false)
                            && (strpos($msg, "失败") !== false
                                || strpos($msg, "没有") !== false)
                        ) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "如若当期匹配失败，没有匹配到CP，可以在匹配界面选择退款或报名下一期"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "退款") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，可以在当期活动结束后（周一早9:00）联系客服工作人员提供UID并说明退款原因进行退款操作~↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "性别") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，若是要修改性别，可以提供你的UID给客服工作人员进行修改~↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "性取向") !== false || strpos($msg, "同性") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，如若已经报名成功，活动未开始前可以联系客服并提供UID进行修改！ 活动开始后，可以在匹配历史界面选择落单登记，进入落单重新配对！ 下期报名要记得确认另一半是要小姐姐还是小哥哥喔↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "不满意") !== false || strpos($msg, "不走心") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "当期活动中，如果觉得对方不合适，可以选择“重新配对”或者和CP协商，然后共同选择落单登记喔（匹配历史界面点击落单登记）~或者也可以在活动结束后（下周一早9：00）联系客服，说明情况并提供UID，进行退款或者是免费报名下一期活动喔！↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "谢谢") !== false || strpos($msg, "麻烦") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "感谢对我们的支持喔~/可爱"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "活动群") !== false || strpos($msg, "群号") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "进入匹配历史界面，点击确认关系（需要双方都确认关系）后，即可解锁活动群号以及CP编号，复制qq群号并搜索加入，修改备注为CP编号。\n注意事项：1）若CP未上线确认关系，可联系CP上线确认关系，若无法联系CP，可以选择重新匹配。2）若解锁活动群后只有一方加入活动群，或双方加入活动群却未“修改备注为CP编号”，小遇会提醒你邀请CP或修改编号，修改之后确认后，即可正常在活动群聊中进行活动。"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "落单群") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "匹配历史界面点击“落单登记”，添加落单小遇工作人员个人qq号码，说明情况，等待小遇拉入落单群。"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "取消报名") !== false || strpos($msg, "退出报名") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "你好，当期活动未开始之前是可以取消报名的，可以在工作日时间联系客服人员提供UID为你取消报名。每周四12：00后则无法取消报名！若当期活动已经开始，则无法取消报名，可在当期活动结束后（下周一9：00）联系客服小象提供UID并说明原因进行退款或免费报名下一期活动操作喔！↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "审核") !== false || strpos($msg, "加急") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "若卡片长时间未审核通过，是因为审核对列里的卡片过多！ 若要加急审核，请提供你的UID，方便工作人员帮你操作！↓ ↓ ↓ 点击下方名片联系客服，客服人员上班时间：周一到周五的9:00到21:00"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::XXD
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "UID") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "这是你在系统里的唯一身份标识。在”分配对象“公众号中回复UID即可查询你自己的UID。注意喔，UID是一串阿拉伯数字！"
                                        ],
                                        [
                                            'msg_type' => 42,
                                            'wxid_card' => self::FPDX
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (
                            strpos($msg, "群") !== false
                            && (strpos($msg, "失败") !== false
                                || strpos($msg, "无法") !== false
                                || strpos($msg, "加不了") !== false
                                || strpos($msg, "加不进去"))
                        ) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "由于加群人数较多或比较频繁会出现此类情况，你可在群聊页面添加管理员小遇，由小遇将你拉入群内。"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }
                        if (strpos($msg, "任务") !== false) {
                            $task = array(
                                'task_type' => 1,
                                'task_dict' => [
                                    'wxid_to' => $body['data']['msg']['wxid_from'],
                                    'at_list' => [],
                                    'msg_list' => [
                                        [
                                            'msg_type' => 1,
                                            'msg' => "活动为帮助各位CP顺利破冰，在匹配成功后的活动环节中，小遇设计了有趣好玩的任务给各位CP们~活动任务将在“活动体验群”中公布，记得要尽快加群哦~"
                                        ]
                                    ]
                                ]
                            );
                            array_push($data['reply_task_list'], $task);
                            break;
                        }

                        $task = array(
                            'task_type' => 1,
                            'task_dict' => [
                                'wxid_to' => $body['data']['msg']['wxid_from'],
                                'at_list' => [],
                                'msg_list' => [
                                    [
                                        'msg_type' => 1,
                                        'msg' => "你好，请搜索关注公众号【分配对象】或点击下方名片点按菜单--72小时恋爱--反馈/联系客服，提供UID并说明原因，↓ ↓ ↓ 客服人员上班时间：周一到周五的9:00到21:00"
                                    ],
                                    [
                                        'msg_type' => 42,
                                        'wxid_card' => self::FPDX
                                    ]
                                ]
                            ]
                        );
                        array_push($data['reply_task_list'], $task);

                        break;
                    default:
                        break;
                }
                break;
            case 'report_contact': // 上报当前好友列表和群列表
                $ack_type = "common_ack";
                // 加入异步队列
                $this->dispatch(new WxkfUserInfoJob($body['wxid'], $body['data']['friend_list']));
                break;
            case 'report_contact_update': // 上报成员信息变化
                $ack_type = "common_ack";
                // 加入异步队列
                $this->dispatch(new WxkfUserInfoJob($body['wxid'], $body['data']['update_list']));
                break;
            case 'report_task_result':
                $ack_type = "common_ack";
                break;
            case 'report_room_member_info':
                $ack_type = "common_ack";
                break;
            case 'report_room_member_change':
                $ack_type = "common_ack";
                break;
            case 'report_new_room':
                $ack_type = "common_ack";
                break;
        }
        return array(
            'error_code' => 0,
            'error_reason' => '',
            'ack_type' => $ack_type,
            'data' => $data
        );
    }

    public function upload()
    {
        $body = file_get_contents("php://input");
        dump($body);
    }
}
