<?php

namespace App\Http\Controllers\Fpdx;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Wechat\Media;
use App\Http\Controllers\Wechat\Kfaccount;
use App\Http\Controllers\Core\User;
use App\Http\Controllers\Core\Ding;
use App\Models\Fpdx\TaskModel;
use App\Models\User\UserModel;
use App\Models\Fpdx\RoomModel;
use App\Models\Fpdx\ActivityModel;

class Task extends Controller
{
    /**
     * 完成任务通知
     * @param  Request  $request
     * @return array|mixed
     * @throws \ApiException
     */
    public function complate(Request $request)
    {
        if (
            empty($request->room_id)
            || empty($request->day)
            || empty($request->uid)
        ) {
            return [
                'code' => 10005,
                'msg' => '参数错误'
            ];
        }
        // uid -> 绑定openid
        $openData = User::getOpenidByUid($request->uid);
        // 期数，房间号,匹配对象uid
        $roomModel = new RoomModel();
        $room = $roomModel->getRoom($request->room_id, $request->uid);

        // 匹配对象照片;
        $pairUser = UserModel::find($room->member[0]);
        // 任务及信息推送
        switch ($request->day) {
            case '1':
                // 任务完成推送
                $content = "你已经完成第一天的恋爱任务！解锁对方交友卡片，<a href='http://www.pocketuniversity.cn/ChargePartner/Http/index?media_id=gh_b598cb7474d8&room_id={$request->room_id}&base_route=couple&page=ucard&ucard_id={$pairUser->uid}&from=fpdx'>点此查看</a>";
                $array = [
                    'touser' => $openData['openid'],
                    'content' => $content,
                    'kf_account' => 'notice@admin'
                ];
                Kfaccount::text($array);
                break;
            case '2':
                if (!$pairUser->check_photo) {
                    break;
                }
                // 任务完成推送
                $content = "你已经完成第二天的恋爱任务！解锁对方照片，<a href='http://www.pocketuniversity.cn/ChargePartner/Http/index?media_id=gh_b598cb7474d8&room_id={$request->room_id}&base_route=couple&page=ucard&ucard_id={$pairUser->uid}&from=fpdx'>点此查看</a>";
                $array = [
                    'touser' => $openData['openid'],
                    'content' => $content,
                    'kf_account' => 'notice@admin'
                ];
                Kfaccount::text($array);
                // 交友照片推送
                $img = "http://oss.pocketuniversity.cn{$pairUser->photo_src}";
                $response = Media::uploadImg($img);
                if ($response['code'] == 0) {
                    Kfaccount::image($openData['openid'], ['media_id' => $response['data']['media_id']]);
                } else {
                    return $response;
                }
                break;
            case '3':
                if (!$pairUser->check_contact) {
                    break;
                }
                // 任务完成推送
                $content = "你已经完成第三天的恋爱任务！解锁对方联系方式，<a href='http://www.pocketuniversity.cn/ChargePartner/Http/index?media_id=gh_b598cb7474d8&room_id={$request->room_id}&base_route=couple&page=ucard&ucard_id={$pairUser->uid}&from=fpdx'>点此查看</a>";
                $array = [
                    'touser' => $openData['openid'],
                    'content' => $content,
                    'kf_account' => 'notice@admin'
                ];
                Kfaccount::text($array);

                // 联系方式推送
                $content_1 = "TA的联系方式是：";
                $content = "";
                if ($pairUser->qq) {
                    $content .= "{$pairUser->qq}(QQ)|";
                }
                if ($pairUser->weixin) {
                    $content .= "{$pairUser->weixin}(微信)";
                }
                if (!empty($content)) {
                    $content = $content_1 . $content;
                    $array = [
                        'touser' => $openData['openid'],
                        'content' => $content,
                        'kf_account' => 'notice@admin'
                    ];
                    Kfaccount::text($array);
                    break;
                }
        }
        return [
            'code' => 0,
            'msg' => '成功'
        ];
    }

    /**
     * 自动创建分配对象当前期cp任务
     */
    public static function autoCreateCpTask()
    {
        $taskModel = new TaskModel();
        $activitys = ActivityModel::getActivitys();
        $stage_id = $activitys['next'];
        $task = $taskModel->where('term', $stage_id)->first();
        if (collect($task)->isEmpty()) {
            $lastTask = $taskModel->where('term', $activitys['last'])->get();
            if (collect($lastTask)->isEmpty()) {
                Ding::robot([
                    'title' => '[分配对象]CP任务创建提醒',
                    'text' => "尝试创建分配对象第「{$stage_id}」期CP任务...\n没有找到上期任务...\n放弃创建\n"
                ]);
            } else {
                foreach ($lastTask as $value) {
                    $taskModel->create([
                        'data' => $value->data,
                        'term' => $stage_id,
                        'day' => $value->day
                    ]);
                }
                Ding::robot([
                    'title' => '[分配对象]CP任务创建提醒',
                    'text' => "尝试创建分配对象第「{$stage_id}」期CP任务...\n创建成功\n"
                ]);
            }
        } else {
            Ding::robot([
                'title' => '[分配对象]CP任务创建提醒',
                'text' => "尝试创建分配对象第「{$stage_id}」期CP任务...\n该任务易存在...\n放弃创建\n"
            ]);
        }
    }
}
