<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/5
 * Time: 15:58
 */
namespace App\Http\Controllers\Post;

use App\Repositories\Post\PostRepository;
use App\Transformers\Post\CommentTransformer;
use App\Transformers\Post\DetailTransformer;
use App\Transformers\Post\LogTransformer;
use App\Transformers\Post\PostStoreTransformer;
use App\Transformers\Post\PostTransformer;
use App\Transformers\Post\StoreDetailTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class PostController extends Controller
{
    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    /**
     * 内容列表
     */
    public function index(Request $request)
    {
        $productList = $this->postRepository->lists($request->all());
        $fractal = new Manager();
        $resource = new Collection($productList, new PostTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
        $data = $fractal->createData($resource)->toArray();
        $data['extra'] = [
            'filters' => [
                'keyword',
                'content',
                'topic_ids',
                'category_ids',
                'is_suggest',
                'created_at',
                'type',
                'sort'
            ],
            'columns' => [
                'id',
                'created_at',
                'uid',
                'topic',
                'content',
                'location',
                'pv',
                'praise_count',
                'share_count',
                'comment_count',
                'collect_count',
                'create_bean',
                'is_suggest',
                'is_hide',
            ]
        ];
        return $data;
    }

    /**
     * 回收站列表
     */
    public function waste(Request $request)
    {
        $params = $request->all();
        $params['waste'] = 1;
        $productList = $this->postRepository->lists($params);
        $fractal = new Manager();
        $resource = new Collection($productList, new PostTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
        $data = $fractal->createData($resource)->toArray();
        $data['extra'] = [
            'filters' => [
                'content',
                'topic_ids',
                'category_ids',
                'created_at',
                'sort'
            ],
            'columns' => [
                'id',
                'created_at',
                'uid',
                'topic',
                'content',
                'location',
                'pv',
                'praise_count',
                'share_count',
                'comment_count',
                'collect_count',
                'create_bean',
                'is_suggest',
            ]
        ];
        return $data;
    }

    /**
     * 内容详情
     */
    public function detail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|exists:post'
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }

        $post = $this->postRepository->detail($request->all());
        return $this->response->item($post, new DetailTransformer());
    }

    /**
     * 内容类型
     */
    public function getType(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|exists:post'
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }

        return $this->postRepository->getType($request->all());
    }

    /**
     * 评论列表
     */
    public function commentList(Request $request)
    {
        $commentList = $this->postRepository->commentList($request->all());
        $fractal = new Manager();
        $resource = new Collection($commentList, new CommentTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
        $data = $fractal->createData($resource)->toArray();

        $data['extra'] = [
            'filters' => [

            ],
            'columns' => [
                'id',
                'post_id',
                'parent_id',
                'uid',
                'content',
                'created_at',
                'is_delete',
            ]
        ];
        return $data;
    }

    /**
     * 发布内容
     */
    public function create(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'uid' => 'required|integer',
            'type' => ['required',Rule::in('image', 'video', 'html')],
            'is_suggest' => ['required',Rule::in(0, 1)],
            'img' => 'required|url',
            'video' => 'required_if:type,video|string|url',
            'video_id' => 'required_if:type,video|string',
            'topic_ids' => 'required|string|max:64',
            'title' => 'nullable|string|max:20',
            'content' => 'required|string',
            'location' => 'nullable|string|max:20',
            'imgs' => 'required_if:type,image|array|max:9',
            'imgs.*' => 'required|url',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->create($request->all());
    }

    /**
     * 评论&回复
     */
    public function comment(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'post_id' => 'required|integer',
            'uid' => 'required|integer',
            'content' => 'required|string|max:150',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->comment($request->all());
    }

    /**
     * 增加数据
     */
    public function addData(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'post_id' => 'required|exists:post,id',
            'add_pv' => 'required|integer|min:0',
            'add_praise_count' => 'required|integer|min:0',
            'add_collect_count' => 'required|integer|min:0',
            'add_share_count' => 'required|integer|min:0',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->addData($request->all());
    }

    /**
     * 推荐内容
     */
    public function suggest(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->suggest($request->all());
    }

    /**
     * 删除内容
     */
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->delete($request->all());
    }

    /**
     * 复原内容
     */
    public function restore(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->restore($request->all());
    }

    /**
     * 删除评论
     */
    public function commentDelete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->commentDelete($request->all());
    }

    /**
     * 隐藏内容
     */
    public function hide(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->hide($request->all());
    }

    /**
     * 日志列表
     */
    public function log(Request $request)
    {
        $commentList = $this->postRepository->log($request->all());
        $fractal = new Manager();
        $resource = new Collection($commentList, new LogTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
        $data = $fractal->createData($resource)->toArray();

        $data['extra'] = [
            'filters' => [
                'log_type',
                'created_at',
            ],
            'columns' => [
                'id',
                'username',
                'log_type',
                'created_at',
                'content',
            ]
        ];
        return $data;
    }

    public function statistics(Request $request){
        $request = $request->all();
        $start = Carbon::parse(!empty($request['start']) ? Carbon::parse($request['start'])->startOfDay()->toDateTimeString() : Carbon::yesterday())->startOfDay()->toDateTimeString();
        $end = Carbon::parse(!empty($request['end']) ? Carbon::parse($request['end'])->endOfDay()->toDateTimeString() : Carbon::yesterday())->endOfDay()->toDateTimeString();
        return $this->postRepository->statistics($start,$end);
    }

    /**
     * 编辑内容话题
     */
    public function updateTopic(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'topic_ids' => 'required|string|max:64',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->postRepository->updateTopic($request->all());
    }

    /**
     * 获取网站内容
     */
    public function createStore(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'start_page' => 'required|integer',
            'end_page' => 'required|integer',
            'size' => 'required|integer',
            'category_id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }

        return $this->postRepository->createStore($request['start_page'], $request['end_page'], $request['size'], $request['category_id']);
    }

    /**
     * 获取网站内容
     */
    public function getStore(Request $request)
    {
        $productList = $this->postRepository->getStore($request->all());
        $fractal = new Manager();
        $resource = new Collection($productList, new PostStoreTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
        $data = $fractal->createData($resource)->toArray();
        $data['extra'] = [
            'filters' => [
                'type',
                'category_id',
                'title',
                'content',
                'source',
                'is_used',
            ],
            'columns' => [
                'id',
                'source',
                'type',
                'category_id',
                'title',
                'content',
                'img',
                'is_used',
                'created_at',
            ]
        ];
        return $data;
    }

    /**
     * 网站内容详情
     */
    public function getStoreDetail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|exists:post_store'
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }

        $post = $this->postRepository->getStoreDetail($request->all());
        return $this->response->item($post, new StoreDetailTransformer());
    }
}