<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\V1\Controller;
use App\Models\BaseSettingsModel;
use App\Models\DreamImages;
use App\Models\DreamInfoModel;
use App\Models\SystemInfoModel;
use App\Models\UserCareUser;
use App\Models\UserDream;
use App\Models\UserInfoModel;
use Illuminate\Http\Request;
use App\Services\Base\ErrorCode;
class HomeController extends Controller
{
    /**
     * @api {get} /api/user/index/ 用户信息(index)
     * @apiDescription 用户信息(index)
     * @apiGroup Home
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} user_id 用户ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *  "nickname": "",  名字
     *  "pic": "",   头像
     *  "tall": "",  身高
     *  "job": "",  职业
     *  "emotion": "1",  情感
     *  "address": "",地址
     *  "score": 1000,  支持分
     *  "care": 4,  关注
     *  "fens": 3,   粉丝
     *  "user_dream": [
     *      "id": 3,
     *      "dream": "3去旅游",
     *      "about": "",
     *      "money": 0,
     *      "time": 0,
     *      "get_money": 0,
     *      "mark": 0,
     *      "status": 0,
     *      "pic": [],
     *      "dream_imgs": []
     *      "near_dream_pic": [
     *          {
     *              "pic": "234"
     *          },
     *    ]
     *  }
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     *{
     *"status": false,
     *"status_code": 1105,
     * "message": "用户不存在",
     *"data": null
     *  }
     */
    public function index(Request $request)
    {
        $user_id = $request->user_id;
        $care = UserCareUser::where('user_id',$user_id)->get();
        $fens = UserCareUser::where('other_user_id',$user_id)->get();
        $user = UserInfoModel::find($user_id);
        if (count($user == 0)) return $this->error(ErrorCode::USER_DOES_NOT_EXIST);
        $job = BaseSettingsModel::where(['category' => 'job'])->where(['key' => $user->job])->first();
        $job = count($job) > 0 ? $job->value : '';
        $emotion = BaseSettingsModel::where(['category' => 'emotion'])->where(['key' => $user->emotion])->first();
        $emotion = count($emotion) > 0 ? $emotion->value : '';
//        当前梦想
        $near_dream_id = UserDream::where('user_id',$user_id)->orderBy('id','desc')->first()->dream_id;
        $near_dream =DreamInfoModel::find($near_dream_id);
//         封面图片
        $near_dream_pic = DreamImages::where('dream_id',$near_dream_id)->select('pic')->get();
//        曾经的梦想
        $dreams = $user->UserDream;

        foreach ($dreams as $dream){
            $dream->pic = $dream->dreamImgs;
        }

        $user->score = 1000;//自定义 算法
        $user->care = count($care);
        $user->fens = count($fens);
        $user->job = $job;
        $user->emotion = $emotion;
//         支持的梦想
        $sup_dreams = $user->supDream;

        foreach ($sup_dreams as $sup_dream){
            $sup_dream->pic = $sup_dream->dreamImgs;
        }

        return $this->api(compact('user','near_dream','sup_dreams','near_dream_pic'));

    }

    /**
     * @api {post} /api/user/support 支持梦想
     * @apiDescription 支持梦想
     * @apiGroup Home
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} coin 支持梦想币数量
     * @apiParam {int} dream_id 梦想ID
     * @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 []
     * }
     *  可能出现的代码
     * {
    *       "status": false,
    *       "status_code": 1303,
    *       "message": "商户余额不足",
    *       "data": null
    *  }
     *
     */
    public function support(Request $request)
    {
        $user = $this->getUser();
        $dream_id = $request->dream_id;
        $dream_info = DreamInfoModel::find($dream_id);
        $validator = \Validator::make($request->all(),
            [
                'coin'  => 'required',
                'dream_id'  => 'required',
            ],
            [
                'coin.required'  => '梦想币不能为空',
                'dream_id.required'  => '支持对象不能为空',
            ]
        );

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

        $coin = $request->coin;

        if ($user->money < $coin) {
            return $this->error(ErrorCode::MERCHANT_BALANCE_NOT_ENOUGH);
        }else{
            $user->money  =  $user->money - $coin;
            $user->save();
            $dream_info->get_money += $coin;
            $dream_info->save();
            $data = [
                'user_id'=>$user->id,
                'other_id'=>$dream_id,
                'coin'=>$coin,
            ];

          $ok =   SystemInfoModel::create($data);
            if (!$ok) {
                return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
            }
            return $this->api('');
        }
    }
    /**
     * @api {post} /api/user/interaction 互动
     * @apiDescription 互动
     * @apiGroup Home
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} user_id 用户ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status": true,
     *      "status_code": 0,
     *      "message": "",
     *      "data": {
     *          评论的梦想
     *          "dream1": [
     *              {
     *                  "dream": "1的梦想",
     *                  "about": "介绍",
     *                  "pic": [
     *                      {
     *                          "pic": "111",
     *                      }
     *          ],
     *      "user": [
     *          {
     *              "pic": "",
     *       ],
     *       "comments": [
     *           {
     *              "level": 0,
     *              "content": "EST",
     *              "created_at": "2017-04-27 12:17:20",
     *           }
     *        ],
     *       回复的梦想
     *           "dream2": [
     *               {
     *                 "level": 0,
     *                  "content": "EST",
     *                  "created_at": "2017-04-27 12:17:20",
     *                  "updated_at": null,
     *                  "deleted_at": null,
     *                  "reply_dream": {
     *                      "dream": "1的梦想",
     *                      "about": "介绍",
     *                      "time": 0,
     *                      "dream_imgs": [
     *                          {
     *                              "pic": "111",
     *                          },
     *              ]
     *       },
     *      "reply_dream_pic": [
     *          {
     *              "pic": "111",
     *          }
     *       ],
     *      "reply_created_at": {
     *           "date": "2017-06-13 02:26:31.000000",
     *          "timezone_type": 3,
     *          "timezone": "UTC"
     *      },
     *      "reply_content": "haha",
     *      "reply_level": 0,
     *      "dream": {
     *          "dream": "1的梦想",
     *          "about": "介绍",
     *          "money": 5000,
     *          "time": 0,
     *          "dream_imgs": [
     *              {
     *                  "pic": "111",
     *              },
     *          ]
     *       }
     *   }
     *  ]
     *  }
     * }
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     * {
     *       "status": false,
     *      "status_code": 1500,
     *      "message": "会员不存在",
     *      "data": null
     * }
     */
    public function interaction(Request $request)
    {
        $user_id = $request->user_id;
        $user = UserInfoModel::find($user_id);
        if (count($user) == 0) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
//        参与的评论与回复 梦想
        $dream1 = $user->comDream;

        foreach ($dream1 as $item){
            $item->pic = $item->dreamImgs;
            $item->user = $item->dreamUser;
            $item->comments = $item->DreamInfo;
            $item->created_at = $item->pivot->created_at;
            $item->content = $item->pivot->content;
            $item->level = $item->pivot->level;
        }

        $dream2 = $user->replyDream;

        foreach ($dream2 as $comment) {
            $comment->reply_dream = $comment->dream;
            $comment->reply_dream_pic = $comment->dream->dreamImgs;
            $comment->reply_created_at = $comment->pivot->created_at;
            $comment->reply_content = $comment->pivot->content;
            $comment->reply_level = $comment->pivot->level;
        }

        return $this->api(compact('dream1','dream2'));
    }
    /**
     * @api {post} /api/Home/paihang 排行
     * @apiDescription 排行
     * @apiGroup Home
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} user_id 用户ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *    "status": true,
     *    "status_code": 0,
     *   "message": "",
     *   "data": {
     *       "arr3": [
     *          {
     *              "nickname": "",  昵称
     *              "pic": "",      头像
     *              "coin": 23      总支持梦想币
     *          }
     *      ]
     *   }
     *}
     *
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     *{
     *  "status": false,
     *  "status_code": 1105,
     *  "message": "用户不存在",
     *  "data": null
     * }
     */

    public function paihang(Request $request)
    {
//      获取支持过用户的人
        $user_id = $request->user_id;
        $user = UserInfoModel::find($user_id);
        if (count($user) == 0) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
        $dreams = $user->UserDream;
        $arr1 = [];

        foreach ($dreams as $dream) {
            $arr1[] = $dream->systemInfo;
        }

//      用户总的支持梦想币
        $arr2 = [];

        foreach ($arr1 as $v) {
            foreach ($v as $item){
                if (!array_key_exists($item->user_id,$arr2)) {
                    $arr2[$item->user_id] = $item->coin;
                }else{
                    $arr2[$item->user_id] += $item->coin;
                }
            }
        }

        $arr3 = [] ;

        foreach ($arr2 as $k => $v){
            $user = UserInfoModel::find($k);
            $user->coin = $v;
            $arr3[] = $user;
        }

        return $this->api(compact('arr3'));

    }
}
