<?php

namespace App\Http\Controllers\Miniprogram;

use App\Services\App\KfService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User\AuthKey;
use App\Models\Common\FormidModel;

class Guide extends Controller
{
    /**
     * 发送客服消息
     * @param  string  $type
     * @param  string|null  $openid
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function custom(string $type, string $openid = null)
    {
        if (empty($openid)) {
            $uid = Auth::auth();
        } else {
            try {
                $akm = new AuthKey();
                $uid = $akm->getUidByKey($openid);
            } catch (\Exception $e) {
                $uid = null;
            }
        }
        $kfs = new KfService();
        switch ($type) {
            // 情报局报名
            case 'qbj_enroll_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓\n↓快速报名参加时遇记情报局↓\n（扫码后不错过活动动态）!"
                    ]
                ];
                break;
            case 'qbj_enroll_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia("qbj_enroll")
                    ]
                ];
                break;
            // 活动报名
            case 'enroll_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓\n↓快速报名72小时恋爱体验活动↓\n（扫码后不错过活动动态）!"
                    ]
                ];
                break;

            case 'enroll_img_0':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia("enrollkd_ce5103f65339")
                    ]
                ];
                break;
            case 'enroll_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia("enroll")
                    ]
                ];
                break;
            case 'enroll_img_2':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia("enrollkd_5edc93aa148e")
                    ]
                ];
                break;
            case 'enroll_img_3':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_qqzonesaohui')
                    ]
                ];
                break;
            case 'enroll_img_4':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_qzoneguanzhu')
                    ]
                ];
                break;
            case 'enroll_img_5':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_b598cb7474d8')
                    ]
                ];
                break;
            case 'enroll_img_6':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_wxkefuxiaoyu')
                    ]
                ];
                break;
            case 'enroll_img_7':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_fuwuhaoqunfa')
                    ]
                ];
                break;
            case 'enroll_img_8':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_dingyuequnfa')
                    ]
                ];
                break;
            case 'enroll_img_9':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('enrollkd_laiyuanpuapp')
                    ]
                ];
                break;

            // 进入公众号聊天
            case 'intoroom_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓\n↓按提示进入房间，体验极速聊天↓"
                    ],
                ];
                break;
            case 'intoroom_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('intoroom'),
                    ]
                ];
                break;

            // 订阅抽奖提醒
            case 'lotteryremind_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓\n↓开启抽奖通知，不错过中奖信息↓"
                    ],
                ];
                break;
            case 'lotteryremind_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('lotteryremind'),
                    ]
                ];
                break;

            // 72小时活动确认客服
            case 'wxkf_text':
                try {
                    $kfs = new KfService();
                    $wxid = $kfs->hashWxkf($uid);
                    if ($wxid) {
                        $data = [
                            'msgtype' => 'text',
                            'text' => [
                                'content' => "点击下方图片，长按扫码添加专属客服！！！\n\n添加后回复【CP】,即可确认CP关系"
                            ],
                        ];
                        \DB::table('kdgx_fpdx_wxkf')->where('wxid', $wxid)->increment('replay_cnt', 1);
                    } else {
                        $data = [
                            'msgtype' => 'text',
                            'text' => [
                                'content' => "点此确认CP关系: https://m.fenpeiduixiang.com/fpdx-bindwxid/index.html"
                            ],
                        ];
                    }
                } catch (\Exception $e) {
                    app('sentry')->captureException($e);
                    $data = [
                        'msgtype' => 'text',
                        'text' => [
                            'content' => "点此确认CP关系: https://m.fenpeiduixiang.com/fpdx-bindwxid/index.html"
                        ],
                    ];
                }
                break;
            case 'wxkf_img':
                try {
                    $kfs = new KfService();
                    $wxid = $kfs->hashWxkf($uid);
                    if ($wxid) {
                        $data = [
                            'msgtype' => 'image',
                            'image' => [
                                'media_id' => $kfs->getMedia($wxid),
                            ]
                        ];
                    } else {
                        return [
                            'code' => 200,
                            'message' => 'success'
                        ];
                    }
                } catch (\Exception $e) {
                    app('sentry')->captureException($e);
                    return [
                        'code' => 200,
                        'message' => 'success'
                    ];
                }
                break;

            // 添加微信认证专员
            case 'wxkfzy_text':
                try {
                    if (is_null($uid)) {
                        $data = [
                            'msgtype' => 'text',
                            'text' => [
                                'content' => "用户身份识别失败,无法获取认证客服号。请按以下流程操作：1. 点击长按下方图片，绑定身份。\n2. 重新进入小程序客服消息(即当前页面)回复[认证]获取认证客服号。\n3. 如还是\"用户身份识别失败,无法获取认证客服号\",回复「客服」联系小象导，并提供此错误码:「{$openid}」（直接复制，错误码尽量不要截图)和你的uid（不知道uid?可以咨询小象导)"
                            ],
                        ];
                    } else {
                        $kfs = new KfService();
                        $wxid = $kfs->hashWxshzy($uid);
                        if ($wxid) {
                            $data = [
                                'msgtype' => 'text',
                                'text' => [
                                    'content' => "点击长按下方图片，添加审核专员\n👇👇👇👇👇\n\n添加后回复「认证」，快速认证"
                                ],
                            ];
                        } else {
                            $data = [
                                'msgtype' => 'text',
                                'text' => [
                                    'content' => "审核专员繁忙，请稍后重试。。。"
                                ],
                            ];
                        }
                    }
                } catch (\Exception $e) {
                    app('sentry')->captureException($e);
                    return [
                        'code' => 200,
                        'message' => 'success'
                    ];
                }
                break;
            case 'wxkfzy_img':
                try {
                    if (is_null($uid)) {
                        $data = [
                            'msgtype' => 'image',
                            'image' => [
                                'media_id' => $kfs->getMedia("bindauthkey"),
                            ]
                        ];
                    } else {
                        $kfs = new KfService();
                        $wxid = $kfs->hashWxshzy($uid);
                        if ($wxid) {
                            $data = [
                                'msgtype' => 'image',
                                'image' => [
                                    'media_id' => $kfs->getMedia($wxid),
                                ]
                            ];
                            \DB::table('kdgx_fpdx_wxkf')->where('wxid', $wxid)->increment('replay_cnt', 1);
                        } else {
                            $data = [
                                'msgtype' => 'text',
                                'text' => [
                                    'content' => "审核专员繁忙，请稍后重试。。。"
                                ],
                            ];
                            return [
                                'code' => 200,
                                'message' => 'success'
                            ];
                        }
                    }
                } catch (\Exception $e) {
                    app('sentry')->captureException($e);
                    return [
                        'code' => 200,
                        'message' => 'success'
                    ];
                }
                break;

            // 打开通知
            case 'notice_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓↓开启消息通知能力↓"
                    ]
                ];
                break;
            case 'notice_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('notice')
                    ]
                ];
                break;

            // 心动邀请订阅通知
            case 'pair_notice_text':
            case 'invitation_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓↓订阅提醒↓↓↓"
                    ]
                ];
                break;
            case 'invitation_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('invitation')
                    ]
                ];
                break;

            // 订阅匹配结果通知
            case 'pair_notice_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('pair_notice')
                    ]
                ];
                break;

            // 订阅开黑通知
            case 'gteam_notice_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓↓订阅提醒↓"
                    ]
                ];
                break;
            case 'gteam_notice_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('gteam_notice')
                    ]
                ];
                break;

            // 联系客服小象导
            case 'kfxxd_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓↓联系客服,碰到任何问题，都可以来找小象导哦～↓"
                    ]
                ];
                break;
            case 'kfxxd_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('fpdxkfgzh')
                    ]
                ];
                break;

            // 订阅晚安伴侣|留言通知
            case 'goodnight_chat_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码↓↓↓获取提醒↓↓↓"
                    ]
                ];
                break;
            case 'goodnight_chat_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('goodnight_chat')
                    ]
                ];
                break;

            // 激活会员
            case 'tobe_vip_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码马上激活会员。"
                    ]
                ];
                break;
            case 'tobe_vip_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('tobe_vip')
                    ]
                ];
                break;

            // 领取会员红包
            case 'get_vip_flower_text':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "点击下方图片，长按扫码领取每日红包"
                    ]
                ];
                break;
            case 'get_vip_flower_img':
                $data = [
                    'msgtype' => 'image',
                    'image' => [
                        'media_id' => $kfs->getMedia('get_vip_flower')
                    ]
                ];
                break;

            case "app":
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "<a href='https://a.app.qq.com/o/simple.jsp?pkgname=com.fenpeiduixiang.www'>点此下载/更新分配对象APP-></a>


重要更新 v1.0.9：
1、正式上线72小时CP功能，大家可以在App中报名参与活动啦！超即时通知，不错过活动动态；
2、修复已知Bug，细节优化完善；
3、v1.1.0在路上，请大家期待。"
                    ]
                ];
                break;

            case 'gh_01c089b58dda_default':
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "回复:[1]活动报名(公众号快速通道)
回复:[2]打开「好友/心动」消息提醒
回复:[3]确认CP关系
回复:[4]抽奖提醒
回复:[5]打开通知
回复:[客服]联系客服
回复:[认证]添加微信认证专员
回复:[聊天]开启公众号聊天
回复:[123]下载/更新分配对象App"
                    ],
                ];
                break;
            default:
                $data = [
                    'msgtype' => 'text',
                    'text' => [
                        'content' => "很抱歉，消息通知/聊天等相关功能正在维护中，请稍后再试\n\n*如需联系客服，请搜索关注公众号【分配对象】点按菜单->72小时恋爱->反馈/联系客服\n\n*如需建立聊天链接，请搜索关注公众号【分配对象】点按菜单->聊天列表"
                    ],
                ];
                break;
        }
        $data['touser'] = $openid;
        $core = new Core();
        $core->custom($uid, $data);
        return [
            'code' => 200,
            'message' => 'success'
        ];
    }

    /**
     * 记录formid
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function logFormid(Request $request)
    {
        $this->validate($request, [
            'form_id' => 'required',
        ]);
        if ($request->form_id == 'the formId is a mock one') {
            return response([
                'code' => 200,
                'message' => 'success'
            ]);
        }
        $public_id = config('miniprogram.public_id');
        try {
            $uid = Auth::auth();
            $authKey = new AuthKey();
            $openid = $authKey->getKeyOfType($uid, $public_id);
        } catch (\Exception $exception) {
            $uid = 0;
            $openid = $request->get('openid');
            if (empty($openid)) {
                return response([
                    'code' => 200,
                    'message' => 'success'
                ]);
            }
        }

        FormidModel::create([
            'uid' => $uid,
            'openid' => $openid,
            'form_id' => $request->form_id,
            'public_id' => $public_id,
            'action_id' => $request->post('action_id') ?? 0,
            'extra' => $request->post('extra') ? json_encode($request->post('extra')) : null,
        ]);
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }
}
