<?php


namespace App\Http\Controllers\V1;


use App\Enums\ApiEnum;
use App\Http\Params\DynamicParam;
use App\Http\Params\DynamicZanParam;
use App\Http\Params\FeedsParam;
use App\Http\Requests\FeedsRequest;
use App\Models\Feed;
use App\Models\FeedForward;
use App\Services\Api\ErrorMsgServive;
use App\Services\Api\FeedsService;
use App\Services\Api\FriendService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use PHPUnit\Util\Exception;

class FeedsController extends Controller
{
    private $feedsService;

    public function __construct()
    {
        parent::__construct();
        $this->feedsService = new FeedsService();
    }

    //动态列表
    public function feedList(Request $request)
    {
        $data = $this->feedsService->getList($request);
        return $this->success($data);
    }

    //动态详情
    public function detail(Request $request)
    {
        $data = $this->feedsService->getDetail($request->feed_id);
        return $this->success($data);
    }

    //发布动态
    public function release(FeedsRequest $request)
    {
        try {
            $content    = $request->input('content', '');
            $fileUrl    = $request->input('file_url', '');
            $gameUserId = $request->input('game_per_id', '');

            if(empty($content) && empty($fileUrl) && empty($gameUserId)){
                throw new Exception(trans('api.NOT_EMPTY'));
            }

            $user = auth('api')->user();
            $lock = Cache::lock(ApiEnum::FEED_RELEASE . $user->id, 1);
            if ($lock->get()) {
                $this->feedsService->release($request);
                $lock->release();
            } else {
                throw new Exception(trans('api.REQUESTS_TOO_FREQUENTLY'));
            }
        } catch (\Exception $exception) {
            ErrorMsgServive::write($exception, request()->url());
            return $this->error($exception->getLine() . $exception->getMessage());
        }
        return $this->success();
    }

    //动态点赞
    public function like(Request $request)
    {
        try {
            $user = auth('api')->user();
            $lock = Cache::lock(ApiEnum::FEED_ZAN . $user->id, 1);
            if ($lock->get()) {
                DB::beginTransaction();
                $like = $this->feedsService->like($request);
                DB::commit();
                $lock->release();
            } else {
                throw new Exception(trans('api.REQUESTS_TOO_FREQUENTLY'));
            }
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->error($exception->getMessage());
        }

        $res['like'] = $like;
        return $this->success($res);
    }

    //删除动态
    public function del(Request $request)
    {
        try {
            $this->feedsService->del($request);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }

    //动态评论
    public function comment(Request $request)
    {
        try {
            $this->feedsService->comment($request);
        } catch (\Exception $exception) {
            ErrorMsgServive::write($exception, request()->url());
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }

    //动态评论回复
    public function commentReply(Request $request)
    {
        try {
            $this->feedsService->commentReply($request);
        } catch (\Exception $exception) {
            ErrorMsgServive::write($exception, request()->url());
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }

    //动态举报
    public function report(Request $request)
    {
        try {
            $this->feedsService->report($request);
        } catch (\Exception $exception) {
            ErrorMsgServive::write($exception, request()->url());
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }


    //我的动态
    public function myFeeds(Request $request)
    {
        $user = auth('api')->user();
        $team_id = $request->post('team_id', 0);
        $data = $this->feedsService->userFeeds($user->id, $team_id);

        return $this->success($data);
    }

    //转发 好友列表
    public function getFriends(Request $request)
    {
        $user = auth('api')->user();
        $FriendService = new FriendService();
        $data = $FriendService->getFriendsList($user->id, $request->post('keyword', ''));

        return $this->success($data);
    }

    //动态转发
    public function feedsForward(Request $request)
    {
        try {
            DB::beginTransaction();

            $feed_id = $request->post('feed_id');
            if (empty($feed_id)) {
                throw new Exception(trans('api.PARAMS_ERROR'));
            }
            $feed = Feed::query()->where(['id' => $feed_id])->whereNull('deleted_at')->first();
            if (!$feed) {
                throw new Exception(trans('api.FEED_NOT_EXIST'));
            }

            $user = auth('api')->user();
            $type = $request->post('type', 1); //转发类型  1转发至动态 2转发给用户 3转发给weixin或者Facebook；
            if ($type == 1) {
                $content = $request->post('content', ''); //转发内容
                //保存动态
                $feed_arr = array();
                $feed_arr['user_id'] = $user->id;
                $feed_arr['content'] = $content;
                $feed_arr['file_url'] = json_encode([]);
                $feed_arr['address_info'] = '';
                $feed_arr['match_title'] = '';
                $feed_arr['game_per_id'] = 0;
                //$feed_arr['team_id'] = $feed->team_id;
                $feed_arr['forward_id'] = $feed_id;
                if (!Feed::query()->create($feed_arr)) {
                    throw new Exception(trans('api.SAVE_ERROR'));
                }
            }
            //增加转发数量
            $feed->forward_num = $feed->forward_num + 1;
            $feed->save();
            //保存转发记录
            FeedForward::query()->create([
                'user_id' => $user->id,
                'feed_id' => $feed_id,
                'status' => 0,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            ErrorMsgServive::write($exception, request()->url());
            return $this->error($exception->getFile());
        }
        return $this->success();
    }
}
