<?php

namespace App\Http\Controllers;

use App\Models\Course;
use App\Models\DwbsUser;
use App\Models\Like;
use App\Models\Read;
use App\Models\Share;
use App\Models\Comment;
use App\Models\Collection;
use Carbon\Carbon;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use FFMpeg\FFMpeg;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;

class CourseController extends Controller
{
    //首页展示数据
    public function getDataInfo(Request $request)
    {
        $data['count']=Read::count();
        return $this->success($data);
    }
    //获取列表
    public function list(Request $request): \Illuminate\Http\JsonResponse
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $c_id=$request->input('c_id');
        $num=$page_size*($page_index-1);
        $data=Course::query()->where('state',1);
        if($c_id){
            $data->where('c_id',$c_id);
        }
        $count=$data->count();
        $list=$data->orderBy('created_at','desc')
            ->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }

    //最近上传
    public function current(Request $request): \Illuminate\Http\JsonResponse
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $num=$page_size*($page_index-1);
        $count=Course::query()->where('state',1)
            ->where('created_at','>',Carbon::now()->subWeeks(1))->count();
        $list=Course::query()->where('state',1)
            ->where('created_at','>',Carbon::now()->subWeeks(1))
            ->orderBy('created_at','desc')
            ->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }

    //获取单个信息详情
    public function detail(Request $request)
    {
        $id=$request->input('id');
        $user=Auth::guard('api')->user();
        $res=null;
        if($user){
           $res = Like::where('user_id',$user->id)->where('article_id',$id)->exists();
        }
        $info['article']=Course::query()->where('id',$id)->first();
        if($res){
            $info['user']['is_like'] = 1;
        }else{
            $info['user']['is_like'] = 0;
        }

        $info['comment']=Comment::where('article_id',$id)->with(['user:id,nickname,headimgurl'])->get();
        return $this->success($info);
    }

    //点赞
    public function like(Request  $request): \Illuminate\Http\JsonResponse
    {
        $user_id=Auth::guard('api')->user()->id;
        $article_id=$request->input('article_id');
        //        $res=Like::create([
        //            'user_id'=>$user_id,
        //            'article_id'=>$article_id
        //        ]);

        //        $res=Redis::sismember('like:article', $article_id.':'.$user_id);
        //        if($res){
        //            Redis::srem('like:article', $article_id.':'.$user_id);
        //            Redis::decr('article_'.$article_id.'_count');
        //            Redis::hdel('article_user_like', $article_id.'_'.$user_id);
        //        }else{
        //            Redis::sadd('like:article', $article_id.':'.$user_id);
        //            Redis::incr('article_'.$article_id.'_count');
        //            Redis::hset('article_user_like', $article_id.'_'.$user_id, Carbon::now());
        //        }
        $res=Like::query()->where('user_id',$user_id)->where('article_id',$article_id)->first();
        try{
            DB::transaction(function()use($res,$user_id,$article_id) {
                if ($res) {
                    Course::query()->where('id', $article_id)->decrement('like');
                    Like::query()->where('user_id', $user_id)->where('article_id', $article_id)->delete();
                } else {
                    Course::query()->where('id', $article_id)->increment('like');
                    Like::query()->create([
                        'user_id' => $user_id,
                        'article_id' => $article_id,
                    ]);
                }
            },5);
            return $this->success([]);
        }catch(\Exception $e){
            return $this->error();
        }
        //        Redis::set('name','wetalk');
        //        return Redis::get('name');
    }

    //浏览
    public function read(Request  $request): \Illuminate\Http\JsonResponse
    {
        $user_id = Auth::guard('api')->user()->id;
        $article_id = $request->input('article_id');
        try{
            DB::transaction(function()use($user_id,$article_id){
                Read::create([
                    'user_id' => $user_id,
                    'article_id' => $article_id
                ]);
                Course::where('id',$article_id)->increment('read');
            },5);
            return $this->success([]);
        }catch(\Exception $e){
            return $this->error();
        }
    }

    //分享
    public function share(Request  $request): \Illuminate\Http\JsonResponse
    {
        $user_id=Auth::guard('api')->user()->id;
        $article_id=$request->input('article_id');
        try{
            DB::transaction(function()use($user_id,$article_id){
                Share::create([
                    'user_id'=>$user_id,
                    'article_id'=>$article_id
                ]);
                Course::where('id',$article_id)->increment('share');
            },5);
            return $this->success([]);
        }catch(\Exception $e){
            return $this->error();
        }
    }


    //提交评论
    public function addComment(Request  $request)
    {
        $user_id=Auth::guard('api')->user()->id;
        $content=$request->input('content');
        if($request->input('article_id')=='wetalk'){
            $article_id=0;
        }else{
            $article_id=$request->input('article_id');
        }
        $res=Comment::create([
            'user_id'=>$user_id,
            'article_id'=>$article_id,
            'content'=>$content,
            'is_audit'=>0
        ]);
        if($res){
            return $this->success([]);
        }else{
            return $this->error();
        }
    }

    //获取微聊所有评论
    public function getAllWetalkComment(Request  $request)
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $num=$page_size*($page_index-1);
        $count=Comment::where('article_id',0)->count();
        $list=Comment::where('article_id',0)->with(['user:id,nickname,headimgurl'])->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }

    //获取微聊所有点赞
    public function getArticleLike(Request $request)
    {
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $sort=$request->input('sort');
        $num=$page_size*($page_index-1);
        $user_id=Auth::guard('api')->user()->id;
        $count=Like::where('user_id',$user_id)->count();
        $list=Like::where('user_id',$user_id)->has('article')->with(['user:id,nickname,headimgurl','article'=>function($query){
            $query->where('id','>',0);
        }])->orderBy('created_at',$sort)->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$count);
    }

    //获取微聊首页三数据
    public function getWetalkData(Request  $request)
    {
        $data['like']=Like::where('article_id',0)->count();
        $data['comment']=Comment::where('article_id',0)->count();
        $data['share']=Share::where('article_id',0)->count();
        return $this->success($data);
    }

    //获取微信授权
    public function getSignPackage()
    {
        $config = config('wechat.official_account.daweiboshi');
        $app = Factory::officialAccount($config);
        $url='https://weiliao.woaidakele.cn/api/gzh';
        $app->jssdk->setUrl($url);
        $data=$app->jssdk->buildConfig(array('onMenuShareAppMessage','onMenuShareTimeline',
            'openLocation','getLocation','chooseImage',
            'uploadImage', 'downloadImage','scanQRCode',
            'updateAppMessageShareData', 'updateTimelineShareData'), true);
        return $this->success($data);
    }


}
