<?php


namespace App\Http\Controllers\Api;


use App\Enums\ApiEnum;
use App\Exceptions\VipException;
use App\Http\Params\DynamicParam;
use App\Http\Params\DynamicZanParam;
use App\Http\Params\UserReportParam;
use App\Models\Banner;
use App\Models\SystemConfig;
use App\Services\DynamicService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use PHPUnit\Util\Exception;

class DynamicController extends Controller
{
    protected $dynamicService;

    public function __construct(DynamicService $dynamicService)
    {
        $this->dynamicService = $dynamicService;
    }

    /**
     * 获取话题列表
     */
    public function get_tag_list(Request $request){
        try {
            $data = $this->dynamicService->tag_list($request->keyword,$request->post('type',0));
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($data);
    }

    /**
     * 删除话题
     */
    public function del_tag(Request $request){
        try {
            $this->dynamicService->del_tag($request->id);
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>"删除成功"]);
    }


    /**
     * 发布动态
     */
    public function release(Request $request){
        $validator = Validator::make($request->all(), [
            'content' => 'required',
        ], [
            'content.required'=>"内容必须",
        ]);
        if ($validator->fails()) {
            return $this->response()->errorForbidden($validator->messages()->first());
        }
        DB::beginTransaction();
        try {
            //throw new Exception(json_encode($request->post('tag')));
            $user = auth('api')->user();
            $lock = Cache::lock(ApiEnum::DYNAMIC_RELEASE.$user->id, 1);
            if ($lock->get()) {
                $dynamicParam = new DynamicParam();
                $dynamicParam->user_id = $user->id;
                $dynamicParam->content = htmlspecialchars($request->post('content'));
                $dynamicParam->img_url = $request->post('img_url');
                $dynamicParam->type = $request->post('type');
                $dynamicParam->status = 1;
                $dynamicParam->zan_num = 0;
                $dynamicParam->tag = $request->post('tag');
                $dynamicParam->city = $request->post('city');
                $dynamicParam->latitude = $request->post('latitude');
                $dynamicParam->longitude = $request->post('longitude');
                $this->dynamicService->release($dynamicParam);
                DB::commit();
                $lock->release();
            }else{
                throw new Exception("请求太频繁");
            }
        }catch (VipException $exception){
            return response()->json(['message'=>$exception->getMessage()])->setStatusCode(410);
        }catch (\Exception $exception){
            DB::rollBack();
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>"发布成功"]);
    }

    /**
     * 动态banner,话题，风险提示
     */
    public function dynamic_info(){
        try {
            $oss_config = config("filesystems.disks.oss");
            $banner = Banner::where(["site"=>1,"status"=>1])->orderBy("sort",'asc')->limit(5)->get(['id','img_url','url']);
            foreach ($banner as $k=>$v){
                $banner[$k]['img_url'] = "https://".$oss_config['bucket'].'.'.$oss_config['endpoint'].'/'.$v['img_url'];
            }
            $res['banner'] = $banner;
            $res['tag'] = DB::table("dynamic_tag")->where('type',0)->orderBy('hot','desc')->limit(10)->get();
            $res['fx'] = SystemConfig::query()->where('id',1)->value("value");
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($res);
    }

    /**
     * 动态列表
     */
    public function get_list(Request $request){

        $where = array();
        $where['type'] = $request->post('type',1); //类型  type 1全部  2关注  3附近
        $where['look_type'] =$request->post('look_type',3);//查看类型  type 1只看男士  2只看女士  3全部
        $where['tag_id'] =$request->post('tag_id',0); //话题标签
        $where['user_id'] =0;
        try {
            $list = $this->dynamicService->dynamic_list($where);
        }catch (\Exception $exception){

            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($list);
    }



    /**
     * 我的动态
     */
    public function my_list(Request $request){
        $user = auth('api')->user();
        $where = array();
        $where['type'] = $request->post('type',1); //类型  type 1全部  2关注  3附近
        $where['look_type'] =$request->post('look_type',3);//查看类型  type 1只看男士  2只看女士  3全部
        $where['tag_id'] =$request->post('tag_id',0); //话题标签

        $where['user_id'] =$user->id;

        try {
            $list = $this->dynamicService->dynamic_list($where);
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($list);
    }

    /**
     * 动态点赞
     */
    public function zan(Request $request){
        try {
            $user = auth('api')->user();
            $lock = Cache::lock(ApiEnum::DYNAMIC_ZAN.$user->id, 1);
            if ($lock->get()) {
                DB::beginTransaction();
                $dynamic_zan_param = new DynamicZanParam();
                $dynamic_zan_param->user_id = $user->id;
                $dynamic_zan_param->dynamic_id = $request->id;
                $zan = $this->dynamicService->zan($dynamic_zan_param);
                DB::commit();
                $lock->release();
            }else{
                throw new Exception("请求太频繁");
            }
        }catch (\Exception $exception){
            DB::rollBack();
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>$zan==1?"点赞成功":"取消点赞",'zan'=>$zan]);
    }

    /**
     * 删除动态
     */
    public function del(Request $request){
        try {
            $DynamicParam = new DynamicParam();
            $DynamicParam->id = $request->id;
            $this->dynamicService->del($DynamicParam);
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>"删除成功"]);
    }

    /**
     * 举报动态
     */
    public function report(Request $request){
        try {
            $user = auth('api')->user();
            $UserReportParam = new UserReportParam();
            $UserReportParam->type = $request->post('type');  //1用户 2动态
            $UserReportParam->user_id = $user->id;
            $UserReportParam->content = $request->post('content');
            $UserReportParam->report_id = $request->post('id');
            $UserReportParam->tencent_im_user_id = $request->post('tencent_im_user_id');
            $UserReportParam->status = 0;
            $UserReportParam->img_url = $request->post('img_url');
            $UserReportParam->info = $request->post('info',"");
            $this->dynamicService->report($UserReportParam);
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>"举报成功"]);
    }
}
