<?php

namespace App\Http\Controllers;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class CmsController extends Controller
{
    public function content_update(Request $request, $content_id) {
        $body = $request->body;
        if($request->file('image')){
            $path = '/images/' . 'cms' . '/' . 'Post/';
            $imageName = Storage::disk('public')->putFile($path, $request->file('image'));
            $imagePath = implode('/', [env('APP_URL'), 'storage', $imageName]);
            $body = $imagePath;
        }

        $response = $this->send_request("POST", "content", "update", $content_id, ['body' => $body]);
        return response($response->json(), $response->status());
    }

    public function blog_create(Request $request) {
        $path = '/images/' . 'cms' . '/' . 'Post/';
        $imageName = Storage::disk('public')->putFile($path, $request->file('image'));
        $imagePath = implode('/', [env('APP_URL'), 'storage', $imageName]);
        $thumbnailsPath = public_path('storage'.$path.'/thumbnails');

        if(!File::isDirectory($thumbnailsPath))
            File::makeDirectory($thumbnailsPath, 0777, true, true);
            
        $thumbnail = \IImage::make('storage'.'/'.$imageName)->resize(300, 300, function ($constraint) {
            $constraint->aspectRatio();
        });

        $thumbnail->dirname = $thumbnailsPath;
        $thumbnail->save();

        $response = $this->send_request("POST", "blog", "create", "", Arr::except($request->all(), ['user_token']) + [
            'user_token' => auth()->user()->token,
            'image_alt' => $request->input('alt'),
            'image_path'      => $imagePath,
            'image_thumbnail' => implode('/', [env('APP_URL'), 'storage', substr($path, 1), 'thumbnails', $thumbnail->basename]),
        ]);
        return response($response->json(), $response->status());
    }
    public function blog_update(Request $request, $blog_id) {
        $imagePath = '';
        $thumbnailPath = '';
        if($request->hasFile('image')){
            $path = '/images/' . 'cms' . '/' . 'Post/';
            $imageName = Storage::disk('public')->putFile($path, $request->file('image'));
            $imagePath = implode('/', [env('APP_URL'), 'storage', $imageName]);
            $thumbnailsPath = public_path('storage'.$path.'/thumbnails');

            if(!File::isDirectory($thumbnailsPath))
                File::makeDirectory($thumbnailsPath, 0777, true, true);
                
            $thumbnail = \IImage::make('storage'.'/'.$imageName)->resize(300, 300, function ($constraint) {
                $constraint->aspectRatio();
            });

            $thumbnail->dirname = $thumbnailsPath;
            $thumbnail->save();
            $thumbnailPath = implode('/', [env('APP_URL'), 'storage', substr($path, 1), 'thumbnails', $thumbnail->basename]);
        }

        $response = $this->send_request("POST", "blog", "update", $blog_id, Arr::except($request->all(), ['user_token', 'image_path', 'image_thumbnail', 'image_alt']) + [
            'user_token' => auth()->user()->token,
            'image_alt' => $request->input('alt'),
            'image_path'      => $imagePath,
            'image_thumbnail' => $thumbnailPath,
        ]);
        return response($response->json(), $response->status());
    }

    public function blog_delete($blog_id){

        $response = $this->send_request("DELETE", "blog", "delete", $blog_id, []);
        return response($response->json(), $response->status());
    }

    public function send_request($method, $model, $action, $id, $data){
        
        $request = Http::withHeaders([
            'Authorization' => env('secret'),
            'Accept' => 'application/json' 
        ])->withOptions(['verify' => false]);

        $response = null;
        if($method == "POST"){
            $response = $request->post(env('CMS_ADDRESS').'/api/admin/'.$model.'/'.$action.($id != "" ? '/' : '').$id, [
                'user_token' => auth()->user()->token,
            ] + $data);
           
        }
        else if($method == "DELETE"){
            $response = $request->delete(env('CMS_ADDRESS').'/api/admin/'.$model.'/'.$action.($id != "" ? '/' : '').$id, [
                'user_token' => auth()->user()->token,
            ] + $data);
        }
        return $response;
    }
}
