<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\CommentInfoModel;
use App\Models\DreamInfoModel;
use App\Models\InteractionInfo;
use App\Models\ReplyCommentsInfo;
use App\Models\SystemInfoModel;
use App\Models\UserCareDream;
use App\Models\UserCareUser;
use App\Models\UserInfoModel;
use Illuminate\Http\Request;
use App\Services\Base\ErrorCode;
use App\Helper\JpushHelper;

class InteractionController extends Controller
{
    use JpushHelper;
    //    发布关于梦想的动态
    /**
     * @api {post} /api/interaction/store 新增动态
     * @apiDescription 新增动态
     * @apiGroup Interaction
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} id                 梦想ID
     * @apiParam {string} title           互动标题
     * @apiParam {string} [video]           视频
     * @apiParam {array} [pics[]]           图片数组
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *      "status": true,
     *      "status_code": 0,
     *      "message": "",
     *      "data": ""
     *}
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     *
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'title'  => 'required',
            ],
            [
                'id.required'  => '梦想ID不能为空',
                'title.required'  => '动态标题不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
        $data = [];
        $pics = $request->pics;
        if (empty($pics) || !is_array($pics)) {
//            return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
        }else{
            foreach ($pics as $k => $pic) {
                $data['pic'.($k+1)] = $pic;
            }
        }
        $dream_id = $request->id;
        $title = $request->title;
        $data['dream_id'] = $dream_id;
        $data['title'] = $title;
        $data['video'] = $request->video;
        $ok = InteractionInfo::create($data);
        if ($ok) {
//            收藏梦想最新动态加一
            UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
            $dream_user_id = DreamInfoModel::find($dream_id)->user_id;
            UserCareUser::where('other_user_id',$dream_user_id)->update(['dream_id'=>$dream_id,'dream_number'=>'1']);
//            UserCareUser::where('dream_id',$dream_id)->increment('interaction_number',1);
            return $this->api('');
        }else{
            return $this->error(ErrorCode::SAVE_USER_FAILED);
        }
    }

    //    评论互动

    /**
     * @api {post} /api/interaction/comment 评论动态
     * @apiDescription 评论动态
     * @apiGroup Interaction
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} id                   动态ID不存在
     * @apiParam {int}   [comment_user_id]          //已经评论者id
     * @apiParam {string} content           内容不能为空
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *      "status": true,
     *      "status_code": 0,
     *      "message": "",
     *      "data": ""
     *}
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     *
     */
    public function comment(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'content'  => 'required',
            ],
            [
                'id.required'  => '动态ID不存在',
                'content.required'  => '内容不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

        $user = $this->getUser();
       /* $user_id = $user->id;
        $user_avatar = $user->avatar;
        $user_nickname = $user->nickname;
        $interaction_id = $request->id;
        $content = $request->content;
        $is_read = 1;
        $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/

//        $data['to_user_id'] = InteractionInfo::find($request->id)->dream->user_id;
//        $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
//        $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
        if (!empty($request->input('comment_user_id'))) {
            $to_user = UserInfoModel::find($request->input('comment_user_id'));
            if (!empty($to_user)){
                $data['to_user_id'] = $request->input('comment_user_id');
                $data['to_user_avatar'] = $to_user->avatar;
                $data['to_user_nickname'] = $to_user->nickname;

                $message = $user->nickname.'在你的互动上留言啦！点击去看看！';
                $info = [
                    'user_id' => $data['to_user_id'],
                    'message' => $message,
                ];
                SystemInfoModel::create($info);

//            长连接
                $this->jPush($message,'',$data['to_user_id']);
            }
        }

        $data['user_id'] = $user->id;
        $data['user_avatar'] =$user->avatar;
        $data['user_nickname'] = $user->nickname;
        $data['interaction_id'] = $request->id;
        $data['content'] = $request->content;
        $data['is_read'] = 1;
        $ok = CommentInfoModel::create($data);


        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }

    //    回复评论

    /**
     * @api {post} /api/interaction/reply 我的回复
     * @apiDescription 我的回复
     * @apiGroup Interaction
     * @apiParam {text}  content        回复内容
     * @apiParam {int} comment_id       评论ID
     * @apiParam {int} interaction_id       动态ID
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "status": true,
     *   "status_code": 0,
     *   "message": "",
     *   "data": ""
     *}
     * HTTP/1.1 200 OK
     * @apiErrorExample {json} Error-Response:
     * {
     *   "status": false,
     *   "status_code": 1000,
     *   "message": "输入不正确",
     *  "data": null
     *}
     * HTTP/1.1 400 Bad Request
     */
    public function reply(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'interaction_id'  => 'required',
                'content'  => 'required',
            ],
            [
                'id.required'  => '评论ID不存在',
                'content.required'  => '内容不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

        $user = $this->getUser();
      /*  $data['to_user_id'] = $user->id;
        $data['to_user_avatar'] = $user->avatar;
        $data['to_user_nickname'] = $user->nickname; */

        $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
//        $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
        $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;

        $data['user_id'] = $user->id;
        $data['user_avatar'] =$user->avatar;
        $data['user_nickname'] = $user->nickname;
        $data['interaction_id'] = $request->id;
        $data['content'] = $request->content;
        $data['is_read'] = 1;

        if (!$request->content)
            return $this->error(ErrorCode::CONNET_NOT_EXIST);

        $ok =  CommentInfoModel::create($data);

        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }

    /**
     * @api {get} /api/comment/delete 删除评论
     * @apiDescription 删除评论
     * @apiGroup Interaction
     * @apiParam {int} id       评论ID
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "status": true,
     *   "status_code": 0,
     *   "message": "",
     *   "data": ""
     *}
     * HTTP/1.1 200 OK
     * @apiErrorExample {json} Error-Response:
     * {
     *   "status": false,
     *   "status_code": 700,
     *   "message": "操作失败",
     *  "data": null
     *}
     * HTTP/1.1 400 Bad Request
     */
    public function delete(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
            ],
            [
                'id.required'  => '评论ID不存在',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

        $user = $this->getUser();


        $ok =  CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();

        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }

    /**
     * @api {get} /api/interaction/destroy 删除动态
     * @apiDescription 删除动态
     * @apiGroup Interaction
     * @apiParam {int} id       动态ID
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "status": true,
     *   "status_code": 0,
     *   "message": "",
     *   "data": ""
     *}
     * HTTP/1.1 200 OK
     * @apiErrorExample {json} Error-Response:
     * {
     *   "status": false,
     *   "status_code": 700,
     *   "message": "操作失败",
     *  "data": null
     *}
     * HTTP/1.1 400 Bad Request
     */
    public function destroy(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
            ],
            [
                'id.required'  => '动态ID不存在',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

//        $user = $this->getUser();

        $id = $request->input('id');
        CommentInfoModel::where('interaction_id',$id)->delete();
        $ok =  InteractionInfo::destroy($id);

        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }
}
