<?php

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Core\Service\PropertyRoomPhotoMappingService;
use App\Core\Service\PropertyPhotoService\PropertyPhotoService;


use Predis\Replication\RoleException;
use Illuminate\Http\Request;

use App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;

class PropertyRoomPhotoMappingController extends Controller
{


    private $request;
    private $propertyRoomPhotoMappingService;
    private $propertyPhotoService;


    public function __construct(
        Request $request,
        PropertyRoomPhotoMappingService $propertyRoomPhotoMappingService,
        PropertyPhotoService $propertyPhotoService
    )
    {
        $this->request = $request;
        $this->propertyRoomPhotoMappingService = $propertyRoomPhotoMappingService;
        $this->propertyPhotoService = $propertyPhotoService;

    }

    public function updatePropertyRoomPhotoMapping(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try
        {
            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = json_decode($this->request->getContent(),1);
            $params = current($params);
            $params['user_id'] =  $this->request->auth->id;

            $response =  $this->propertyRoomPhotoMappingService->updatePropertyRoomPhotoMapping($params);

        }catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }

        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPropertyRoomPhoto(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestFact = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'room_id' => fillOnUndefined($params, 'room_id'),
            ];

            $getPropertyPhoto = $this->propertyPhotoService->getPropertyRoomPhoto($requestFact);
            if ($getPropertyPhoto['status'] != 'success') {
                throw new ApiErrorException($getPropertyPhoto['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $getPropertyPhoto['data'] ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }


}
