<?php

namespace App\Http\Controllers\Web;

use App\CaseTopic;
use App\Helpers\ImageHelper;
use App\Helpers\Sms;
use App\Http\Controllers\Controller;
use App\Http\Requests\Web\CasesRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\CaseRequest;
use Mockery\Exception;


class CaseController extends Controller
{
    private $view = 'web.cases.';

    public function index()
    {
        $title = __('caseRequests.case_request');
        $topics = CaseTopic::withDescription();
        return view($this->view.'case_request', get_defined_vars());
    }

    /**
     * Cases request
     * @param CasesRequest $request
     * @return JsonResponse
     */
    public function caseRequest(CasesRequest $request)
    {
        $data = $request->all();
        $files = [];
        if ($request->files && count($request->files)){
            $allowedTypes = ['application/pdf','image/png', 'image/jpg', 'image/jpeg'];
            foreach ($request['files'] as $file) {
                if (!in_array($file->getMimeType(), $allowedTypes)){
                    $message = __('caseRequests.file_not_allowed');
                    $message = str_replace('@@', $file->getClientOriginalName(), $message);
                    return response()->json(['errors' => $message]);
                }

                $requestFile = ImageHelper::upload($file, 'cases_requests_files');

                if ($requestFile['errors'] ?? null){
                    return response()->json(['errors' => $requestFile['errors']]);
                }

                $files[] = $requestFile;
            }
        }
        $user = Auth::user();
        $data['user_id'] = $user->id;
        $data['name'] = $user->name;
        $data['email'] = $user->email;
        $data['phone'] = $user->phone;
        $data['request_files'] = $files;

        $case = CaseRequest::create($data);

        $phone = explode('0', $user->phone, 2);
        try {
            $message = __('caseRequests.case_sms_message') . $case->id;
            Sms::send($phone[1], $message);
        } catch (Exception $exception){}

        return response()->json(['success' => true, 'case_no' => $case->id]);

    }

    public function updateCaseRequest(Request $request, $caseRequestId)
    {

        if (!count($request->files)){
            return response()->json(['errors' => __('caseRequests.please_attach_file')]);
        }

        $caseRequest = CaseRequest::find($caseRequestId);
        $user = Auth::user();
        if (!$caseRequest || $caseRequest->user_id != $user->id){
            return response()->json(['errors' => __('caseRequests.no_data')]);
        }

        $newFiles = [];
        if ($request->file){
            $file = $request->file;
            $allowedTypes = ['application/pdf','image/png', 'image/jpg', 'image/jpeg'];
//            foreach ($request['files'] as $file) {
                if (!in_array($file->getMimeType(), $allowedTypes)){
                    $message = __('caseRequests.file_not_allowed');
                    $message = str_replace('@@', $file->getClientOriginalName(), $message);
                    return response()->json(['errors' => $message]);
                }

                if ($file->getMimeType() == 'application/pdf'){
                    $newFiles['type'] = 'pdf';
                } else {
                    $newFiles['type'] = 'image';
                }

                $request_files = ImageHelper::upload($file, 'cases_requests_files');
                if ($request_files['errors'] ?? null){
                    return response()->json(['errors' => $request_files['errors']]);
                }
                $newFiles['request_files'] = asset($request_files);

//            }
        }

        $requestFiles = $caseRequest->request_files;
        array_push($requestFiles, $newFiles['request_files']);

        $caseRequest->update(['request_files' => $requestFiles]);
        $newFiles['message'] = __('caseRequests.file_send_success');
        return response()->json($newFiles);

    }


}
