<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Libraries\Helper;
use App\Libraries\Validation;
use App\Repositories\PostRepository;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index(Request $request)
    {
        $payload = $request->all();
        $filters = ['page' => isset($payload['page']) && $payload['page'] ? $payload['page'] : 1];
        $data = Helper::preparePagePayload('Posts List');
        $data['posts'] = PostRepository::fetchItems($filters)->toArray();

        return view('admin.post.index', $data);
    }

    public function deletePost(Request $request)
    {
        $payload = $request->all();
        if (isset($payload['id']) && $payload['id']) {
            $status = PostRepository::deletePost($payload);
            if ($status) {
                return response()->json(['status' => true]);
            }
        }

        return response()->json(['status' => false]);
    }

    public function save($id = null)
    {
        $data = Helper::preparePagePayload('Save Post');
        if ($id) {
            $data['post'] = PostRepository::fetchPost(['id' => $id]);
            if (! $data['post']) {
                return redirect('admin/posts');
            }
        }

        return view('admin.post.save', $data);
    }

    public function savePost(Request $request)
    {
        $payload = $request->all();
        $result = Validation::validatePayload($request->all(), ['title' => 'required','slug' => 'required', 'description' => 'required']);
        if ($result['status']) {

            $image = $request->file('image');
            if (! $payload['id'] && ! $image) {
                return response()->json(['status' => false, 'message' => 'Image File required.']);
            }
            if($image){
                $extension = $image->getClientOriginalExtension();
                if (! in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
                    return response()->json([
                        'status' => false,
                        'message' => 'Invalid File.Only png, jpg, jpeg and gif file allowed to upload.',
                    ]);
                }

                $uploaded_path = public_path('images');
                if (! is_writable($uploaded_path)) {
                    return response()->json(['status' => false, 'message' => 'File directory is not writable.']);
                }
                $file_name = rand().'.'.$extension;
                $uploadSuccess = $image->move($uploaded_path, $file_name);
                if (! $uploadSuccess) {
                    return response()->json(['status' => false, 'message' => 'Unable to upload file.']);
                }
                $payload['media'] = $file_name;
            }

            $status = PostRepository::savePost($payload);
            if ($status) {
                return response()->json(['status' => true]);
            }

            return response()->json(['status' => false]);
        }

        return response()->json($result);
    }
}
